I created a custom validation rule set in my Laravel application. First, I created the validators.php file, which is located in the App\Http directory:
function requireParameterCount($count, $parameters, $rule) { if (count($parameters) < $count): throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters."); endif; } $validator->extend('image_width_max', function ($attribute, $value, $parameters) { requireParameterCount(1, $parameters, 'image_width_max'); list($width, $height) = getimagesize($value); if ($width >= $parameters[0]): return false; endif; return true; });
Then I add, including this to my AppServiceProvider.php file (as well as adding use Illuminate\Validation\Factory; at the top of this file):
public function boot(Factory $validator) { require_once app_path('Http/validators.php'); }
Then, in my form request file, I can invoke a custom validation rule, for example:
$rules = [ 'image' => 'required|image|image_width:50,800', ];
Then, in the Laravel validation.php file located in the resources/lang/en directory, I add another key / value to the array to display an error message if the check returns false and fails, for example:
'image_width' => 'The :attribute width must be between :min and :max pixels.',
Everything works fine, it checks the image correctly, displays an error message if it fails, but I'm not sure how to replace :min and :max values โโdeclared in the form request file (50,800), in the same way :attribute is replaced with the name form fields. Therefore, it currently displays:
The image width must be between :min and :max pixels.
While I want it to display as follows
The image width must be between 50 and 800 pixels.
I saw some replace* functions in the main Validator.php file (vendor/laravel/framework/src/Illumiate/Validation/) , but I cannot figure out how to make it work with my own validation rule.