21

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?

1
  • English message 'This value should be a valid number' comes from IntegerType (and maybe other Types) and can be overriden with ['invalid_message' => 'String in German'] Commented Mar 24, 2022 at 12:03

6 Answers 6

28

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Assert\Type(type="integer") is not working. That was the first thing I tried.
Have you seen second part of answer? What form type are you using for this property?
This answer has been posted 2 years ago.
18

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))
     )
 ))

1 Comment

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.
13

If the field type should be a string, you can use this instead:

/**
 * @Assert\Type(type="digit")
 */

Although it is not mentioned in the documentation, TypeValidator also utilizes ctype_* functions.

See \Symfony\Component\Validator\Constraints\TypeValidator:

public function validate($value, Constraint $constraint)
{
  ...

  $ctypeFunction = 'ctype_'.$type;

  ...

  } elseif (function_exists($ctypeFunction) && call_user_func($ctypeFunction, $value)) {

  ...
}

1 Comment

Great, thank you! I've created a pull request to add this to the documentation.
6

You can use

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

or create a validator with ctype_digit.

2 Comments

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]*)$/")
Shorter version does not start with 0, then only digits are accepted@Assert\Regex(pattern="/^(?!0)\d+$/")
3

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.

Comments

0

I had to use number in form field type, but it showed an asterisk * next to the input label although it is not mandatory. So,I had to use 'required' => false as well. The form field type integer did not work. The underlying field data type is smallint.

->add('storey', 'number', array('required' => false))

The Regex constraint in YML is not working too while type is integer or nothing provided. I don't know why

storey:
    - Regex: '/^[0-9]+$/'

My Symfony version is 2.7.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.