Does anyone know how to validate nested input sets using the Laravel 4 validator?
I have a table with a set of line items, and each quantity field has the unique package id in square brackets in the name:
<input type="text" name="quantity[package_one]" />
<input type="text" name="quantity[package_two]" />
This results in a nested input array:
<?php
array(
'quantity' => array(
'package_one' => 3,
'package_two' => 12
)
);
Which is exactly what i want, but i'm unsure how to specify rules for these items using the validator class:
// Does not work :-(
Validator::make(Input::all(), array(
'quantity[package_one]' => 'numeric|required|min:1',
'quantity[package_two]' => 'numeric|required|min:1'
));
This does not seem to work, neither does nesting the rules under quantity. Obviously there are workarounds to this like building a custom array of input yourself before passing it to the validator etc, but what i'd like to know is:
is there a native, "Laravel" way of handling nested input like this?
Thanks in advance,
Dan.
The dirty way....
Controller
$input = Input::all();
$input = array_merge($input, $input['quantity']);
Validator::make(Input::all(), array(
'package_one' => 'numeric|required|min:1',
'package_two' => 'numeric|required|min:1'
));
Validator does not look into nested arrays. just bring that array to the outer one and you are done. (you can unset $input['quntity'] if you want after that)
loophole here is, it assumes that $input['quantity'] is present in the array. You need to validate this before putting into validation.
Works but not efficient.
You could make an extra Validator object just for the quantity input. It requires more code but i think it's more robust than merging in to one array
Related
So I am using CakePHP and I need to POST array of values. How it should be done? Either by creating input for every element or one input with all elelents. First one is fine with the convention, but pumping DOM with dozens of inputfields feels counter-productive.
So witch approach is considered as good practice?
foreach($tms['deletable'] as $tm){
echo $this->Form->control("ids[]", [
"type" => "hidden",
"value" => $tm->id,
]);
}
Or
echo $this->Form->control("ids", [
"type" => "hidden",
"value" => implode(",",collection($tms['deletable'])->extract("id")->toList())
]);
I'd say if you don't plan to modify the hidden field values in JavaScript, then go with the one field.
On the other hand, it requires you to do special handling on the receiving side (exploding), so use multiple fields.
A dozen fields are nothing if you look at the bloated HTML pages these days.
In the Laravel Docs on validation they speak of 'nested parameters':
If your HTTP request contains "nested" parameters, you may specify them in your validation rules using "dot" syntax:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
What would the HTML look like for this nesting? I googled around, and found nothing except things about form nesting. Also, the "dot" syntax, is this specific to Laravel?
The dot notation is for easily accessing array elements, and making their selectors more "fluent".
Validating author.name would be the equivalent of having checking the value of the input <input type="text" name="author[name]" />.
This makes having multi model forms or grouping related data much nicer =). You can then get all the data for that thing by doing something like $request->request('author'); and that would give you the collection/array of all the values submitted with author[*]. Laravel also uses it with its config accessors - so config.setting.parameter is the equivalent of config[setting][parameter]
Basically makes working with array data easier.
See https://github.com/glopezdetorre/dot-notation-access for some examples!
The Html form will look like Nothing More
So, okay i tried a lot of rules from validation docs but all give me same error saying
Array to string conversion
Here is how I add the array:
$this->validate($request,[
'employee' => 'required|in:'.$employee->pluck('id')->toArray(),
],[
'employee.in' => 'employee does not exists',
]);
Any hint on how to achieve this?
i created a custom validator but still passing array seems to be not possible
Implode the array as a string and join it on commas.
'employee' => 'required|in:'.$employee->implode('id', ', '),
This will make the correct comma separated string that the validator expects when making an in comparison.
Edit
This still works, but is not the Laravelesque way of doing it anymore. See the answer by #nielsiano.
Update: You are now able to use the Rule class instead of imploding values yourself as described in the correct answer. Simply do:
['someProperty' => ['required', Rule::in(['needed', 'stuff'])]];
As mentioned in the 'validating arrays' section in the documentation: https://laravel.com/docs/5.6/validation#validating-arrays
Normally, you can use code such as this to validate incoming data:
$post = Validate::factory($_POST);
$post->rules('field_name', array(
'not_empty' => null, // Must be non-empty.
'regex' => '/\d+/' // Must be a digit.
));
[ Yes, I know to move field validation rules to a Model ]
In this particular case, I'd like to only validate certain fields only if they were actually present in the $_POST data.
Is this accomplished by simply leaving out the not_empty rule, or will the regex rule (in this case) still not be satisfied, and fail validation? (my assumption).
This looks like it might solve the issue (haven't tried yet) but I wonder if this is the best practice in this case, or if this would be "the right way"...
$post->rules( /*... some rules I always want... */ );
if (isset($_POST['field_name'])) {
$post->rules( /* ... conditionally applied rules ... */ );
}
What's the Kohana way?
not_empty will only make sure the value isn't empty (empty array, null, false, '' - more like !empty() as a function, not as a lang. construct).
You can do it this way manually (as you proposed), although a callback seems like a more valid solution in your case (which would check for your conditions, does it really have to be $_POST, maybe you want to check those keys / conditions for other arrays as well?).
I am building an HTML form with a set of checkboxes for selecting multiple categories using this format:
<input type="checkbox" name="category[]" />
So, when I post and print_r($_POST) to view the variable and values I get:
Array
(
[27] => on
[28] => on
[29] => on
)
Once I run $this->form_validation->run(); the categories array becomes "Array" as a string. I believe I have narrowed it down to "prep_for_form" function in the system/libraries/Form_validation.php file, but it seems like the recursive function is working correctly.
Thank you in advance.
I figured it out. When using the $this->form_validation->set_rules() method, in the validation rules (third parameter) I set it to trim|required. I guess the trim function treats the actual Array like a string "Array" and trims the word. I simply removed the "trim" rule from my validation rules. e.g.
$this->form_validation->set_rules('category', 'categories', 'trim|required');
// when parsing an array (set of checkboxes, radio buttons, etc.)
// - remove the "trim" validation
$this->form_validation->set_rules('category', 'categories', 'required');
I hope others find this useful.
That solution did not work for me, but there is a fix listed here that did: http://ellislab.com/forums/viewthread/156497/