0

I am Struggle With Dynamic Form Validation In My Current Project, in my Form Having Two Fields like User Name and Email, Both Are Mandatory.

If User Click Add-More(Up to 2 times only) Button Then Same above fields are came. those two fields are also Mandatory. if user removes those fields validation will not be work

I Already Wrote those 2 fields(3 times) just, I Kept ng-hide and ng-show, And My Code is below,

enter code here

I have Added My Code To Plunker: http://plnkr.co/edit/ErJl2Kg8maOn5GLaz9mb?p=preview

0

3 Answers 3

3

Avoid to use ng-hide or ng-show. Prepare on model i.e. JSON Array and bind using ng-repeat and write associated code in JavaScript.

Example: Click here for Example

Sign up to request clarification or add additional context in comments.

Comments

0

Use ng-required="showUser2" instead of just required.

7 Comments

@HaRdik.. Can You please Guide Me How To Write.. My Above Code Using ng-repeat
Example: Click here for Example Try this example.
@HaRdik.. I follow Example as u mentioned. In My Database there is 3 records. then it comes 3 times. its looks fine. But In Another Situation In My DB There is No records. In This Case, there is nothing to display(including Form also). But I Need those 2 fileds 3 times (if having data or not).. if there is no data. then user will be added to db. if data is there then user may be chance to update the data. please guide me what can i do.
check in your controller if the result from post method that return data the list is empty, just push into your list one empty object and you will have the empty form there
I would check if response.status == 200 cause the way you have it here, you should send from backend data="fail" which is not ok. But the code you have is the ok. Just make sure you have initialized the $scope.formDataTwo = {} and $scope.formDataTwo.premiumusers = [] somewhere in your controller
|
0

In an AngularJS-form a field is still required even when its hidden. That means when you hide your required form input field with ng-hide or ng-show then the AngularJS FormController completeProfile still treats the form as $invalid.

To get dynamic forms how you want them you can use ng-if to hide your unwanted input fields. Input fields inside of a ng-if are not considered by the form validation logic.

I updated your Plunker. The submit button is only shown when the form is valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.