Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel 5.1中的复选框输入_Php_Eloquent_Laravel 5.1 - Fatal编程技术网

Php 使用Laravel 5.1中的复选框输入

Php 使用Laravel 5.1中的复选框输入,php,eloquent,laravel-5.1,Php,Eloquent,Laravel 5.1,我的表格中有以下复选框。该表单使用从模型绑定: {!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update']]) !!} {!! Form::hidden('text_only_email', false) !!} <div class="checkbox"> <label>

我的表格中有以下复选框。该表单使用从模型绑定:

 {!! Form::model($user, ['method' => 'PATCH', 'action' => ['AccountController@update']]) !!}
       {!! Form::hidden('text_only_email', false) !!}
       <div class="checkbox">
            <label>
                  {!! Form::checkbox('text_only_email', 'true') !!} 
                  I'd prefer to receive emails as text only
            </label>
      </div>
      <div class="form-group">
           {!! Form::label('name', 'Name *', ['class' => '']) !!}
           {!! Form::text('name', null, ['class' => 'form-control']) !!}
           {!! errors_for('name', $errors) !!}
      </div>
      <button type="submit" class="btn btn-primary ">Update</button>
  {!! Form::close() !!}
在我的用户模型中,我也将其设置为质量分配字段:

protected $fillable = ['name', 'text_only_email'];
基本上这就是问题所在,如果我选中复选框并将名称字段留空(名称字段是必需的),我会得到预期的回发错误,并且隐藏的输入字段设置为
true
。现在,假设出于某种原因,我决定在重新提交之前取消回发后的复选框,或者说这是一个更新表单,而不是一个创建表单,在这种情况下,隐藏字段值将保持设置为
true
,因此表单提交无法按预期工作

我能想到的唯一方法是,当用户选中或取消选中复选框时,使用一些jquery切换隐藏值


我知道在服务器端可以通过如下方式实现:
$request->input('text\u only\u email',false)
,但我想知道是否有人找到了更优雅的解决方案,这样我就不需要在服务器端继续检查了?

我想您有两个同名字段,
text\u only\u email
是一个隐藏的输入,也是一个复选框

通过添加一个手动隐藏的输入字段而不是使用表单生成器生成它来修复:

复选框如果未选中,则不向服务器发送任何值。因此,上面的技巧是确保将某些值发送回服务器。
protected $fillable = ['name', 'text_only_email'];