I'm trying to validate a radio button in Laravel. This is my code, but it doesn't work.
In my case, i have a dynamic form with many questions with different of type such as : radio, checkbook, single input, number input,... So I have to use array name for each type of question. For example : name="radio['.$k.']".
In my controller i make validation and $key is the $k value in initial form.
public function rules()
{
$rules = [];
if (Input::has('radio')) {
foreach (Input::get('radio') as $key => $val) {
$rules['radio.' . $key] = 'required';
}
}
if (Input::has('singleinput')) {
foreach (Input::get('singleinput') as $key => $val) {
$rules['singleinput.'.$key] = 'required|max:10';
}
}
}
public function messages()
{
$messages = [];
if (Input::has('radio')) {
// some code here;
}
}
public function answer_store($survey_id, $patient_id)
{
$rule = $this->rules();
$message = $this->messages();
$validator = Validator::make(Input::all(), $rule, $message);
}
In the view:
<input type="radio" name="radio['.$k.']" value="'.$str1.'">'.$answer->answer_body
My code works with text input type but not with radio & checkbox. Anyone can help me?
$keyis otherwise you are just requiring what you already have which doesn't make any sense and a waste of time.