2

I know this isn't standard, but I wanted to see if there's a way to do it in a simple flexible way.

My question is similar to Validation of a form before submission except I will also be working with entities that can be edited.

I have an entities that are tied together to create one cart/order/etc. I want to be able to highlight all the fields that are required for the final cart to be submitted. However, I want the user to be able to save partial information in between. I.E., the fields aren't actually required right then and there until the end process.

For example, let's say we're working with tax information across multiple forms or something else that's complex. For validation purposes, I might require certain fields to not be blank or have callbacks constraints depending on where their sources of income are from or something. At the same time, that's a lot of information to sit down and process at once if someone had to manually type everything in. (Pretend there are no services I can hit to prepopulate information.) So being able to save partial information even though it isn't valid yet to finish the whole process would be beneficial.

So if the user comes back after a couple days or two weeks, I'd like to visually point to exactly what they still need to fill out based on whatever information I have on hand in my hypothetical example.

The forms themselves are made in symfony2 and are mapped to entities. The entities would have a mix of validation classes. Some as annotations on the individual fields and others as custom callbacks. I was thinking of having the form builders do something weird such as

  • Inject the validator into the forms. As I build each field, validate the property for each mapped value against a non-Default validation group. Add an attribute or label attribute if it's not valid.
  • Inject a validator into a listener. Loop through the mapped values of the form and then remove and add them back with a modified attr/label_attr option because options can only be set when adding the field.*

By modifying attr or label_attr, my thought is I could either add classes or custom attributes to target with css. Visually it would be represented by different color borders on the fields or asterisks appearing after the labels. And in this scenario, the required option would be false for the most part because I want to enable saving part of the entity as I described earlier.

I can't go with the answer from Validation of a form before submission is that if loaded up an entity for editing but submitted blank information, it would load up the form with blank information.

*I'm running into a problem with this approach where custom form types are adding an 'input' option. When I add form fields back with a copy of the old options, I have to remove this 'input' option first to avoid an UndefinedOptionsException and actually add the field. Which seems odd to my limited knowledge of Symfony forms.

Hopefully the premise makes sense. If anyone else has any solutions that are flexible and robust I'd love to hear them. Thanks!

Community
  • 1
  • 1
Mike
  • 21
  • 2

0 Answers0