Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all validation constraints in child class properties

Tags:

symfony

I have problem with clearing all validation constraint that extend from super class. Below is my code

User.php

  * @var string
  * @ORM\Column(type="text", unique=true)
  * @Assert\NotBlank()
  * @Assert\NotNull()
  * @AdminAssert\MyCustomValidation
  */
 protected $phoneNumber;

In Admin.php I wrote code something like below

class Admin extends User

  * @var string
  * @ORM\Column(type="text", unique=true)
  */
 protected $phoneNumber;

I want to remove all validation constraints but can't remove it.

like image 912
vibol Avatar asked Jun 11 '16 02:06

vibol


1 Answers

For disabling the validation of a form you can set the validation_groups option to false, as described here in the doc.

In your case you can check the class data (as described here in the doc) for disabling or not the form validation, as example:

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => function (FormInterface $form) {
            $data = $form->getData();

            if ($data instanceof Admin) {
                return;
            }

            return array('Default');
        },
    ));
}

Hovenever in your code i see a a custom validation on admin validation, if so consider to use the validation groups.

Hope this help

like image 87
Matteo Avatar answered Nov 07 '22 11:11

Matteo