[Form] Add options "min" and "max" to NumberType #6732

Closed
romaricdrigon opened this Issue Jan 14, 2013 · 11 comments

Comments

Projects
None yet
9 participants

Hi,

At the moment, using the Range validation constraint, we can set up a min & max for a numeric value in an entity property.

Html5 offers min & max attributes for number inputs, so we can also enforce this client-side in any form:

$builder
    ->add('aNumber', 'number', array(
            'attr' => array('min' => 1, 'max' => 100)
    ))

It may be nice to offer min & max options for number field type. It seems pretty logical, as some other fields already have such attributes (ie max_length for text fields).

The syntax may be:

$builder
    ->add('aNumber', 'number', array(
            'min' => 1,
            'max' => 100
    ))

These attributes will be used client-side & the submitted object will be validated too.

What do you think?
I'm ok about contributing the code (& tests & doc), but I wanted the idea to be reviewed first.

Contributor

thewholelifetolearn commented Jan 14, 2013

+1
I think it is a good idea. The 2nd syntax is a good proposal. It would have the same architecture then "text" field.

Contributor

jameshalsall commented Jan 14, 2013

+1 for this idea, would be great as a convenience

Member

webmozart commented Feb 21, 2013

In fact max_length should be removed (see #7148) so I don't think that we should add new options. But we should indeed add functionality to guess the "min" and "max" attributes for the number type (in case Min, Max or Range is used). Marking this as feature request.

Member

stof commented Feb 21, 2013

@bschussek why shouldn't we try to guess them to improve the client side validation ? Some other client-side validation attributes are already guessed.

Member

webmozart commented Feb 21, 2013

Could it be you misread my post? :)

Member

stof commented Feb 21, 2013

hmm, indeed.

fnagel commented Aug 20, 2013

+1 subscribe

@stefanosala stefanosala pushed a commit to stefanosala/symfony that referenced this issue Dec 14, 2013

Stefano Sala [Form] Add min and max attributes to integer type
Fixes #6732
1016eb3

@stefanosala stefanosala pushed a commit to stefanosala/symfony that referenced this issue Mar 31, 2014

Stefano Sala [Form] Add min and max attributes to integer type
Fixes #6732
9ca9214
Contributor

vmattila commented Nov 8, 2014

This is definitely useful here (+1). Wouldn't it make sense to also change number field to render as HTML5 number instead of text? This also relates to ticket #11979 .

Contributor

hhamon commented Apr 19, 2015

+1

Member

Tobion commented Apr 19, 2015

What's the point of adding these options instead of using attr? All attributes that have no backend logic have actually been removed, e.g. max_length, pattern, read_only. So I'm rather -1 on doing the same thing with two different approaches.

Member

webmozart commented Jun 17, 2015

As I said, I'm also against adding options if we can also use the attributes. See #15014.

webmozart closed this Jun 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment