dimanche 3 décembre 2017

Laravel Validation custom message does not work

I create a custom validation in AppServiceProvider:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Validator::extend('greaterEqual', function($attribute, $value, $parameters, $validator) {
            $min_field = $parameters[0];
            $data = $validator->getData();
            $min_value = $data[$min_field];
            return $value >= $min_value;
        });

        Validator::replacer('greaterEqual', function($message, $attribute, $rule, $parameters) {

            return str_replace(':field', $parameters[0], $message);
        });
    }

    public function register()
    {
        //
    }
}

I want to pass custom validation message but not worked and after submit form i receive this message: validation.greater_equal.

This is my validation in controller:

$this->validate($request,[
  'quantity' => 'required|min:1|integer|greaterEqual:sale_quantity',
  'sale_quantity' => 'required|min:0|integer',
],[
  'quantity.greaterEqual' => 'The :attribute number is invalid'
]);

What is my mistake?!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire