-
Notifications
You must be signed in to change notification settings - Fork 291
Refactoring #160
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
base: master
Are you sure you want to change the base?
Refactoring #160
Conversation
…r. Added required attribute to each radio button
This relates to the issue i explained on Friday (#135) about completely removing the 'rejected' error/errors in the This demonstrates that point, as the 'rejected' error is applied to the http://plnkr.co/edit/9MqYF7WzZBUN1SJwLDpk?p=preview Problem When dealing with an error that relates to an
The docs state that the purpose of
So what's actually occurring here, is that the state of a non-existent control - 'rejected' - is being set to false. Thus causing the form to be invalid. Solution By manipulating the error state of the controls instead, you correctly set the In the plunker, if you follow these steps:
http://plnkr.co/edit/OxCv91wfscq1IrVXutoU?p=preview Now each individual control has it's own 'rejected' state set to false, which causes the 'rejected' error state of the Obviously this is not complete, as we need to remove all control 'rejected' errors when any single control is clicked. But i'll come back to that in a minute. |
Problem Using a http://plnkr.co/edit/Qjf1oLl80r0Fd1nJKBJz?p=preview
If you look at the console output, you'll see that the Solution Instead of adding a
if you follow the same steps as before you'll see that the 'rejected' error is now cleared http://plnkr.co/edit/SqhRvyDTg4ZR4Y7JJrCw?p=preview
Due to the way in which As they are all bound to the same model value on scope - We've now completely removed the need for the |
…tored djangoForms.setErrors to work with this
We can go one step further by moving the specific logic to add/remove 'rejected' errors into (namespaced) methods on each
So we're essentially creating an interface for adding and clearing the rejected messages. For example, the
Now the existing logic for
|
…jangoForms.setErrors and validate-multiple-fields to handle this
Finally, i've refactored the Also (going back to my earlier comment about calling I'm also wondering if this could be refactored further to completely remove the need for the Unless there are cases where a direct child |
That's everything. Sorry for the sligthly verbose explanation, i just wanted to be clear on why i've made the decisions i have when refactoring this logic. |
validate-multiple-fields
for RadioSelect
- change to handling of 'rejected' errors - change to validate-multiple-fields
handling of 'required' validation for CheckboxSelectMultiple
validate-multiple-fields
for RadioSelect
- change to handling of 'rejected' errors - change to validate-multiple-fields
handling of 'required' validation for CheckboxSelectMultiple
If you look at the initial console output of the plunker you can see what's happening
http://plnkr.co/edit/7tWnDtnXfss4v8rkvO0m?p=preview
As each radio button is added to the form, as they all share the same name, they replace one another. So the last radio button added is the one that's retrieved from the
formController
byform.cheese
.If you select each radio button in order, you can see a side effect of this, as only the last one ('maybe') sets the
ngModelController
to$dirty
.This issue is fixed by
validate-multiple-fields
, but i'd like to propose another solution that doesn't require that directive to handle aRadioSelect
.Solution
Firstly rather than using
validate-multiple-fields
to handle a 'required' group of radio buttons, you can just use therequired
attribute on each radio button. As they all bind to the sameng-model
value, as soon as one is selected and theng-model
value becomes populated, every buttons 'required' validation is satisfied and becomestrue
.http://plnkr.co/edit/w0tUBsuxXSNqAkPhNkaB?p=preview
Then to remove the
$dirty
/$pristine
issue, you can wrap the elements in anngForm
of the same name, so that all view/error state is bound to that instead, which is then updated by each input.http://plnkr.co/edit/WmezNdFd4SU5x44A1uoZ?p=preview
As each radio buttons
ngModel
is bound to the same property on scope, the resulting data structure still matches the data structure expected by the server.