dqjmq28248 2014-10-23 19:27
浏览 63
已采纳

Laravel 4 PHP:使用“未选中”复选框编辑表单会产生“未定义索引”错误

I have an edit form containing several checkboxes. When I try to update the form with any of the checkboxes left 'unchecked', I get an "undefined index" error for that particular checkbox. When I initially store new data, 'unchecked' checkboxes are stored just fine. It's only an issue if I try to edit data and leave checkboxes 'unchecked'.

I've tried using the '{{Form::hidden(fieldname, 0) }}' method but it hasn't been working for me.

edit-album.blade.php(View):

{{ Form::model($album, array('method' => 'PUT', 'route' => array('edit_album2', $album->album_id))) }}

<div class ="form-group">

    {{ Form::checkbox('album_application_kitchen', 'Kitchen') }}
    {{ Form::label('album_application_kitchen', 'Kitchen') }}
    {{ Form::checkbox('album_application_bathroom', 'Bathroom') }}
    {{ Form::label('album_application_bathroom', 'Bathroom') }}<br />

</div>

{{ Form::close() }}

EditAlbumsController2.php(Controller):

public function update($id) {

$input = \Input::all();

$validation = new Validators\Album($input);

    if ($validation->passes())
    {
      $album = Album::find($id);
      $album->album_application_kitchen = $input['album_application_kitchen'];  
      $album->album_application_bathroom = $input['album_application_bathroom'];
      $album->touch();
      $album->save();

      return \Redirect::route('gallery.album.show', array('id' => $id));
    }
    else
    {
      /* Code for when validation fails */
    }
}

Is there a special trick to solve this problem or am I just not using the {{Form::hidden()}} structure properly?

  • 写回答

2条回答 默认 最新

  • doujiao1814 2014-10-23 20:05
    关注

    In your controller, when you assign Input::all() to $input, no element gets added to the $input array for unchecked checkboxes, because they don't exist in the Input::all() array (unchecked checkboxes don't get passed in POST.) When updating, use Input::get() instead, which will return null if there is no value for an input, as is the case with any unchecked checkboxes:

    $album->album_application_kitchen = Input::get('album_application_kitchen');  
    $album->album_application_bathroom = Input::get('album_application_bathroom');
    

    Besides, $input = Input::all() is redundant anyway, since Input::all() is already an array. Just pass Input::all() to your validator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。