Laravel 4 - Input :: old for switches

I was wondering if some of the Laravel guys could help.

I have a form in which I have 2 switches, when the form submits it through the validator, if the validator does not work, it returns to the form, fills in the fields with the input, and displays error messages.

I can’t do this for the switches, if I click on the button when submitting the form and an error occurs, it returns to the form with all EXCEPT filled in, the switch that has been checked is now empty.

My switches:

<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.

+6
source share
7 answers

You can do it:

 <input type="radio" name="genre" value="M" class="radio" id="male" <?php if(Input::old('genre')== "M") { echo 'checked="checked"'; } ?> > <input type="radio" name="genre" value="F" class="radio" id="female" <?php if(Input::old('genre')== "F") { echo 'checked="checked"; } ?> > 
+12
source

You can try this using Laravel out of the HTML radio box ... Laravel Docs Form Flags and Formats

Using a blade,

 {{ Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio')) }} {{ Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio')) }} 

Or just php,

 echo Form::radio('genre', 'M', (Input::old('genre') == 'M'), array('id'=>'male', 'class'=>'radio')); echo Form::radio('genre', 'F', (Input::old('genre') == 'F'), array('id'=>'female', 'class'=>'radio')); 
+19
source
+1
source

I just stumbled upon this and I don’t want to repeat such conditions for each form, so I created a function in the Helper class.

helper.php:

 class Helper { // other functions public static function oldRadio($name, $value, $default = false) { if(empty($name) || empty($value) || !is_bool($default)) return ''; if(null !== Input::old($name)) { if(Input::old($name) == $value) { return 'checked'; } else { return ''; } } else { if($default) { return 'checked'; } else { return ''; } } // Or, short version: return null !== Input::old($name) ? (Input::old($name) == $value ? 'checked' : '') : ($default ? 'checked' : ''); } } 

So now in my forms I just use it like this:

 <label>Please select whatever you want</label> <div class="radio-inline"><label><input type="radio" name="whatever" value="1" required {{ Helper::oldRadio('whatever', '1', true) }}> One</label></div> <div class="radio-inline"><label><input type="radio" name="whatever" value="2" {{ Helper::oldRadio('whatever', '2') }}> Two</label></div> <div class="radio-inline"><label><input type="radio" name="whatever" value="3" {{ Helper::oldRadio('whatever', '3') }}> Three</label></div> 

Each option passes its name and value to the auxiliary function, and the previously selected one will print 'checked'. In addition, the option can pass "true" as the third parameter, so it is selected if the old input was not.

+1
source

Usage with Bootstrap and Auto Check

Add this code at the end of the 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

+1
source

I tried just using Input :: get () instead of Input :: old () .... and it works !! {{Form::radio('estado','V',(Input::get('estado')=="V"))}}

0
source

My approach is a nested abbreviated if / else statement with blade syntax. This solution also takes into account the initial value set in the database.

 <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="sex" id="male" value="male"{!! ((old('sex') == 'male') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'male') ? ' checked="checked"' : '')) !!}/> <label class="form-check-label" for="male">Männlich</label> </div> <div class="form-check form-check-inline"> <input class="form-check-input" type="radio" name="sex" id="female" value="female"{!! ((old('sex') == 'female') ? ' checked="checked"' : ((empty(old('sex')) && $user->sex == 'female') ? ' checked="checked"' : '')) !!}/> <label class="form-check-label" for="female">Weiblich</label> </div> 

Tested with Laravel 5.7.

0
source

Source: https://habr.com/ru/post/947080/


All Articles