S
S
symnoob2018-05-08 12:00:48
symfony
symnoob, 2018-05-08 12:00:48

Symfony 4 using Validator, how to find out which field the request came from?

Good afternoon forum!
Can anyone tell me how to find out from which field in the validator the request is being processed and how can I count the locale in the Validator?
Validator is built on this example:
symfony.com/doc/current/validation/custom_constrai...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Elena, 2018-05-12
@lenk0belk0

To solve your problem, it is better to use groups, as answered above. And the answer to your question:

/** @var ConstraintViolation[] $violations */
$violations = $validator->validate($entity);
foreach ($violations as $violation) {
    $property = $violation->getPropertyPath());
}

D
Denis, 2018-05-09
@prototype_denis

there are 3 fields, title_ru, title_de and title_en, if the site is currently working on locale==ru, then title_ru is required to fill in; if locale==en, then title_en is required to be filled in

Use validation groups.
One of the options will look something like this.
/**
 * @Assert\NotBlank(groups={"foo_en"})
 */
private $title_ru;

$builder->add('title_ru', null, ['validation_groups' => sprintf('foo_%s', $options['locale'])])

$resolver->setDefaults([
  'locale' => 'en'
]);

$this->createForm(AcmeFormType::class, $object, ['locale' => $request->get('_locale')]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question