Could you be more specific please? Like I said, I have looked at the docs but did not found the solution there. I've looked at it once more when I have saw your message but still did not found it.
Ok sorry, was my mistake, is an array well you access to the fields and make the next steps something like
//to validate field has something
if(!empty(products.quiantity))
{
//you can try to parse it
$newval=(int)$val;
//if it is not numeric well the variable will return 0 and you can catch it
}
I don't know if you can validate arrays with rules maybe you can but that rules are to validate the inputs and well I think that the validator method in the docs validate an object if parse or change the array to an object could be easily
or maybe this could be functional like this
$validator = Validator::make(
array('quantity' => products.quantity),
array('quantity' => 'required|min:1'|numeric')
);
I am not really sure I understand what you mean. Yes the first solution should work but it would be terrible from maintenance point of view. I want to keep rules at one place so when things change I know where I have to go and what to change (preferably just one change not multiple repeated).
The second solution I don't get at all. How could products.quantity)
this work? Maybe you have a typo and it should be something like 'products.quantity'
but again if I understand it correctly than it would require to have multiple changes so it's more or less similar to the first one. Or maybe I just misunderstood what you were talking about. I don't know how could that work but I'll try it maybe I just don't get it at all.
Anyway I've tried something and found that if I make the rules like this:
'products.1.product_id' => 'required|exists:products,id',
'products.1.quantity' => 'required|numeric|min:1',
than it passes. So the problem is that laravel is looking at it under array index. Is there some way I could give it some kind of wildcard or something like that so it would look at values under products -> any number -> product_id
or something like that?
I imagine that maybe I could create some helper method that would create these rules in a loop from actual posted values but again that is not really a good solution since it would require me to change multiple places to tweak single feature.
Also I am not too crazy about the fact that it would display the error messages like this The products.1.quantity field is xyz
but maybe it could be changed in a messages for validator (hopefully in some clean way).
In case somebody is interested in what solution I ended up doing I did it like this.
public static $productIdValidationRuleTemplate = 'required|exists:products,id';
public static $productQuantityValidationRuleTemplate = 'required|numeric|min:1';
public static function generateArrayRules($numberOfProducts) {
foreach(range(1, $numberOfProducts) as $index) {
static::$rules['products.'.$index.'.product_id'] = static::$productIdValidationRuleTemplate;
static::$rules['products.'.$index.'.quantity'] = static::$productQuantityValidationRuleTemplate;
}
return static::$rules;
}
Than in my controller I get rules like this:
Order::generateArrayRules($numberOfProducts);
instead of
Order::$rules;
I will probably push this down to the BaseModel
and maybe even create some static function like getRules that would call array rule generation with $numberOfProducts
being optional parameter. I will have to think about it but right now I have bigger problems than this...
There is an each() method on the $validator object but it doesn't seem to play well with an indexed array, only associative arrays.
Using the mergeRules method that is available on the $validator object I did the following:
foreach($input['my_field'] as $key => $value) {
foreach ($value as $valueKey => $valueValue) {
$validator->mergeRules("my_field.$key.$valueKey", 'required');
}
}
This will basically create a rule for each item in your array in the following format:
'my_field.0.name' => 'required',
'my_field.0.email' => 'email',
'my_field.1.name' => 'required',
'my_field.1.email' => 'email',
My friend, here is solution.
https://laracasts.com/discuss/channels/general-discussion/how-to-validate-array-fields?page=2
Here
https://ericlbarnes.com/2015/04/04/laravel-array-validation/
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community