insin / insin/newforms

Adding an error to a specific field in cross-field cleaning leads to unexpected behaviour

Open
#76 0 comments 0 reactions 0 assignees View on GitHub
Defect
Dominant language
JavaScript
Stars
634
Forks
46
PR merge metrics
No merged PRs in 30d

Description

Example:

``` javascript
var SignupForm = forms.Form.extend({
password: forms.CharField({widget: forms.PasswordInput}),
confirm: forms.CharField({widget: forms.PasswordInput})

clean: ['password', 'confirm', function() {
var {password, confirm} = this.cleanedData
if (password && confirm && password != confirm) {
this.addError('confirm', 'Does not match the entered password.')
}
}]
})
```

If you type a password, type a mis-matching confirmation of it, then change the password to match the confirmation, `onChange` validation will not clear the error which has been added to the confirm field, as `addError()` removed its data from `cleanedData` and the user hasn't interacted with the field again.

You could use `form.data` instead to mitigate this, but then you can't make use of the type coerced value which would have been present in `cleanedData` for things like data and numeric fields, e.g:

``` javascript
var ReleaseForm = LayoutForm.extend({
startDate: forms.DateField(),
endDate: forms.DateField(),

clean: ['startDate', 'endDate', function() {
var {startDate, endDate} = this.cleanedData
if (startDate && endDate && startDate > endDate) {
this.addError('endDate', 'Cannot be prior to Start Date.')
}
}]
})
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.