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'
];
}
}
You can achieve that with an existing Laravel Validation Rule: Alpha num. From the documentation:
alpha_num
The field under validation must be entirely alpha-numeric characters.
So your validation can be just this:
// ...
public function rules()
{
return [
'code' => 'required|string|min:7|max:7|alpha_num',
];
}
// ...