ArtSasin
ArtSasin

Reputation: 3

Symfony2 Form validation with Assert statement

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!

Upvotes: 0

Views: 517

Answers (2)

Marcel Burkhard
Marcel Burkhard

Reputation: 3523

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)

Upvotes: 2

StackOverflowUser
StackOverflowUser

Reputation: 125

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?

Upvotes: 0

Related Questions