Skip to content

[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

Conversation

Modelizer
Copy link

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

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.

@Modelizer Modelizer changed the title Exposing constraint error messages via method. [Validator] Exposing constraint error messages. Feb 16, 2021
@Nyholm
Copy link
Member

Nyholm commented Feb 16, 2021

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

@Modelizer
Copy link
Author

Modelizer commented Feb 16, 2021

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 getErrorNames to create rule {"IS_BLANK_ERROR": "This value should not be blank."}

Many Thanks!

@chalasr chalasr added this to the 5.x milestone Feb 16, 2021
@derrabus derrabus changed the title [Validator] Exposing constraint error messages. [Validator] Exposing constraint error messages Feb 16, 2021
@fabpot
Copy link
Member

fabpot commented Oct 30, 2021

@Nyholm Can we take a decision about this PR?

@fabpot fabpot modified the milestones: 5.4, 6.1 Nov 16, 2021
@fabpot fabpot modified the milestones: 6.1, 6.2 May 20, 2022
@wouterj
Copy link
Member

wouterj commented Jul 21, 2022

Hi @Modelizer!

I'm a bit unsure how you're using the rules, as there is no indication on what to validate. If the front-end is calling an API to validate a form, it'll be able to use Constraint::getErrorMessage() to transform the code into a human-readable message AFAICS.

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 :)

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh
Copy link
Member

xabbuh commented Dec 22, 2023

closing here as the PR seems to be stalled and @wouterj's concerns have not been answered

@xabbuh xabbuh closed this Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants