【问题标题】:Ajax format after submit - Laravel提交后的 Ajax 格式 - Laravel
【发布时间】:2023-03-23 12:23:01
【问题描述】:

我正在做项目,我遇到了一些问题

如果我填写所有字段然后提交,则没有问题并将其保存到数据库中,但如果某些字段为空,则验证消息错误会以 JSON 格式显示在另一个页面中。

我的视图文件中没有使用任何 AJAX 代码。

这是控制器代码:

public function store(RegisterRequest $request){
    $user = User::create($request->all());
    $user->password = Hash::make($request['password']);

    if ($request->file('avatar')) {
        $image = $request->file('avatar');
        $destinationPath = base_path() . '/public/uploads/default';
        $path = time() . '_' . Str::random(10) . '.' . $image->getClientOriginalExtension();
        $image_resize = Intervention::make($image->getRealPath());
        $image_resize->resize(300, 300);
        $image_resize->save($destinationPath . '/' . $path);
    } else {
        $path = $user->avatar;
    }
    $user->avatar = $path;

    $user->save();

    return redirect()->route('admin.user.index')->with('message','User created successfully');

这里是RegisterRequest 代码:

public function rules()
{
    return [
        'name'         => 'required',
        'email'        => 'required|email|unique:users,email',
        'password'     => 'required|min:6|confirmed',
        'country_code' => 'sometimes|required',
        'phone'=>Rule::unique('users','phone')->where(function ($query) {
            $query->where('country_code', Request::get('country_code'));
        })
    ];

你能帮帮我吗?

【问题讨论】:

  • phone 规则是唯一重要的事情,您是否尝试将其注释掉? error appear in another page - 另一个页面是什么,我的意思是 URL 是什么?它是您开始的表单页面的 URL 吗?就路由和表单操作等而言,您是如何到达那里的是否有意义?

标签: json laravel forms validation request


【解决方案1】:

您的错误应该可以在带有 $errors 变量的刀片文件中访问,您需要迭代并显示错误。 链接到将帮助您处理渲染部分的文档 - https://laravel.com/docs/7.x/validation#quick-displaying-the-validation-errors

显然也来自文档

If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.

https://laravel.com/docs/7.x/validation#creating-form-requests

还将代码重构如下,只运行一个查询来创建用户,而不是创建然后更新。

 public function store(RegisterRequest $request){
    if ($request->hasFile('avatar')) {
        //use try catch for image conversion might be a rare case of lib failure
        try {
            $image = $request->file('avatar');
            $destinationPath = base_path() . '/public/uploads/default';
            $path = time() . '_' . Str::random(10) . '.' . $image->getClientOriginalExtension();
            $image_resize = Intervention::make($image->getRealPath());
            $image_resize->resize(300, 300);
            $image_resize->save($destinationPath . '/' . $path);
            $request->avatar = $path;
        } catch(\Exception $e){
            //handle skip or report error as per your case
        }
    }
    $request['password'] = Hash::make($request['password']);
    $user = User::create($request->all());
    return redirect()->route('admin.user.index')->with('message','User created successfully');
}

【讨论】:

  • 非常感谢,但我已经在我的视图中使用了 $errors 变量,我遇到了同样的问题
  • 如果你被卡住并且赶时间,那么使用内联验证器来处理请求可以让你更好地控制重定向。
  • 尝试在钩子之后添加并在钩子之后添加重定向,这也应该可以工作,还没有测试过。 public function withValidator($validator) { $validator->after(function ($validator) { //有错误时重定向回来 }); }
  • 我已经试过了,但我还是有同样的问题。我不是唯一一个建造这个项目的人。有没有可能是其他程序员更改了一些文件?
  • 很有可能。删除您的供应商文件夹。运行 composer install 再次安装需求。
猜你喜欢
  • 2015-10-25
  • 2016-06-11
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 2020-03-25
相关资源
最近更新 更多