Gabotronix
5 months ago

Laravel Form Request custom error message for regex rule

Posted 5 months ago by Gabotronix

Hi everybody, I'm trying to display a custom validation error message for my regex inside Form Request, I haven't succeded so far.

This is my Form Request logic, regex rule only accepts letters and numbers.

<?php

namespace App\Http\Requests\discounts;

use Illuminate\Foundation\Http\FormRequest;

class CheckoutCode 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()
    {
        return [
            'code' => 'required|string|min:7|max:7|regex:/([A-Za-z0-9 ])+/',
        ];
    }


    public function messages()
    {
        return [
            'code.regex' => 'Estas jodido'
        ];
    }


}

Please sign in or create an account to participate in this conversation.