0

I can't validate my form in Symfony2 with Assert statement at Entity property.

This my Entity:

use Symfony\Component\Validator\Constraints as Assert;

    class AqquiringRequestData
    {
        /**
         * @Assert\IsTrue(message="Необходимо принять условия")
         * @Assert\NotNull(message="Необходимо принять условия")
         */
        public $checkacc;

This one my form builder:

public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('checkacc', 'checkbox', array(
                'label' => 'I agree',
                'required'  => false,               
                'validation_groups' => array('Default'),
        ));
    }

This one is controller action:

            $formData = new AqquiringRequestData();

            $flow = $this->get('cib.form.flow.aqquiringreq');
            $flow->bind($formData);
            $form = $flow->createForm();

            if ($flow->isValid($form)) {
                   ...

And when i try to submit form with unchecked checkbox it's successfully submitted. What I'm doing wrong?

Thanks!

3
  • Is your bundle using yml configuration by any chance? Commented Jul 16, 2015 at 12:01
  • Yes, i'm using yml config files in my bundle Commented Jul 16, 2015 at 12:27
  • I think answered your question now, based on that comment Commented Jul 16, 2015 at 12:35

2 Answers 2

2

Option 1: enable annotations for validation

Validation > Configuration

The annotation reader for validation is not enabled by default. You can read in the link above how to change that.

Option 2: use yaml for validation rules

You're bundle is using the yml configuration format, thus I advise you to write down your validation rules in the yml format too.

This will look something like this:

# src/AppBundle/Resources/config/validation.yml
AppBundle\Entity\AqquiringRequestData:
    properties:
        checkacc:
            - NotNull: ~

Read this: http://symfony.com/doc/current/book/validation.html (Note: examples have multiple tabs for the different configuration formats)

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

2 Comments

Thanks Marcel! Your answer was helpful and solved my problem!
Thanks for your feedback, I'm glad I could be of help.
0

Shouldn't you handle the Request by either $form->handleRequest($request) or $form->bind($formData).

$formData = new AqquiringRequestData();

$flow = $this->get('cib.form.flow.aqquiringreq');
$form = $flow->createForm();
$form->handleRequest($formData); // Your form needs to be associated with an entity some how

if ($form->isValid($form)) { // You are validating the form, right?

1 Comment

No, i am using CraueFormFlowBundle to create multistep form and it's used exactly like i wrote in my question (based on bundle author's example).

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.