I don't know why it is made not to validate empty strings, but I will give you an example.
I have a Model's update method overloaded and have a Validator in it.
The problem is that if I set "required" for the required fields, then I can't call $model->update() without them present in the input data, which is unacceptable.
On the other hand, if I remove the "required", then Laravels lets me write an empty string in the DB.
And this behavior can be fixed currently only by overloading the Validator class.
So, is there a specific reason not to validate empty strings?
Had another use with this problem the other day. I'm working on a solution.
Added sometimes short-cut for this: http://laravel.com/docs/validation#conditionally-adding-rules
@taylorotwell @barakuda28 How would you do it if all you care about is for the ''key'' to be set in the request and you don't care about the actual value. I essentially want to make sure the key is set but also want to allow empty string so ''sometimes|require'' doesn't work for me either.
I tried to create a custom validator the issue is it doesn't get called if I don't have required rule.
Essentially I have an API that allows POSTING empty string to clear the field but the field is required either value or no value.
@taylorotwell It seems you cannot create this type of validation rule (on L5): "the (current) field is required if another field is greater than a value" (for example I need that a user enters a string if another field is greater than 0, otherwise is an optional field and he can leave it blank). As the current field is an empty string, the custom rule is not ever called even if you use "sometimes" parameter.
How can I create this type of rule on L5?
@tranceptor available on 5.1 , tested :+1:
To the original issue posted, how to make an item required on CREATE but only validated if included on an UPDATE.
Your POST request validation has the required rule and your UPDATE validation request has the filled rule.
To future Googlers: you can use the string validation rule to accept strings that may be empty.
EDIT: this may have changed since 5.4 because I don't think it works any more.
I my case i dont want any empty string.
exists, nullable -> allow empty string (weird because it doesnt match the rules)
required, exists, nullable -> disallow empty string (required and nullable is weird too)
What finally worked for me in Laravel 5.1 was
'sometimes|required'
Doesn't work in Laravel 5.5
@rotaercz try nullable.
how about present|string?
Greate ! present|string works for me!
present
The field under validation must be present in the input data but can be empty.
Added
sometimesshort-cut for this: http://laravel.com/docs/validation#conditionally-adding-rules
Awsome
@rotaercz try
nullable.
Superb Thanks
Having ConvertEmptyStringsToNull on, sometimes|required worked for me.
Cause when the field is left out from the API request, I don't want it validated, cause I want to ignore that field completely in that case.
Most helpful comment
how about
present|string?