Skip to content

Put many forms in same $scope and validate for separate. #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mrpotato3 opened this issue Nov 18, 2014 · 5 comments
Closed

Put many forms in same $scope and validate for separate. #169

mrpotato3 opened this issue Nov 18, 2014 · 5 comments

Comments

@mrpotato3
Copy link

Hi. I have a problem with form validation. I need to put more than one different forms and validate/send data separate.

I have:

$scope.schema1 = ...
$scope.schema2 = ...
$scope.schema3 = ...
$scope.form1 = ...
$scope.form2 = ...
$scope.form3 = ...
$scope.model1 = ...
$scope.model2 = ...
$scope.model3 = ...
$scope.onSubmitForm1 =function (form)
{
   $scope.$broadcast('schemaFormValidate');  ---> PROBLEM, BROADCAST VALIDATES ALL.
}
$scope.onSubmitForm2 =function (form)
{
   $scope.$broadcast('schemaFormValidate');  ---> PROBLEM, BROADCAST VALIDATES ALL.
}
$scope.onSubmitForm3 =function (form)
{
   $scope.$broadcast('schemaFormValidate');  ---> PROBLEM, BROADCAST VALIDATES ALL.
}

I hope somebody can help me.

Thanks!

@mrpotato3 mrpotato3 changed the title How can I have 2 o 3 different forms in same $scope and validate for separate. Put many forms in same $scope and validate for separate. Nov 18, 2014
@mike-marcacci
Copy link
Contributor

Hey @mrpotato3, one easy way to make these work separately is to use 3 separate controllers:

<div ng-controller="form1"> ... </div>
<div ng-controller="form2"> ... </div>
<div ng-controller="form3"> ... </div>

This way $scope broadcast will only effect the form on the same or child scopes.

@mrpotato3
Copy link
Author

Hi @mike-marcacci!
Thank you for your answer. I know this solution but I'm not sure if it's the best option... is the unique option?
Thanks.

@davidlgj
Copy link
Contributor

davidlgj commented Jan 7, 2015

I'd say that @mike-marcacci solution sounds good!

@davidlgj davidlgj closed this as completed Jan 7, 2015
@tmoc
Copy link

tmoc commented Mar 6, 2015

I also had this issue and I disagree that multiple controllers is a good solution. Reason being, it drastically increases complexity in some scenarios, such as when you want to share form behavior logic across different parts of the app. It forces you to make multiple controllers all over the place that do nothing but use a single service.

@Alttaf
Copy link

Alttaf commented Oct 9, 2015

+1, you should not have to declare multiple controllers to manage just one service gets repetitive and goes agains DRY principles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants