【发布时间】:2015-02-25 07:33:15
【问题描述】:
验证后,我在我的模式中丢失了 Session/messagebag $error 数据。我想这是因为我正在将我的编辑表单 attendee/edit 从链接加载到模态正文中,因此 $error 数据被发送到调用模态而不是模态正文的页面。当我关闭模式并让它直接加载页面 attendee/edit 时,验证错误显示正常。有没有办法将我的 $error 变量传递给模态体中显示的编辑表单?
我更喜欢使用 AJAX,因为在我当前的实现中,我似乎需要重新加载模式,以便它弹出/弹出。但我也不确定如何将 $error messagebag 对象从编辑表单/会话数据传输到模态。我想知道有没有办法使用stackoverflow.com/questions/25103743/laravel-4-validation-in-bootstrap-modal这个方法来填写$error messagebag。
主视图
<td>{{link_to_route('attendee.edit','',array($attendee->id), array(
'class'=>'edit-attendee btn btn-info btn-xs glyphicon glyphicon-pencil',
'data-title' => 'Edit Attendee'))}}
</td>
JQuery 调用 BootstrapDialog
$(document).ready(function(){
$('.btn.edit-attendee').click(function(e){
e.preventDefault();
url = $(this).attr('href');
BootstrapDialog.show({
title: $(this).data('title'),
message: $('<div></div>').load(url),
buttons: [{
label: 'Update',
action: function(dialogRef) {
$('form').submit();
}
}]
});
});
});
与会者/编辑视图
{{ Form::model($attendee, array('class'=>'form-horizontal', 'method' => 'PATCH', 'route' => array('attendee.update', $attendee->id))) }}
<div class="form-group {{{ $errors->has('first_name') ? 'has-error' : '' }}}">
<label class="col-xs-3 control-label", for="first_name">First Name</label>
<div class="col-xs-9">
{{ Form::text('first_name', null , array('class' => 'form-control')) }}
</div>
{{ $errors->first('first_name', '<span class="help-inline">:message</span>')}}
</div>
<div class="form-group {{{ $errors->has('special_care') ? 'has-error' : '' }}}">
<label class="col-xs-3 control-label", for="special_care">Special Care</label>
<div class="col-xs-9">
{{ Form::text('special_care', null , array('class' => 'form-control')) }}
</div>
{{ $errors->first('age', '<span class="help-inline">:message</span>')}}
</div>
{{Form::submit()}}
{{ Form::close() }}
控制器
public function update($id)
{
$attendee = Attendee::findOrFail($id);
$validator = Validator::make($data = Input::all(), Attendee::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$attendee->update($data);
return Redirect::route('attendees.index');
}
编辑表单后,我想返回模态窗口以显示验证错误,但 $errors 不会传递给模态。
【问题讨论】:
-
如果一个页面上有多个表单,->withErrors($validator, 'update');和 update->first('fieldname'); ?>
-
我只有一张表格。
标签: ajax laravel bootstrap-dialog