0

I have a form containing a field that expects an integer. This is the field definition in the form type entity definition:

/**
 * @ORM\Column(type="integer", nullable=true)
 * @Assert\Type(type="integer", message="Number of pieces must be a number.")
 * @Assert\GreaterThanOrEqual(value=1, message="Number of pieces cannot be lower than 1.")
 */
protected $numberOfPiecesSent;

The relevant form builder looks like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('numberOfPiecesSent', 'integer', array('label' => 'Number of pieces sent:', 'required' => false));
}

When I submit the form with a non-numerical value in the field (say, 'aaa'), it just saves the form and leaves the field numberOfPiecesSent NULL in the database instead of failing on validation. I also tried to make the field non-NULL, but that didn't help. Any ideas why this isn't working, please?

3
  • You can try regex Commented Jun 28, 2015 at 18:56
  • Surprisingly not even the Regex works. This is very strange because the GreaterThanOrEqual() assert works, so in general, my asserts do work. Commented Jun 28, 2015 at 20:29
  • Checkout the answer below. Commented Jun 28, 2015 at 20:43

2 Answers 2

2

I've just tested and this works fine. You can add constraints into your property below. You can remove NotBlank as well. Modify as you wish.

entity

/**
 * @ORM\Column(type="integer", nullable=true)
 */
protected $numberOfPiecesSent;

form

use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;

->add(
    'numberOfPiecesSent',
    'integer',
    [
        'constraints' => [
            new NotBlank(
                [
                    'message' => 'The numberOfPiecesSent is required.'
                ]
            ),
            new Range(
                [
                    'min' => 1,
                    'minMessage' => "The numberOfPiecesSent must contain at least {{ limit }}"
                ]
            )
        ]
    ]
)

UPDATE

use Symfony\Component\Validator\Constraints\Regex;

->add(
    'name',
    'text',
    [
        'constraints' => [
            new Regex(
                [
                    'pattern' => "/^[0-9]+$/"
                ]
            )
        ]
    ]
)

OR

use Symfony\Component\Validator\Constraints\Regex;

->add(
    'name',
    'integer',
    [
        'constraints' => [
            new Regex(
                [
                    'pattern' => "/^[0-9]+$/"
                ]
            )
        ]
    ]
)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I just tried this. It only works if I use the NotBlank constraint, which I don't want to use because I need the field to be optional, so this workaround isn't a solution for me. I am really wondering why the Assert\Type thing doesn't work. I might end up writing my own validator, but I'd prefer to use built-in features.
Thank you. So this actually works for you? I just tried this and it doesn't do anything. When I submit 'aaa' as the value of the field, it just saves is as NULL. The field also has 'required' => false set on it. I am on Symfony 2.7.
Aah, it works with the widget type set to 'text' instead of 'integer'. Makes sense. I wish it worked with the Assert\Type solution. I am glad this works. I am going to upvote this because this is a good workaround, but I'll wait a few days before accepting it in case someone knows how to get the Assert\Type working, which is the subject of the original question. Thanks a lot for your help!
That's fine. Glad that it provides some sort of solution.
0

Have you tried to make field not null with: @ORM\Column(type="integer", nullable=false) or did you also tried with the assert NotNull ? http://symfony.com/fr/doc/current/reference/constraints/NotNull.html

2 Comments

I tried @ORM\Column(type="integer", nullable=false) - that didn't help. I didn't try the NotNull thing because I actually need the field to be optional. I just need ensure that if any value is entered in it, it has to be a number.
Peut-on voir ton controller également ?

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.