Skip to content
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

feat(forms): add support for standalone ngModel dirs inside forms #9522

Merged
merged 1 commit into from
Jun 23, 2016

Conversation

kara
Copy link
Contributor

@kara kara commented Jun 23, 2016

This PR allows the use of ngModels within forms without necessarily requiring the registration of their form controls with the parent form.

r: @vsavkin
cc: @StephenFluin: can you look at the new error message?

Before
If you wanted to include an ngModel within a form tag, it had to have a name attribute so that the ngModel could be registered with the parent form properly (for aggregate form validation and value).

<form>
    <input [(ngModel)]="person.name" name="first">
    <input [(ngModel)]="person.food">      //  always throws error because name is not set
</form>

However, in some rare cases, it might be desirable to have an ngModel within a form tag, but not registered with the form. This setup was not supported and would throw an error.

After

<form #f="ngForm">
    <input [(ngModel)]="person.name" name="first">
    <input [(ngModel)]="person.food" [ngModelOptions]="{standalone: true}"> 
</form>

{{ f.value | json }}       // {first: ""}

Now, if for some reason you need to have a standalone ngModel within a form tag, it's still usable without registering it with the parent form by setting the standalone property in ngModelOptions. This way, you can explicitly dictate that you don't want the form registration, but users who may forget the name attribute will still get a helpful error.

It's worth noting that ngModels can still, as always, be used outside of a form tag without a name attribute or an ngModelOptions property.

@kara kara added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jun 23, 2016
@@ -93,30 +92,37 @@ export class NgModel extends NgControl implements OnChanges,
return composeAsyncValidators(this._asyncValidators);
}

get isStandalone(): boolean {
Copy link
Contributor

@vsavkin vsavkin Jun 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it public or private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably private; I'll update.

@vsavkin vsavkin added pr_state: LGTM and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Jun 23, 2016
@kara kara merged commit 6edf047 into angular:master Jun 23, 2016
@kara kara deleted the ngModelOptions branch June 27, 2016 05:28
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants