I have the following check box in my form. The form uses from model binding:
{!! 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() !!}
My controller action is as follows:
public function update(AccountRequest $request)
{
Auth::user()->update($request->all());
flash()->success('Details updated');
return redirect('account');
}
In my user model I have set this as a mass assignment field as well:
protected $fillable = ['name', 'text_only_email'];
Basically this is the issue, If I check the checkbox and leave the name field blank (name field is mandatory), I get the postback error as expected and the hidden input field gets set to true
. Now lets say for some reason I decide to uncheck the checkbox after the postback before I re-submit or lets say it's an update form rather than a create form, in which case the hidden field value will remain set to true
hence the form submission doesn't work as expected.
The only way I can think of how to make this work is by using some jquery to toggle the hidden value when the user checks or unchecks the checkbox.
I am aware that it can be achieved on the server side by using something like this: $request->input('text_only_email', false)
but I was wondering if anyone has come across a more elegant solution so that I dont need to keep checking on the serverside?
我认为您有两个名称相同的字段, text_only_email
是一个隐藏的输入以及一个复选框
通过添加一个手动的隐藏输入字段而不是使用表单生成器来生成它来设法解决该问题: <input name="text_only_email" value="" type="hidden">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.