halloei
9 months ago

Cover validation from server errors

Posted 9 months ago by halloei

When I want to validate a string, my rule could be

'foo' => ['required', 'starts_with:bar']

But if some bad boy tries to insert an array into foo, an ErrorException would be thrown (which leads to a http 500).

So we extend the rule:

'foo' => ['required', 'string', 'starts_with:bar']

That will still throw an ErrorException, because the validation won't stop after the failed string rule.

So we need to add bail:

'foo' => ['bail', 'required', 'string', 'starts_with:bar']

That will work.

But wait: My table column is varchar(255):

'foo' => ['bail', 'required', 'string', 'starts_with:bar', 'max:255']

I think these are a plenty of rules to be sure that no server error occurs. Did you all - unlike me - think about this and got all these rules? Is there a way to make Laravel stop after the first rule fails, so we can omit bail?

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