3

Is it possible to use my custom validation rule in a validation request file?

i want to use my custom rule called EmployeeMail here is the code of the request file

class CoachRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    $rules = [];

    if ($this->isMethod('post') ) {
        $rules = [
            'name' => 'required|string',
            'email' => 'required|email|employeemail', <<<--- this
            'till' => 'required|date_format:H:i|after:from',
        ];
    }

    //TODO fix this
    //TODO add custom messages for every field

    return $rules;
}
}

it gives me an error when i try to use it like this

Method [validateEmployeemail] does not exist.

code of custom rule

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class EmployeeMail implements Rule
{
/**
 * Create a new rule instance.
 *
 * @return void
 */
public function __construct()
{
    //
}

/**
 * Determine if the validation rule passes.
 *
 * @param  string  $attribute
 * @param  mixed  $value
 * @return bool
 */
public function passes($attribute, $value)
{
    // If mail is that of an employee and not a student pass it
    return preg_match("/@test.nl$/", $value) === 1;
}

/**
 * Get the validation error message.
 *
 * @return string
 */
public function message()
{
    return 'Email is geen werknemers mail';
}
}

can i only use this custom rule like this?

$items = $request->validate([
    'name' => [new FiveCharacters],
]);
3
  • 4
    It seems you are validating string with a regular expression, the same logic can be achieved by regex buit-in validation method. Check it out. laravel.com/docs/5.5/validation#rule-regex No need to create your own validation rule. Commented Dec 20, 2017 at 22:53
  • 4
    If you want to use your validation pass it into an array. like this. 'email' => ['required', 'email', new employeemail], Commented Dec 20, 2017 at 22:57
  • 4
    @RutvijKothari Ah thanks, i will use the regex rule instead then, i forgot about it. and also thanks for mentioning how i would use a custom rule in request file Commented Dec 20, 2017 at 23:09

1 Answer 1

4

Rutvij Kothari answered the question in the comments.

It seems you are validating string with a regular expression, the same logic can be achieved by regex buit-in validation method. Check it out. laravel.com/docs/5.5/validation#rule-regex No need to create your own validation rule. – Rutvij Kothari

If you want to use your validation pass it into an array. like this. 'email' => ['required', 'email', new employeemail]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.