我是bootstrap模式的新手。 在使用laravel 5.2进行验证后,如何在模态中显示变量$ errors? 更像是这样的东西:
in modal我有:
<!--- Register Modal -->
<div class="modal fade" id="Register" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Registro</h4>
</div>
<div class="modal-body">
<form role="form" actior="{{ route('register') }}" method='post' >
<label> <div class="form-group">
<label for="email">Nombre: </label>
<input type="name" class="form-control" name='name' placeholder="ingresa tu nombre">
</div>
</label>
<label>
<div class="form-group">
<label for="email">Apellido: </label>
<input type="name" class="form-control" name='lastname' placeholder="ingresa tu apellido">
</div>
</label>
<div class="form-group">
<label for="email">Correo electronico: </label>
<input type="email" class="form-control" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="pwd">Contraseña:</label>
<input type="password" class="form-control" name="password" placeholder="ingrese la contraseña">
</div>
<div class="form-group">
<label >Confirmar contraseña:</label>
<input type="password" class="form-control" placeholder="ingrese nuevamente la contraseña">
</div>
<input type="hidden" name="_token" value="{{Session::token()}}"> <!---seguridad -->
<button type="submit" class="btn btn-default" onclick="send(event)" data-dismiss="modal" id='Submit'>Registro</button>
</form>
</div>
</div>
</div>
变量使用以下代码通过jquery发送它们:
function send(event){
event.preventDefault();
$.ajax({
type: 'POST',
url: "{{route('register')}}",
data: {name: $('#name').val(), lastname: $('#lastname').val(), email: $('#email').val(), password: $('#password').val(), _token:'{{Session::token()}}'}
});
$('#header').load();
}
谢谢!
答案 0 :(得分:1)
您应该看看here,尝试手动验证您的表单,然后将错误附加到响应中?
在JS中:
$.ajax({
type: 'POST',
url: "{{route('register')}}",
data: {
name: $('#name').val(),
lastname: $('#lastname').val(),
email: $('#email').val(),
password: $('#password').val(),
_token:'{{Session::token()}}' // Bad practice to mix Js and PHP
},
success: function(res) {
if (res.error) {
// Add error in your modal, for example :
$('#Register').prepend(data.error);
}
else {
// Close your modal
$('#Register').modal('hide');
}
}
});
在Laravel控制器中:
$validator = Validator::make($request->all(), [
// Your form rules, for example :
'name' => 'required|string|max:32',
'lastname' => 'required|string|max:32',
'email' => 'required|email',
'password' => 'required|alpha_num',
]);
if ($validator->fails())
return response()->json(['error' => $validator->errors()->first()]);
对于Js部分,您应该查看jQuery .serialize() method。