Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

automaticko's avatar

Handling not displayed validation errors

I'm currently running a Laravel 5.4 project which has lots of different forms. Form Request Validations are in place for each. Every form displays its validation errors under each of its fields.

From time to time, I am requested to remove/rename a field. The problem I face arises if the form request validation rules for that field aren't removed accordingly. The form validation fails because of these rules and the form is displayed again with no tip of what went wrong.

In order to handle this situation, I thought to Load an extended version of Illuminate\Validation\ValidationServiceProvider This CustomValidationServiceProvider, would be calling a CustomValidator class with an overwrote passes method that would instantiate a CustomMessageBag that would

  • Overwrite method get, maintain the existing behavior and add a way keep track of which fields have been called via the get method.
  • Create a method to get fields that weren't accessed via the get method.

After this, I would be implementing an after middleware for every get route (probably only for forms routes), that would check if the errors variable isn't empty and there are any fields which haven't been got, which translates to displayed in this context and act accordingly.

All this seems a lot of trouble in order to handle these "forgotten" validation rules.

The question is, do you guys can point me to someone that handled this situation before and shared its way, or, you can hint me on how to accomplish this without all the classes extensions.

Edit: I understand and agree with some of the comments stating that tests would be the proper way to handle this, but that's an option I don't have at the moment. I need to have a way to know if a validation error wasn't displayed despite the fact that this may not be a recommended approach.

0 likes
8 replies
Cronix's avatar

From time to time, I am requested to remove/rename a field. The problem I face arises if the form request validation rules for that field aren't removed accordingly. The form validation fails because of these rules and the form is displayed again with no tip of what went wrong.

Isn't this what unit/integration testing is for? Write tests for your forms/validation. If you change some code that breaks the validation, you'll know immediately when running your test suite.

automaticko's avatar

Yes, thanks. But again, I could "forget" to update these tests, or have no time to create one for a new form.

I need something to help me remember there is a problem.

Cronix's avatar

So you're ok with having more code run on every single request that a user makes, slowing things down a bit, so you don't forget something in development? This "test" should only be run in development, not on a production server...

It sounds like you just need a better checklist/procedure. This stuff shouldn't really be done with code deployed to a production server.

automaticko's avatar

Thank you Cronix, I appreciate your feedback.

There are many things that don't depend on me, tests are one of these. In any case, if it was my decision and I was to decide that I don't want tests, I would still need a way to solve this.

Again, I appreciate you are trying to improve my development but is not what I'm asking. We can have a discussion on the topic you aim, in another thread or via pm if it can be done, but please, do not continue with this discussion in this thread.

Vilfago's avatar

From time to time, I am requested to remove/rename a field. The problem I face arises if the form request validation rules for that field aren't removed accordingly. The form validation fails because of these rules and the form is displayed again with no tip of what went wrong.

Despite I agree with Cronix, you can add sometimes to your rules. https://laravel.com/docs/5.6/validation#conditionally-adding-rules

automaticko's avatar

Thank you Vilfago. That could work but at the same time, I would be open to field name typos possibly making these field to be taken as "not present" for the validation rule.

Btw, I agree with Cronix as well.

Vilfago's avatar

I'm really not sure of what you want to achieve...

so last idea, working with $errors->all(), so you're sure that you have all errors if your validation.

Please or to participate in this conversation.