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

Robstar's avatar
Level 50

Validate Validation Rules

So, I have a client that's been reading about Laravel validation rules.

They currently have a settings screen that manages a very basic store of keys and values. i.e. "max free samples" = 10, "login attempts" = 5 etc.

They came up with a pretty interesting idea.

For each setting, they'd like to use any of the Laravel built in rules and type these in themselves. Now, when they create a setting they'd enter the key, value and populate a rules field. i.e.

Key = Max of something
Value = 100
Rules = required|integer|max:500

I've already implemented this and everything works. However, at the moment it's very dependent upon the client following the Larvel Docs. If they were to enter in a invalid rules a BadMethodCall exception is thrown - which due to the fact I'm using form requests and the way the Validation instance is bound to the ioc, isn't immediately obvious how I can catch this.

To save some time, is anyone aware of a good package that can validate the validation rules. i.e. required|email would be valid, whereas requireddd|asijdha would of course be invalid. There is also the issues of rules with parameters like between:10,20.

Thanks

0 likes
9 replies
jlrdw's avatar

I've already implemented this and everything works.

Where did you place your code? This is pretty neat.

Snapey's avatar

I think I would be tempted to use the validation rules themselves when they save the rule.

They will enter a string into a form field.You could also accept a sample correct input that they would need to fill in and submit with the rule.

Then when accepting this data, you could validate their correct input with their rule, but do this in a try catch block. This way if the validation blows up then you can tell them there is something wrong with the rule. You might not be able to say what is wrong with it but you can catch it before it is put to use.

Robstar's avatar
Level 50

Within the FormRequest:

if ($this->has('rules')) {
      $rules['value'] = $this->get('rules')
}

The value field is the the setting value.

It's very flaky as it's entirely reliant upon the user entering in valid, validation rules. I don't trust the client :)

jlrdw's avatar

@Robstar like the three programming rules:

  • Rule 1 Never trust user input
  • Rule 2 Never trust user input
  • Rule 3 Never trust user input
Robstar's avatar
Level 50

@Snapey If you follow the validation service provider & look for the validateAttribute function, it's not immediately obvious how I'd catch the exception thrown.

There isn't really the time to implement a UI for this - maybe in the next round of changes.

Robstar's avatar
Level 50

@Snapey I can do this and catch the error:

try {
    $this->validate(request(), [
        'value' => 'email|kjdsafhkjsahdfk|string'
     ]);
} catch (\BadMethodCallException $ex) {
    // Bad rules string
}

However, that only works in the controller and not in my form request class as I'd like. It's certainly a start as it stops my app from blowing up :)

Snapey's avatar

perhaps if you catch it then throw your own exception which you can then handle in the exceptions class?

The problem with doing it in the form request is how you return the error to the user.

Robstar's avatar
Level 50

I'm over thinking it probably. For the time being I'll just catch the exception without using form requests. There must be a way to do this though :/

Please or to participate in this conversation.