Validation an integer input from a form

37,621

Solution 1

You should use:

@Assert\Type(type="integer")

But be careful, you should use it with an IntegerType, not a NumberType or a TextType:

Symfony\Component\Form\Extension\Core\Type\IntegerType 

IntegerType is identical to NumberType except that it integrates the proper data transformer.

Solution 2

This works for me:

 ->add('field_name', 'integer', array(
     'label' => 'Your label here', 
     'data' => 0, // default value
     'precision' => 0, // disallow floats
     'constraints' => array(
         new Assert\NotBlank(), 
         new Assert\Type('integer'), 
         new Assert\Regex(array(
             'pattern' => '/^[0-9]\d*$/',
             'message' => 'Please use only positive numbers.'
             )
         ),
         new Assert\Length(array('max' => 2))
     )
 ))

Solution 3

You can use

/**
 * @Assert\Regex(pattern="/\d+/")
 */

or create a validator with ctype_digit.

Solution 4

Starting from Symfony v2.3 version you should use:

/**
 * @Assert\Type(type="integer")
 * @Assert\GreaterThan(0)
 */

But keep in mind that forms field type should be integer (IntegerType::class), otherwise i'll get negative validation.

Share:
37,621
sprain
Author by

sprain

http://www.sprain.ch

Updated on July 09, 2022

Comments

  • sprain
    sprain almost 2 years

    I have an entity with several fields in it. One of them is being validated after form submission as follows:

    /**
     * @var integer $anzahl
     *
     * @ORM\Column(name="anzahl", type="integer")
     * @Assert\NotBlank(message="Bitte geben Sie eine Kistenanzahl an.")
     * @Assert\Type(type="numeric", message="Die Kistenanzahl muss eine Zahl sein.")
     * @Assert\Min(limit="1", message="Sie müssen mindestens eine Kiste suchen oder anbieten.")
     */
    private $anzahl;
    

    I am having two problems with this solution:

    Only integer values higher than zero should be accepted. However also floats/doubles are being accepted by this validation. However, if I change @Assert\Type(type="numeric") to @Assert\Type(type="integer") no input is validated as true. How can I validate my input to be an integer value?

    The other problem is, after entering an obviously invalid value (like a string of letters) I receive not only my German error message for Type validation but also the English message 'This value should be a valid number'. Where does this message come from and how can I get rid of it?

  • sprain
    sprain almost 12 years
    @Assert\Type(type="integer") is not working. That was the first thing I tried.
  • Paweł Jędrzejewski
    Paweł Jędrzejewski almost 12 years
    Have you seen second part of answer? What form type are you using for this property?
  • insertusernamehere
    insertusernamehere over 11 years
    As this is a solution that accepts numbers even using text as the field type, it has the downside that it accepts values like "123ABC". Maybe this pattern would be more useful, as it also does not accept values starting with leading zeros: * @Assert\Regex(pattern="/^([1-9][0-9]*)$/")
  • Tony Knibb
    Tony Knibb over 9 years
  • Paweł Jędrzejewski
    Paweł Jędrzejewski over 9 years
    This answer has been posted 2 years ago.
  • Stphane
    Stphane almost 8 years
    Shorter version does not start with 0, then only digits are accepted@Assert\Regex(pattern="/^(?!0)\d+$/")
  • Bhavin Thummar
    Bhavin Thummar about 6 years
    In my case regex is working very well but Type function integer is not working. This is wondering for me because i have checked all the code i have already added use Type in my code.
  • Bhavin Thummar
    Bhavin Thummar about 6 years
    Hello Tony In my case regex is working very well but Type function integer is not working. This is wondering for me because i have checked all the code i have already added use Type in my code.