Search code examples
phplaravellaravel-5laravel-validation

Custom placeholders for custom validation rules in Laravel 5


I've created a set of custom validation rules in my Laravel application. I first created a validators.php file which is in the App\Http directory:

/**
 * Require a certain number of parameters to be present.
 *
 * @param  int     $count
 * @param  array   $parameters
 * @param  string  $rule
 * @return void
 * @throws \InvalidArgumentException
 */

    function requireParameterCount($count, $parameters, $rule) {

        if (count($parameters) < $count):
            throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters.");
        endif;

    }


/**
 * Validate the width of an image is less than the maximum value.
 *
 * @param  string  $attribute
 * @param  mixed   $value
 * @param  array   $parameters
 * @return bool
 */

    $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;

    });

I'm then adding including this in my AppServiceProvider.php file (while also 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 call the custom validation rule, like so:

$rules = [
    'image' => 'required|image|image_width:50,800',
];

Then in the Laravel validation.php file located in the resources/lang/en directory, I'm adding another key/value to the array to display an error message if the validation returns false and fails, like so:

'image_width' => 'The :attribute width must be between :min and :max pixels.',

Everything works fine, it checks the image correctly, displays the error message if it fails, but I'm not sure how to replace :min and :max with the values declared in the form request file (50,800), the same way :attribute is replaced with the forms field name. So currently it displays:

The image width must be between :min and :max pixels.

Whereas I want it to display like this

The image width must be between 50 and 800 pixels.

I've seen some replace* functions in the master Validator.php file (vendor/laravel/framework/src/Illumiate/Validation/), but I can't quite seem to figure out how to get it to work with my own custom validation rule.


Solution

  • I haven't used it this way but you can probably use:

    $validator->replacer('image_width_max',
        function ($message, $attribute, $rule, $parameters) {
            return str_replace([':min', ':max'], [$parameters[0], $parameters[1]], $message);
        });