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

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',

Related

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());

Necessity to verify if input is not empty string in 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...

Select specific field in MongoDB using ruby then pass the value to a variable

I'm working on a script that will return value in a specific field and exclude other fields i tried these codes:
name = 'bierc'
puts collection.find({"name"=> name},{:fields => { "_id" => 0}}).to_a
and
name = 'bierc'
collection.find("name" => name,"_id" => 0).each{|row| puts row.inspect}
These two returns
{"_id"=>BSON::ObjectId('55f0d965fcd4fe1c659cf472'), "name"=>"bierc", "song"=>"testsong"}
I want to select name only and exclude the song and especially the _id then will work on to pass the value of name field to a variable.
The option is not fields but projection. You do need _id => 0 but then 1 for any field or fields you do want to select should exclude the others.
collection.find("name" => name, projection: => { "_id" => 0, "name" => 1}})

Laravel Validator

I have the following rule in the validator:
'keywords' => 'array|required'
'date_intervals' => 'array|required'
The array needs to be populated with at minimum 1 element (should not be empty).
Is there an existing rule to achieve this, or is it required that I write a custom rule for it?
Does min:1 work with array validation?
Thanks.
No, you'd be better counting the array elements and then passing the count for validation.
$count = count(Input::get('keywords'));
$input = ['keywords' => Input::get('keywords'),'count' => $count];
$rules = ['keywords' => 'required|array', 'count' => 'min:1'];

CodeIgniter: Using array within array

I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.

Resources