Answer the question
In order to leave comments, you need to log in
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
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());
}
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
/**
* @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 questionAsk a Question
731 491 924 answers to any question