D
D
dmz92017-05-09 14:24:41
symfony
dmz9, 2017-05-09 14:24:41

Symfony 3 Validation - how to make mutually exclusive validations?

Used by Symfony/Validation as a separate component, the rest of the environment has no other Symfony components.
There is a form, in the form of a drop-down list. The default value is empty.
You need to check the incoming data for an empty value - return the error "Select a value from the list", or, if it is not empty - return the error "Value *** is not in the list".
Validation only on the value of the variable.
Design on the backend in the style of php code (not annotations, not xml / yaml).
Like this now

$errors = $validator->validate(
      $value,
      [
        new NotBlank(
          [
            'message' => 'Выберите значение из списка'
          ]
        ),
        new Choice(
          [
            'choices' => $valuesList,
            'message' => 'Значение {{ value }} отсутствует в списке'
          ]
        )
      ]
    );

In this form, both errors are given at once, which are confusing. It is necessary that either the first or the second is given.
On the backend, I want to make sure that the form validator class and all the information associated with it is in one place, i.e. in one file, so it is desirable not to have to write a dozen more metadata factories.
I don’t understand symphony, but I want to study it, but as I understand it, no one really understands even in the bourgeois Internet how to use this standalone monster without the rest of the framework.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2017-05-09
@prototype_denis

NotBlank is superfluous, unless of course there is an empty value in the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question