0

I, i have to add an Assert to an atribute when other atribute is equal than something. Like this:

/**
* @Assert\Callback(methods={"isChildMinor",)
*/
class PatientData
{
/**
 * @Assert\Date()
 */
public $birthday;

public $role;

public function isChildMinor(ExecutionContext $context)
{
    if ($this->role == 3 && check @assert\isMinor() to $birtday) {
    =>add violation
    }
}

so, i want check if the patient is minor (with assert or somethings else) if the role is equal than 3. How do this?

1 Answer 1

2

There are several ways to do, what you want.

1) You could make it right in the form. Like that:

use Symfony\Component\Validator\Constraints as Assert;

public function buildForm(FormBuilderInterface $builder, array $options)
{
  $yourEntity = $builder->getData();
  //here you start the field, you want to validate
  $fieldOptions = [
     'label'     => 'Field Name',
      'required' => true,
  ];
  if ($yourEntity->getYourProperty != 'bla-bla-bla') {
    $fieldOptions[] = 'constraints' => [
       new Assert\NotBlank([
          'message' => 'This is unforgivable! Fill the field with "bla-bla-bla" right now!',
       ]),
    ],
  }
  $builder->add('myField', TextType::class, $fieldOptions);

2) Other way - is to make your custom validation callback in your Entity and play with direct asserts there. It's possible, I think.

3) But the optimal way, from my point of view - is to use several asserts with validation groups. You need to specify Assert\isMinor(groups={"myCustomGroup"}) on birthday field. And then, in your form:

public function configureOptions(OptionsResolver $resolver)
{
  $resolver->setDefaults([
     'validation_groups' => function (FormInterface $form) {
        $yourEntity = $form->getData();
        if ($yourEntity->role !== 3) {
            return ['Default', 'myCustomGroup'];
        }
        return ['Default'];
     },

Hope this'll be helpful for you.

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

2 Comments

Hi, i'm trying to implement this, I have one problem: the things that i don't understand is how you obtain the validation to return as 'myCustomGruop'. I specify the assert Minor like this: /** * @Assert\Date() * @Assert\GreaterThan("-18 years") */ public $birthday; so i don't know how put in that return array.
i understand with this symfony.com/doc/current/components/options_resolver.html and the group validation and now all works fine. Thanks for the help

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.