1

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?

2
  • When validating, you need to know what the $key is otherwise you are just requiring what you already have which doesn't make any sense and a waste of time. Commented Jun 3, 2015 at 17:40
  • @user3158900: In my case, i have a form with many questions with different of type such as : radio, checkbook, single input, number input,... So I have 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. Anw, I updated my question more clearly. Commented Jun 4, 2015 at 6:04

3 Answers 3

1

Okay with no reply, this is my answer if you are using Laravel 5.

Laravel 5 uses requests when you submit a form. You can perform validation on that data before it executes your controller.

Firstly use your terminal to run a artisan command

php artisan make:request MyRequest

This will create a file in App\Http\Requests.

Put this in the new request file

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class MyRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'myRadios' => 'required'
        ];
    }

}

In your view, have radios like the following, ensuring the group of radios you want all have the same name.

<input type="radio" name="myRadios" value="1"> Number 1
<input type="radio" name="myRadios" value="2"> Number 2
<input type="radio" name="myRadios" value="3"> Number 3

In your Controller you will need to reference the request file and put it into your function using dependency injection.

When you want to use the value of the radio that was selected, you use the $request array

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Http\Requests\MyRequest;

class MyController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function myFormFunction(MyRequest $request)
    {
        //Get the value of the radio selected
        $myVariable = $request['myRadios'];
    }

}

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but i think my solution also using 'required' rule, same with you, and it works in case of text input validation. I will try your solution also. I'm using Laravel 5.
I found my problem here. Using if (Input::has('radio')) { } in the rule code. Because if i don't select anything, don't have any information related radio to validate.
1

After one day, I found the solution for my problem.

Using if (Input::has('radio')) { } in the rule code. Because if i don't select anything, don't have any information related radio to validate.

For array of radio questions I use:

<input type="radio" name="radio['.$count_radio.']" value="'.$str1.'">

My problem is i don't know how many radio question each form. I have many different form. I will count number of radio form in View file and pass to Controll ($count_radio).

In controller, i make rule like this:

for ($key = 1; $key <= $count_radio; $key++) {
            $rules['radio.' . $key] = 'required';
        }

I also add error with this code in View:

       if($errors->has('radio.'.$count_radio.'')) {
            echo $errors->first('radio.'.$count_radio.'');
       }

That's all.

Comments

0

You have the input tag ending in </label>. Not sure if that will help.

3 Comments

Thanks, but i don't know what you mean? I can do validation with <input type="text" ..
@Giang I think he's saying you're missing the closing / in your input tag: <input ... />, but that is a markup thing, not what is causing your issue.
What version of Laravel are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.