vikiedr
vikiedr

Reputation: 19

Laravel Request Validation Regex without Required

I have a problem with Request validation. Everything works but one pattern. And its if I dont want the field to be required but once it is filled I want it to match a regex.

But it throws regex error when I leave the field empty.

Any tips on how I should handle it? BTW: I made a custom Request class where I take care of the validation so if the solution could be also in the Request and not directly in Controller that would be great.

return [
    'dic' => 'max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];

Upvotes: 2

Views: 3932

Answers (1)

Jan Wytze
Jan Wytze

Reputation: 3497

return [
    'dic' => 'nullable|max:12|regex:/^[a-zA-Z]{2}[0-9]{8}[0-9]*$/',
];

nullable won't check the other rules when the field is empty.

Upvotes: 6

Related Questions