-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Exposing constraint error messages #40210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Validator] Exposing constraint error messages #40210
Conversation
Hey. Thank you for this PR. Could you elaborate on your scenario? I think I had the same issue but I manage to solve it. $violations = $this->validator->validate($message, null, $groups);
if (0 === \count($violations)) {
// No errors
return;
}
$violationMessages = [];
/** @var ConstraintViolationInterface $v */
foreach ($violations as $v) {
$violationMessages[] = \sprintf('%s: %s', $v->getPropertyPath(), (string) $v->getMessage());
}
// Send $violationMessages to the client |
Hey @Nyholm ! Thanks for the reply. We want to build a from without doing the validation. We want to use Constraints and attach it to the fields. Then add these fields to the form and convert it into array. This is the sample code: $form = new \MyFormBuilder\Form('/offers');
$form->addFields(
(new TextField('name'))->addRules(new NotBlank())->setLabel('Name')->setPlaceholder('Name'),
);
return $form->toArray(); This should produce this: {
"method": "POST",
"return-url": "\/offers",
"fields": [
{
"type": "text",
"name": "name",
"label": "Name",
"placeholder": "Name",
"rules": [
{
"IS_BLANK_ERROR": "This value should not be blank."
}
]
},
],
} Please note I will use Many Thanks! |
@Nyholm Can we take a decision about this PR? |
Hi @Modelizer! I'm a bit unsure how you're using the If this is useful for a front-end form builder (like https://jsonforms.io/), the change makes sense to me. But I don't see how it is useful at the moment :) |
closing here as the PR seems to be stalled and @wouterj's concerns have not been answered |
While building dynamic forms and sending it via APIs. It is preventing us to send the constraints inside the form. Adding a getter will help us to retrieve all the available validation messages of the Constraint.