-
Notifications
You must be signed in to change notification settings - Fork 439
select2 is never marked valid #6
Comments
Interesting, guess we'll have to do $attrs.$observe('class') or something. The problem is that it's easy to simply ADD all the classes, but $observe doesn't really give us a good way to check the delta (to remove classes). I don't know a good way to resolve this issue. |
Checking for those particular classes is also not a good idea, since ideally you'd want all angular classes to be kept in sync. Notice how |
I know it's a ugly solution, but I did this modification in the directive we are using here in my job: [...]
[...] |
Mind opening a pull request? Also, don't pass a reference to the DOM element, I would just access it directly. And remember you can chain the removeClass().addClass() etc so I wouldn't bother even creating functions. |
You mean, like this?
Really better actually... I have forget to return the value too, otherwise it breaks the update of the model value |
@DiegoPires that works for me, thanks. |
Please open a pull request instead of pasting it into an issue
|
Sure @ProLoser, I've posted just to make sure it was a good solutions, now I just need to learn how to create the pull request... |
Hi, I just downloaded the source code, which includes this fix. I still get a dropdown that is dirty and invalid when the form opens. This is my directive:
And this is my controller:
Best regards, |
Hi, This example below works. <select ui-select2>
<option value=""></option>
<option value="1">one</option>
</select> But when dynamically generating a combobox with select2 by using ng-repeat or the query-function in the select2-options, the form validiation is broken --> the $dirty-flag is true just after initialization. Example: <select ui-select2>
<option value=""></option>
<option ng-repeat="item in items" value="item.value">{{item.text}}</option>
</select> Can anybody help me? Thanks |
Hi, I have the same problem as @anagelcg. Using ngRepeat, the select field is set to dirty after initialization. Despite, unlike @martijnsenden, the form is valid. Here is a Plunker: http://plnkr.co/edit/DwzccaqEJQDzZx7auqUD?p=preview Maybe this issue could be re-opened? Thanks. |
Same. |
I tried changing angular-ui-select2.js to be have the following at around line 140:
This seems to work, though it requires the unstable version of AngularJS. |
I've the same problem. Form was set to dirty, but valid. |
This line appears to be the culprit. It's recommended to make sure We've patched ours like so: if (watch) {
scope.$watch(watch, function (newVal, oldVal, scope) {
if (angular.equals(newVal, oldVal)) return;
// Delayed so that the options have time to be rendered
$timeout(function () {
elm.select2('val', controller.$viewValue);
// Refresh angular to remove the superfluous option
elm.trigger('change');
});
});
} |
MattWalker, great patch! Thanks! |
@MattWalker, thanks for the patch! 👍 |
At our project it seems the Edit: It does break, as I was afraid. Will look into a possible solution as @MattWalker's solution does not work for me. |
Hi, I have tried everything you guys mentioned here, and I'm still having the same problem. @anzeo: any news about you solution? Thanks, Nadavst. |
@nadavst I ended up writing a mock implementation of the $setPristine method introduced in angular 1.1.1. |
So there's no solution to this still? |
Still no solution for this? |
Ok, it seems the form is marked invalid because it also takes into consideration the loading of the select elements. And because initially the select is empty and then it is loaded with the defined options, the select will always be marked dirty by default. The "dirty" means the select has been loaded with options. I fixed this by changing line 120 from this: So basically I just check to see if the old value has not been defined before (because the select was empty). I have not yet tested all possible scenarios, but for me it works with this change and I am pretty confident the dirty is set because the select is loaded with the available options. |
http://plnkr.co/edit/w1TW1j
It's correctly marked as ng-invalid and ng-invalid-required, but when you choose an option the classes aren't updated. Note that if you inspect the select element which select2 hides, that one's classes are updated correctly.
(Brought over from angular-ui/angular-ui-OLDREPO#531, updated the plunker, same issue is still there.)
The text was updated successfully, but these errors were encountered: