Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

select directive should allow user to handle missing option for given model value #10127

Closed
mdvorak opened this issue Nov 19, 2014 · 4 comments
Closed

Comments

@mdvorak
Copy link

mdvorak commented Nov 19, 2014

Angular 1.3.3, single-value select directive

Today, when model has value that is not in ng-options collection, it inserts either placeholder option or empty option with '?' value. There is however no way to validate this state, since its all stored in private variables and model is considered valid. It is impossible to mark such field visually using css.

Directive should set a flag on ngModel controller to allow validation. All logic is now however in its $render method, and I'm not sure its the right place to do that, althou it would probably work.

When this is done, it would be easy to add custom validator that would check this flag and mark the field as invalid. Such validation directive could be part of core, since its quite common use-case, that non-existent value in select is invalid value.

@petebacondarwin petebacondarwin added this to the Backlog milestone Nov 20, 2014
@petebacondarwin petebacondarwin modified the milestones: 1.3.x, Backlog Nov 20, 2014
@petebacondarwin
Copy link
Member

I think this sounds reasonable. Fancy putting together a PR?

@mdvorak
Copy link
Author

mdvorak commented Nov 20, 2014

I'll try to put something together. It's not trivial thou, so anyone feel free to contribute in any way.

@mdvorak
Copy link
Author

mdvorak commented Nov 21, 2014

As I'm thinking about it, I realised that this code itself may not have correct behavior:

      if (!multiple) {
        if (nullOption || viewValue === null) {
          // insert null option if we have a placeholder, or the model is null
          optionGroups[''].unshift({id:'', label:'', selected:!anySelected});
        } else if (!anySelected) {
          // option could not be found, we have to insert the undefined item
          optionGroups[''].unshift({id:'?', label:'', selected:true});
        }
      }

Using placeholder for unrecognized model value seems wrong to me. I think it would be nice to provide template option for invalid value as well, and keep placeholder option. With current behavior, when you have placeholder option specified, and unknown model value, it is presented as no value at all. It is also impossible to deselect the value. I think i'll create separate PR for this first.

@mdvorak
Copy link
Author

mdvorak commented Nov 21, 2014

This seems to work, I need to perform a lot of tests + unit tests, but anyway, take a look...
67af1e0

@Narretz Narretz modified the milestones: Backlog, 1.3.x - superluminal-nudge Sep 14, 2015
Narretz added a commit to Narretz/angular.js that referenced this issue Apr 26, 2017
Narretz added a commit to Narretz/angular.js that referenced this issue Apr 27, 2017
This allows custom directives to manipulate the select's and
ngModel's behavior based on the state of the unknown and
the empty option.

Closes angular#13172
Closes angular#10127
Narretz added a commit to Narretz/angular.js that referenced this issue Apr 27, 2017
This allows custom directives to manipulate the select's and
ngModel's behavior based on the state of the unknown and
the empty option.

Closes angular#13172
Closes angular#10127
Narretz added a commit to Narretz/angular.js that referenced this issue Apr 27, 2017
This allows custom directives to manipulate the select's and
ngModel's behavior based on the state of the unknown and
the empty option.

Closes angular#13172
Closes angular#10127
Narretz added a commit that referenced this issue Apr 27, 2017
This allows custom directives to manipulate the select's and
ngModel's behavior based on the state of the unknown and
the empty option.

Closes #13172
Closes #10127
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants