title: Symfony5 Custom Validator in FormType - Validate password field when check box is on
body: <p>I would like to validate a input password field in a form based in the value of checkbox.  If the value is TRUE the password fields should be NOTBlank and Equal first to second.  This what i have so far:</p>
  
  <pre><code>namespace App\Form;
  
  use App\Entity\User;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolver;
  use Symfony\Component\Validator\Constraints\Email;
  use Symfony\Component\Validator\Constraints\NotBlank;
  use Symfony\Component\Validator\Constraints\Length;
  use Symfony\Component\Validator\Constraints\Callback;
  use Symfony\Component\Validator\Context\ExecutionContextInterface;
  use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  use Symfony\Component\Form\Extension\Core\Type\EmailType;
  
  
  class UsuarioFormType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
          -&gt;add('nombre',null,[
              'required'   =&gt; true,
              'constraints'=&gt; [
                  new NotBlank([
                      'message' =&gt; 'Escriba el nombre por favor',
                  ]),
              ]
          ])
          -&gt;add('apellido',null,[
              'required'   =&gt; true,
              'constraints'=&gt; [
                  new NotBlank([
                      'message' =&gt; 'Escriba el apellido por favor',
                  ]),
              ]
          ])
          -&gt;add('user_name',null,[
              'required'   =&gt; true,
              'empty_data' =&gt; '',
              'constraints'=&gt; [
                  new NotBlank([
                      'message' =&gt; 'Escriba el nombre de usuario por favor',
                  ]),
              ]
          ])
          -&gt;add('active', CheckboxType::class, [
              'required' =&gt; false
          ])
          -&gt;add('email',EmailType::class,[
              'constraints' =&gt; [
                  new Email([
                      'mode'=&gt; 'html5',
                      'message' =&gt; 'El correo electrónico {{ value }} no es un correo electrónico válido',
                  ]),
              ]
          ])        
          -&gt;add('plainPassword', RepeatedType::class, [
              'type' =&gt; PasswordType::class,
              'invalid_message' =&gt; 'Los campos de contraseña deben ser iguales',
              'mapped' =&gt; false,
              'required'   =&gt; false,
              'constraints' =&gt; [
                  new Length([
                      'min' =&gt; 6,
                      'minMessage' =&gt; 'Your password should be at least {{ limit }} characters',
                      // max length allowed by Symfony for security reasons
                      'max' =&gt; 4096,
                  ]),
              ],
          ]);
      }
  
      public function configureOptions(OptionsResolver $resolver)
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; User::class,
              'constraints' =&gt; [
                  new Callback([$this, 'validate']),
              ],
          ]);
      }
      public function validate($data, ExecutionContextInterface $context): void
      {
          if ($data['chk_passUpdate']){
              if (trim($data['plainPassword']['first']) == '' ) {
                  $context-&gt;buildViolation('El campo de contraseña no puede estar en blanco')
                  -&gt;atPath('plainPassword')
                  -&gt;addViolation();
              }
              if ($data['plainPassword']['first'] != $data['plainPassword']['second'] ) {
                  $context-&gt;buildViolation('Los campos de contraseña deben ser iguales')
                  -&gt;atPath('plainPassword')
                  -&gt;addViolation();
              }
          }
      }
  }
  </code></pre>
  
  <p>This code throw a Exception:</p>
  
  <p><em>Argument 1 passed to App\Form\UsuarioFormType::validate() must be of the type array, object given, called in C:\Csi\CsiWorkspace\SynfonyTest\SymfonyTest\vendor\symfony\validator\Constraints\CallbackValidator.php on line 46</em></p>