2013-06-12 27 views
6

我在想,如果一些Laravel人可以幫忙。Laravel 4 - 輸入::老單選按鈕

我有一個窗體,其中有2個單選按鈕,當表單提交它通過驗證程序時,如果驗證程序失敗它將返回到窗體,填充輸入字段並顯示錯誤消息。

對於單選按鈕,我似乎無法做到這一點,如果在提交表單時出現單擊按鈕,並且出現錯誤,它將返回表單,其中所有內容都填寫完畢,除非被選中的單選按鈕現在爲空。

我的單選按鈕如下:

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

任何幫助將不勝感激。

回答

12

你可以這樣做:

<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"; } ?> > 
+0

嘿,那會檢查男性單選按鈕默認雖然不會呢? – BigJobbies

+1

是的,你可以切換它,讓女性默認。但是如果你想要的話,你可以在兩個輸入字段中輸入一個if語句。 – JasonMortonNZ

+1

您是否不希望默認選擇任何無線電輸入?我更新了我的代碼,因此默認情況下不會檢查輸入。 – JasonMortonNZ

19

你可以試試這個使用Laravel的開箱即用HTML無線電... Laravel Docs Form checkboxes and Radio

使用刀片,

{{ 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')) }} 

或者只是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')); 
+0

我看到這個'方法無線電不存在'。當我使用這 – BigJobbies

+0

它似乎並沒有出現問題的代碼是使用刀片模板或laravel HTML /表單助手 – JasonMortonNZ

+0

糟糕...只是建議..因爲他提到laravel ..乾杯 – Melvin

1

我剛偶然到這一點,我不想讓每個表單上重複這樣的條件,所以我創建了我的Helper類的功能。

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' : ''); 
    } 
} 

所以,現在在我的形式,我只是用這樣的:

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

每個選項都經過它的名稱和值的輔助功能,前面選擇一個將打印「檢查」。此外,所以如果沒有舊的輸入被選中的選項可以通過「真實」作爲第三個參數。

1

與引導&自動檢查

在文件的末尾添加該代碼使用:應用程序/啓動/ 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; 
}); 

在您的視圖。PHP

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

最終結果:

enter image description here

0

我試圖簡單地用輸入:: get()方法,而不是輸入::舊的()......,它的工作! {{Form::radio('estado','V',(Input::get('estado')=="V"))}}