简体   繁体   English

如何在 InertiaJS 中进行表单验证

[英]How to make a form validation in InertiaJS

I am follow this Link for a inertiaJS tutorial.我关注此链接以获取inertiaJS教程。 BTW I'm using laravel 8顺便说一句,我正在使用laravel 8

I have this image below from my webpage.下面这张图片来自我的网页。 I don't know why I don't have a page>errors in picture given.我不知道为什么我没有页面>图片中的错误。 Is there any other way to validate a form other than the link that I provided?.除了我提供的链接之外,还有其他方法可以验证表单吗?

I commented out the page.error in my HTML because it causes me an error Error in render: "TypeError: Cannot read property 'email' of undefined" because of my Page.Error is an empty object.我在 HTML 中注释掉了 page.error,因为它导致我出现Error in render: "TypeError: Cannot read property 'email' of undefined"因为我的 Page.Error 是一个空的 object。

在此处输入图像描述

Update: Code for my controller更新:我的 controller 的代码

在此处输入图像描述

Script Code脚本代码

<script>
import Layout from "../../Shared/Layout";
export default {
components: {
   Layout,
},
data() {
return {
  lead: {
    name: "",
    email: "",
    phone: "",
    dob: "",
    interested_package: "",
  },
};
},
methods: {
async handleSubmit() {
  let res = await this.$inertia.post("/leads/save", this.lead);
  },
  },
};
</script>

App Service Provider应用服务提供商

   public function boot()
{
    //
    Schema::defaultStringLength(255);
    Inertia::share([
        'errors' => function () {
            return Session::get('errors')
                ? Session::get('errors')->getBag('default')->getMessage()
                :  (object)[];
        }
    ]);

    Inertia::share('flash', function () {
        return [
            'message' => Session::get('message'),
            'success' => Session::get('success'),
            'error' => Session::get('error'),
        ];
    });
}

To access the email error, context needs to be added via the this keyword ie.要访问 email 错误,需要通过this关键字添加上下文,即。 this.$page.props.errors.email . this.$page.props.errors.email

Hey i think you are not sharing error bag in Controller that is why is empty,to solve this, go to your AppServiceProvider and add this code.嘿,我认为您没有在 Controller 中共享错误包,这就是为什么为空,要解决此问题,请将 go 到您的AppServiceProvider并添加此代码。

  Inertia::share([
    'errors' => function () {
        return Session::get('errors')
            ? Session::get('errors')->getBag('default')->getMessages()
            : (object) [];
    },
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM