Laravel 4 - Input::old for radio buttons

2020-07-10 09:11发布

I was wondering if some Laravel guys can help out.

I have a form in which i have 2 radio buttons, when the form submits it goes through the validator, if the validator fails it comes back to the form, populates the fields with the input and displays error messages.

I cant seem to do this for radio buttons, if one is clicked when the form is submitted and there was an error, it comes back to the form with everything filled out EXCEPT the radio button that was checked is now empty.

My radio buttons are as follows:

<input type="radio" name="genre" value="M" class="radio" id="male" />
<input type="radio" name="genre" value="F" class="radio" id="female" />
<span class="error">{{ $errors->first('genre') }}</span>

Any help would be greatly appreciated.

7条回答
在下西门庆
2楼-- · 2020-07-10 10:00

Using with the Bootstrap & Automatic checking

Add this code at end of file: app/start/global.php

//...

Form::macro('radio2', function($group ='group-name', $value_model = 'value-model', $label ='label-radio', $value_radio = 'value-radio', $attrs = array())
{

  $item  = "<div class='radio'>";
  $item .=   "<label>";
  $item .=      Form::radio($group, $value_radio, ($value_model == $value_radio) ? true : false, $attrs);
  $item .=      $label;
  $item .=   "</label>";
  $item .= "</div>";

  return $item;
});

In your view.php

{{ Form::radio2('status', Input::old('status'), 'Online', '1', array()) }}
{{ Form::radio2('status', Input::old('status'), 'Offline', '0', array()) }}

Final result:

enter image description here

查看更多
登录 后发表回答