验证错误laravel后保持模态对话框打开
所以基本上我有一个blade.php、控制器页面和表单请求页面(验证)。如果出现错误,我会尝试保持模态对话框打开,但我就是想不出来,我遗漏了或需要更改代码的哪一部分 blade.php验证错误laravel后保持模态对话框打开,laravel,modal-dialog,laravel-5.1,Laravel,Modal Dialog,Laravel 5.1,所以基本上我有一个blade.php、控制器页面和表单请求页面(验证)。如果出现错误,我会尝试保持模态对话框打开,但我就是想不出来,我遗漏了或需要更改代码的哪一部分 blade.php <div id="register" class="modal fade" role="dialog"> ... <script type="text/javascript"> if ({{ Input::old('autoOpenModal', 'false') }}) { /
<div id="register" class="modal fade" role="dialog">
...
<script type="text/javascript">
if ({{ Input::old('autoOpenModal', 'false') }}) {
//JavaScript code that open up your modal.
$('#register').modal('show');
}
</script>
request.php
class StoreNewUserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
// create the validation rules ------------------------
return [
'name' => 'required', // just a normal required validation
'email' => 'required|email|unique:users', // required and must be unique in the user table
'password' => 'required|min:8|alpha_num',
'password_confirm' => 'required|same:password', // required and has to match the password field
'mobile' => 'required',
'role_id' => 'required'
];
}
}
Laravel会自动检查会话数据中的错误,因此,
$errors
变量实际上在所有视图中始终可用。如果要在出现任何错误时显示模式,可以尝试以下操作:
<script type="text/javascript">
@if (count($errors) > 0)
$('#register').modal('show');
@endif
</script>
@如果(计数($errors)>0)
$('#register').modal('show');
@恩迪夫
将If条件置于脚本之外。以上这些在我的情况下不起作用
@if (count($errors) > 0)
<script type="text/javascript">
$( document ).ready(function() {
$('#exampleModal2').modal('show');
});
</script>
@endif
@if(计数($errors)>0)
$(文档).ready(函数(){
$('exampleModal2').modal('show');
});
@恩迪夫
只需将您的模式名称替换为“登录模式”。为了避免错误,请将其放在链接或初始化jquery的jquery文件之后
<?php if(count($login_errors)>0) : ?>
<script>
$( document ).ready(function() {
$('#login-modal').modal('show');
});
</script>
<?php endif ?>
$(文档).ready(函数(){
$(“#登录模式”).model('show');
});
非常感谢,感谢您提供有关$errors部分的更多知识。=)我已经使用了代码,现在我的错误消息是确定的。但是当我点击登录按钮后,模态就绝望了,当我再次点击模态时,它会显示错误消息。我该怎么解决呢?@KaziRabbiHassan你解决了这个问题吗?是的,我已经解决了这个问题。
<?php if(count($login_errors)>0) : ?>
<script>
$( document ).ready(function() {
$('#login-modal').modal('show');
});
</script>
<?php endif ?>