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

form with FloadField required=false passes validation with wrong value #83

Open
limpbrains opened this issue Apr 15, 2015 · 4 comments
Open

Comments

@limpbrains
Copy link

I have a form with

amount : forms.FloatField({label: 'Amount', required: false, maxValue: 1}),

When I input there randome string, like 'asdfasdf'
form.validate() returns true, form.cleanedData returns {amount: null}

When I input a big number, like 123 it throws me validating error "Ensure this value is less than or equal to 1."

Is it a correct behaviour ?

@insin
Copy link
Owner

insin commented Apr 16, 2015

How are you getting user input into the form?

Having null in cleaned data is consistent with providing an "empty" value for a required field, which indicates that 'asdfasdf' might not actually have been set as input data when the form was validated:

> var TestForm= forms.Form.extend({
...   amount: forms.FloatField({label: 'Amount', required: false, maxValue: 1})
... })
> var f = new TestForm({data: {amount: ''}})
> f.validate()
true
> f.cleanedData
{ amount: null }
> var f = new TestForm({data: {amount: 'asdfasdf'}})
> f.validate()
false
> f.cleanedData
{}
> f.errors().asText()
* amount
  * Enter a number.

The behaviour of maxValue is correct - if you wanted 123 to be a valid input, did you mean minValue instead?

@limpbrains
Copy link
Author

I've prepeared a jsfiddle with this error https://jsfiddle.net/23Lmvcc7/
try to enter some text into field, I'm recieving "This field is required" error insted of "Enter a number"
Maybe newforms-bootstrap is the problem ?

@insin
Copy link
Owner

insin commented Apr 17, 2015

Looks like this is due to this field using an <input type="number"> by default - according to the HTML5 spec, its value must be set to an empty string when invalid, so newforms never sees the non-numeric input.

If the value of the element is not a valid floating-point number, then set it to the empty string instead.

@limpbrains
Copy link
Author

You are right. I've fixed my issue by moving _onSubmit function from button onClick to form onSubmit.
Now browser validates my field and I cant submit form with wrong values.

Maybe I'm wrong, but I remember developers of angularjs have fixed this problem. You can use number field with novalidate form and have a nice angularjs validating error.

PS
Thanks for the great lib.

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

No branches or pull requests

2 participants