vendredi 12 octobre 2018

Laravel : Group existing validation rules in single custom Rule?

Is it possible to use existing Laravel validation rules in custom rule?

Example : required|string|max:100 I just want to group these rules in one rule custom_rule_name.

Either this way

Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
        return $value == 'required|string|max:100';//something like this
    });

or

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Uppercase implements Rule
{
/**
 * Determine if the validation rule passes.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function passes($attribute, $value)
{
    return $value == 'required|string|max:100';//something like this
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return 'The :attribute must be uppercase.';
}
}

Let me know, if there is any possibility?

Thanks,

Kaleem



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire