Necessity to verify if input is not empty string in laravel? - laravel

Is it necessary to verify if input has type string and it is not empty in Laravel's validator?
Example:
$v = Validator::make(['myinput' => $request->input('myinput')], ['myinput' => 'integer|min:1']);
if (!$v->fails()) {
$someModel->integer_field = $request->input('myinput');
}
Field is not required, and if user supplies this POST request
'field' => ''
Then Mysql complains about empty string that can not be assigned to integer field.
Also if I provide, for example, empty array as input
'field' => []
Then it fails too.
So my question is: how to write robust validators for NOT required fields (e.g., if field is present, then it must be:
1. For integers: 1,2,3,4,... (+ null value if column is nullable),
so any other supplied value will not pass
(like boolean true which can be converted to 1 automatically but should not)
)
2. For string: '', 'example', ... (+ null value if column is nullable)
Here is example https://github.com/laravel/framework/issues/10747
But sometimes|required|string still passes for empty array...

Related

Require a field if the value exists in another field, which is an array

Say a model has a status property, that holds an array of strings. If this array contains a string name "Other", the status_other field should be required. I can achieve this with the following rules:
'status' => 'nullable|array',
'status_other' => Rule::requiredIf(in_array('Other', $this->model->status))
Is there a way to write the status_other rule as a string? I tried:
'status_other' => 'required_if:status,in:Other',
and
'status_other' => 'required_if:status,Other',
Both that did not work.
You can compare it by using '==' to match the string in the array.
'status_other' => 'required_if:status,==,Other',

How to check for null in other field laravel FormRequest?

I am trying to exclude a field from being validated when another field has a value (not null). In this case, i want 'deleted_pictures' to be excluded if product_pictures is an array (not null). My problem is that i think exclude_unless:product_pictures,null evaluates null as a string, and not as, well, null.
This is my rule.
ProductRequest.php
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,null', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless product_pictures has a value of 'null'
I tried this to confirm my suspicion and it works like it should.
//test_field = 'some_value'
return [
'test_field' => 'required|string'
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:test_field,some_value', 'required', new cant_delete_all($max,'1')],
];
read : exclude deleted_pictures unless test_field has a value of 'some_value'
In my first case, deleted_pictures is excluded because it doesn't detect that product_pictures is 'null' (string)
While on the second case, deleted_pictures is NOT excluded because test_field matches the given value.
My question is, how do you evaluate null value in FormRequest Laravel?
So apparently you can just leave the second parameter blank to evaluate it as null
return [
'product_pictures.*' => 'required_with:product_pictures|file|max:2048|mimes:jpeg,jpg,png',
'product_pictures' => 'sometimes|array',
'deleted_pictures' => ['exclude_unless:product_pictures,', 'required', new cant_delete_all($max,'1')],
];
I'm not sure if this is how its supposed to be done or intended behavior. But im just gonna leave this answer just in case someone might need it. If someone can suggest the 'proper' way of doing it then I'll accept that instead.

Any way to except empty field from $request->all() in laravel?

I want to except empty value field from $request->all();
Array ( [first_name] => Dev 1 [password] => [last_name] => [phone] => 123456 [password_confirmation] => )
I got the array like this but I want to except field from above array like last_name, password which has no value.
Any way to do this without for loop.
I mean laravel provide any default method for that ?
Thanks
array_filter will remove empty elements:
$filtered = array_filter($request->all());

create method in store function stores array value in db

$array = array($t, $c, $s);
foreach ($array as $a) {
receipt::create($a);
}
i want to pass the array content to database but i got this error
"Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given,
Actually while insert we should provide the array data even single field or more no of fields.
For example two fields : receipt::create($a). In this $a should contain two values in array format.
New example updated below as per your request. In this your $a variable should be like below format
$a = array(
'user_id' => $userId,
'receipt_description' => $desc
);
$receiptId = receipt::create($a)->id;

sfValidatorRegex not working

I am trying validate a username field on a register form so that a username can only be made up of letters. However, when I use the sfValidatorRegex(), it always returns invalid ("johnny" will return invalid, as does "JoHnNy"). Here's the code I'm using:
// From RegisterForm.class.php
$this->validatorSchema['username'] = new sfValidatorRegex(
array(
'pattern' => '[A-Za-z]',
),
array(
'invalid' => 'only lowercase letters',
)
);
What am I missing?
Didn't understand whether you wanted lower or uppercase but try:
Only lowercase: '/^[a-z]*$/'
Lower or uppercase: '/^[A-z]*$/i'

Resources