I actually need to do a validation, which needs to check for a filed which is
correct_topic
wrong_topic
Now I am writing the rules like the following for other fields.
return [
'category' => 'required|numeric',
'text' => 'required',
'type' => 'required|numeric|in:1,2,3',
'vendor' => 'required|numeric|exists:users,id',
'topic' => 'required|array'
];
How can I improve this and include the above mentioned validation for topic
?
I am writing these rules in a TopicRequest
class. Basically I need to check whether the filed topic
is an array which has got at least 2 columns and not more than 4 and one of the array key should be correct-topic
and others needs to be wrong_topic_1
etc.
Is there any default validation rules that can be used effectively for this situation ?
You can create your own validator. Laravel Valditation
You can possibly fulfill above validation lists with first array
validation and using dot notation
validation style like the below:
return [
................
'topic' => 'required|array|min:2|max:4',
'topic.correct_topic' => 'required',
'topic.wrong_topic' => 'required'
];
Using the min and max according to the Laravel doc, uses the size rule with on an files fields which I believe should also be relevant for arrays , ie employs the count()
method. Ref min-here and max-here
PS: I can't be sure this really fix your issue particularly but I believe it should give an inspiration to the answer you need.
You can also refer to Validating Arrays if any doubts
Hope it helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.