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

Error "Multiple select model should be array" in NgForm #348

Closed
ghost opened this issue Mar 13, 2018 · 12 comments
Closed

Error "Multiple select model should be array" in NgForm #348

ghost opened this issue Mar 13, 2018 · 12 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Mar 13, 2018

Expected behaviour

Error Error: Multiple select model should be array. should not happen when <ng-select> is being used inside NgForm.

Actual behaviour

The error happens when ngModel is null and when ngModel is a predefined array.
Demo: https://stackblitz.com/edit/angular-jtj3dd?file=app%2Fapp.component.html

More Info

ng-select version: 0.26.1
browser: Google Chrome
reproducible in demo page: NO (demo doesn't have NgForm example)

@varnastadeus
Copy link
Member

@Maxkorz there is validation error since you are binding same property city in three different dropdowns while multiple dropdown requires value of type array.

@ghost
Copy link
Author

ghost commented Mar 13, 2018

@varnastadeus Sorry but you are wrong. Each dropdown is unique as it is bound to local <form>. Each city property is unique within each form.

I made three different dropdowns to show the error in different scenarios.

If you want, here's a single dropdown https://stackblitz.com/edit/angular-tr8vjg?file=app/app.component.css I'm still getting the error.

@varnastadeus
Copy link
Member

varnastadeus commented Mar 13, 2018

@Maxkorz sorry I overlooked that. Error is happening because resetForm sets initial value to empty string "" and ng-select requires it to be array.

@varnastadeus varnastadeus reopened this Mar 13, 2018
@ghost
Copy link
Author

ghost commented Mar 13, 2018

@varnastadeus but resetForm sets initial value to city: ['Nida'],....
It works fine, but I get that annoying error in the developer console.

Also, the error happens when initial value is null. Which is not correct, as null should be skipped

    private _validateWriteValue(value: any) {
        if (!isDefined(value)) {
            return;
        }

@varnastadeus
Copy link
Member

varnastadeus commented Mar 13, 2018

@Maxkorz it doesn't happens when it's null. Firstly when form is created initial value of city is "" and then ['Nida'] thats why error happens. Maybe you should use reactive forms or use [(ngModel)] 🤔

@ghost
Copy link
Author

ghost commented Mar 13, 2018

@varnastadeus

it doesn't happens when it's null.

It does happen for me. Take a look at this demo https://stackblitz.com/edit/angular-fcpv2n?file=app/app.component.ts

Maybe you should use reactive forms or use [(ngModel)]

I'm using a basic feature of Angular. I'm pretty sure the issue is somewhere in your plugin. I'm using over 20 3rd party modules in my project and only this plugin has such issue.

@ghost
Copy link
Author

ghost commented Mar 13, 2018

I forked the code and added console.log to every method in NgSelectComponent

Here's what I got:

For some reasonwriteValue function executes twice. The second call is made with an empty string out of nowhere

@varnastadeus
Copy link
Member

It doesn't comes out of nowhere, it comes from form you are having.

@ghost
Copy link
Author

ghost commented Mar 13, 2018

You were right. That's how Angular works. writeValue is called twice and the second call is made with an empty string for some reason.

Maybe you could extend "isDefined" method to filter out empty strings?

@anjmao
Copy link
Member

anjmao commented Mar 14, 2018

Simply workaround would for now would be to initial ngModel with empty array like this [ngModel]="[]"

But this is a bug, ngModel should not need to be set at all if it is used only for form to gather values. I will fix it.

@anjmao anjmao self-assigned this Mar 14, 2018
@anjmao anjmao added the bug label Mar 14, 2018
@varnastadeus varnastadeus added this to the Version 1.0.0 milestone Mar 14, 2018
jakemdunn pushed a commit to jakemdunn/ng-select that referenced this issue Oct 16, 2018
* fix: skip writeValue handling if value is empty fixes ng-select#348 

* warn if ngModel is not valid and skip
@hemuA1
Copy link

hemuA1 commented Oct 6, 2021

How the error can be resolved?
Reply

@NenadZivotic
Copy link

If you still have an error, check the form control initial value when creating the group. Example:

this.fb.group({someValue: new FomControl({value: []})}) -> this should be this.fb.group({someValue: new FomControl([])}), as this syntax with ({value: []}) can introduce this issue.

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

No branches or pull requests

4 participants