gpt4 book ai didi

laravel - 验证错误laravel后保持模态对话框打开

转载 作者:行者123 更新时间:2023-12-03 15:05:59 24 4
gpt4 key购买 nike

所以基本上我有一个blade.php、 Controller 页面和一个表单请求页面(验证)。如果出现错误,我试图让我的模态对话框保持打开状态,但我无法弄清楚,我遗漏了哪些部分代码或需要更改?

Blade .php

<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>

Controller .php
class ManageAccountsController extends Controller
{
public $userRepository;

public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}

public function index()
{
$users = User::orderBy('name')->get();
$roles = Role::all();

return view('manage_accounts', compact('users', 'roles'));
}

public function register(StoreNewUserRequest $request)
{
// process the form here
$this->userRepository->upsert($request);
Session::flash('flash_message', 'User successfully added!');

//$input = Input::except('password', 'password_confirm');
//$input['autoOpenModal'] = 'true'; //Add the auto open indicator flag as an input.

return redirect()->back();
}
}

class UserRepository {

public function upsert($data)
{

// Now we can separate this upsert function here
$user = new User;
$user->name = $data['name'];
$user->email = $data['email'];
$user->password = Hash::make($data['password']);
$user->mobile = $data['mobile'];
$user->role_id = $data['role_id'];

// save our user
$user->save();

return $user;
}
}

请求文件
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 会自动检查 session 数据中的错误,因此,$errors变量实际上在您的所有 View 中始终可用。如果您想在出现任何错误时显示模态,您可以尝试以下操作:

<script type="text/javascript">
@if (count($errors) > 0)
$('#register').modal('show');
@endif
</script>

关于laravel - 验证错误laravel后保持模态对话框打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33459143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com