使用Laravel 4,如果会话密钥是指定值,我如何将单选按钮标记为已检查


Using Laravel 4, how do I mark a radio button as checked if a session key is a specified value?

我有一个多页表单,其中包含两个具有相同名称属性的单选按钮。当我选择一个并单击下一步按钮时,我会将该单选按钮的值保存到一个会话数组中,该数组包含表单字段名称和所选值。如果用户返回页面,我希望选中之前选择的单选按钮。

这就是我想到的:

视图:选择列表类型.blade.php

<div class="form-group">
  <?php $checked_status = Session::get('listing_form_data.type') === 'property' ? true : false; ?>
  {{ Form::radio('type', 'property', $checked_status) }} Create Property Listing
</div>
<div class="form-group">
  <?php $checked_status = Session::get('listing_form_data.type') === 'room' ? true : false; ?>
  {{ Form::radio('type', 'room', $checked_status) }} Create Room Listing
</div> 

这很有效,但看起来很草率。首先,我认为检查会话值的if语句不应该出现在视图中,我很想在blade中找到一种方法。

使用Laravel 4,根据指定会话密钥的值将单选按钮标记为已检查的最佳做法是什么?

由于您提到您想在控制器中执行此操作:

$type = Session::get('listing_form_data.type');
return View::make('view')->with('type', $type);

视图:

{{ Form::radio('type', 'property', $type === 'property') }} Create Property Listing
{{ Form::radio('type', 'room', $type === 'room') }} Create Room Listing

甚至:

$type = Session::get('listing_form_data.type');
$isProperty = ($type === 'property');
$isRoom = ($type === 'room');
return View::make('view')->with(compact('isProperty', 'isRoom'));

视图:

{{ Form::radio('type', 'property', $isProperty) }} Create Property Listing
{{ Form::radio('type', 'room', $isRoom) }} Create Room Listing

为什么不将条件权限与Form助手内联,如下所示:

<div class="form-group">
  {{ Form::radio('type', 'room', (Session::get('listing_form_data.type') === 'room') ? true : false) }} Create Room Listing
</div>

就我个人而言,我认为从这个角度检查会话设置没有什么错。。。