2

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]*$/',
];
1
  • You may make the pattern match an empty string regex:/^(?:[a-zA-Z]{2}[0-9]{8}[0-9]*)?$/ Commented Nov 6, 2017 at 16:58

1 Answer 1

6
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.

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

1 Comment

I tried to upvote your answer but it says this "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Sorry :/

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.