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

Marwelln's avatar

How do you setup a custom validation rule in 4.3?

In my rules I have a custom rule, my_custom_rule. Now when it tries to validate I get Method [validateMycustomRule] does not exist.. I tried adding a method in my FormRequest class, public function validateMyCustomRule() {}, but it didn't help.

Where are you supposed to add custom validations?

0 likes
1 reply
bashy's avatar

You can extend the validator like so

Validator::extend('alphanum_dash_space', function($attribute, $value, $parameters)
  {
   return preg_match("/^[a-zA-Z0-9 -]+$/u", $value);
  });

Then in the rules, just add the new name 'somefield' => 'alphanum_dash_space|requried

Please or to participate in this conversation.