0

I was struggling with adding template driven form validation in my angular2 app.

i have a forked the plunker https://plnkr.co/edit/phhe74kAUmNZgNSmcsvm?p=preview

i failed to achieve the form validation for my custom component alone and other html components are working fine.

In the plunker, if you start typing name and street it will updated in both form and and my model object as well. but the when you start typing postcode it will update only in my model object and not with form.

I wanted the form to be invalid if i make postcode filed to empty.

Is i missed something here ?

1 Answer 1

2

Currently, your form doesn't recognize your child components' input field as a form field, you need to change your child tag a bit... to have the name attribute and ngModel to bind this child component to the form. Also include required here.

Lastly, you need to add ngDefaultControl to have two-way binding for the form field. So your child tag should look like this in your parent:

<my-input name="postcode" ngModel ngDefaultControl required [(value)]="user.address.postcode"></my-input>

Here is your forked

Plunker

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.