Giter Site home page Giter Site logo

Comments (5)

WebMamba avatar WebMamba commented on August 20, 2024

Sorry I don't get what you are trying to do here... What error are you trying to catch ? Form errors happen when the value submitted by the user are not valid but no errors are throw the errors are just added to the form. If the error came from outside the form it's a good idea to have an another props for the error outside the form.

from ux.

Mauriceu avatar Mauriceu commented on August 20, 2024

The Idea is that apart from FormErrors being added by Constraints, FormErrors would also be added manually by the Component.

For example, a User submits some data and this data - if valid from a constraint perspective - is then sent to a third party which validates it against some internal constraints and saves it. If this third party rejects the data, or an error occurs in transit, or while saving it, I would like the Form to reflect that by manually adding a FormError.

To expand on the example above:

     #[LiveAction]
     public function save(): ?Response
     {
          /** @var SomeModel $model */
          $model = $this->getForm()->getData();
          try {
               $this->someService->save($model);
          } catch (\Throwable $e) {

               // Possible error cases would be:
               // An error occurred during transit
               // Third party server is not available

               // manually add an error to the form which would then be rendered by the 
               // "form_errors(form)" twig function
               $error = new FormError('An error occurred while saving your data');
               $this->form->addError($error);
          }
     }

If this is currently not possible is it worth thinking about opening a feature request? Otherwise, the FormInterface::addError() function would be obsolete within LiveComponents.

from ux.

WebMamba avatar WebMamba commented on August 20, 2024

Ok now I got it now, thanks! You can't add Errors after your form have been submitted and validated. This not about LiveComponent, but more about how Symfony form works. So I think you have two options:

  • You can create a custom constraint that do the check with your third party, and then add this custom constraint to your form (https://symfony.com/doc/current/validation/custom_constraint.html)
  • You choose to validate your data after the form is submitted, but then you need to store the error message on a new propertie of your component and not on the form

from ux.

Mauriceu avatar Mauriceu commented on August 20, 2024

Yea I thought about these too, however they would still feel like a workaround, because even when adding the FormError to the form within the "instantiateForm()" function it is not rendered - and at this point the form is not submitted.

from ux.

smnandre avatar smnandre commented on August 20, 2024

Only errors related to validated field / models are kept (see: https://github.com/symfony/ux-live-component/blob/2.x/src/ComponentWithFormTrait.php), and the live props are the form values, not the form itself.

Not sure at all, but maybe you can call resetForm and then add some error ?

from ux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.