Skip to content
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

[Form] Show errors even without submit #32336

Open
iquito opened this issue Jul 3, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@iquito
Copy link
Contributor

commented Jul 3, 2019

Description

I have quite a few forms where the data in the form is highly "changeable" over time, which usually means that a previously valid value is not valid anymore (for example a product in the cart was available before, but now it is not available anymore). Currently, a form only shows its errors when it is submitted, but often this is not very user-friendly: if you can point out what needs to be adjusted in existing data right when somebody opens the form then the user knows right away what needs to change, and this kind of form validation would be the same whether the form was just opened or submitted.

My suggestion would therefore be:

  • Make it possible to show form errors without submission (could be enabled specifically, or enabled by default), as currently you can add errors to a form even when it was not submitted, but these errors are never shown in any way
  • Add a new event which is always executed any time the form is used and which is executed after POST_SUBMIT, making it possible to add custom validation & errors for both the initial form load or after a submit. A possible name could be PRE_VIEW.
  • An event like this would also open up an easier way to load additional data for the form only if some data in the form already exists (because PRE_SET_DATA and POST_SET_DATA do not take into account any submitted data and so possibly operate on "stale" data, and the SUBMIT events are only executed when the form was submitted), example: loading the product names/details for products in a cart, currently this kind of behavior has to happen outside of the form

This gives a developer more flexibility, as a form can point out some errors right when the form loads if, for example, it has existing data which can be checked and has to be changed. These changes would also be backwards-compatible, especially if the showing form errors without submission is opt-in.

I would be ready to help implement this kind of functionality, but first wanted to find out if a change like this would be welcome or not, or what the thoughts are behind the current behavior versus this proposed change.

@iquito

This comment has been minimized.

Copy link
Contributor Author

commented Jul 3, 2019

A slight amendment: With these changes it might make sense to also have an additional event after PRE_SUBMIT that is always run (so even without form submission the form could be changed / parts added or removed) in addition to the event after POST_SUBMIT that is always run.

That way a form can dynamically change according to the current form data (with submitted data taken into account if the form was submitted, or using preexisting data if not), so parts could be added or removed after PRE_SUBMIT, and additional always-run validation would be possible after POST_SUBMIT. In terms of how they work they could be named PRE_SUBMIT_ALWAYS and POST_SUBMIT_ALWAYS, although one might find better names for it.

I can give additional use cases about the advantages of these features if it remains unclear, or go more into specifics.

@BoShurik

This comment has been minimized.

Copy link
Contributor

commented Jul 3, 2019

Now you can show errors with this:

if ($form->isSubmitted()) {
    if ($form->isValid()) {
        // 
    }
} else {
    $form->submit(null, false);
}
@iquito

This comment has been minimized.

Copy link
Contributor Author

commented Jul 4, 2019

An explicit call to $form->submit as a "workaround" has the following drawbacks:

  • You cannot distinguish in the view if the form was actually submitted or not, as the form itself will always think it was submitted
  • All validations and errors will show up, even ones which cannot be validated without submission, for example the CSRF token
  • My main point was to be able to validate only certain aspects every time the form is loaded, because you rarely want to validate everything except when a form is actually submitted, it is more about being able to point out needed changes to existing data when the form loads which are not obvious to a user but which are obvious for the application
  • Being able to adapt and react to the fully assembled form data (either with submitted data, if it exists, or preexisting data, if no form exists) is currently only possible with duplication, for example by using two events like POST_SET_DATA and PRE_SUBMIT: use PRE_SUBMIT if the form was submitted or POST_SET_DATA if the form was not submitted

Two common use cases for my suggestions:

  • Updating an expired credit card used for payment, where you want to point out what has to be updated/what is not valid anymore right away. The same can happen with any data that can expire or change its requirements and where a user just needs to amend the parts which are now invalid or missing and not showing the needed changes would lead to a bad UI experience
  • Additional address fields during an order (invoice address different from shipping address) which are only shown if a user has clicked a checkbox saying "I want to supply a different invoice address", so in that case address fields for the invoice address are added (both on submit and on initial load). This kind of adhoc form changes can happen with all sorts of data as soon as one form field value leads to other forms being included or not, and the final form data (including submitted data if it exists) needs to be considered
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.