Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

laravel - How to make a form validation in InertiaJS

I am follow this Link for a inertiaJS tutorial. BTW I'm using 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.

enter image description here

Update : Code for my controller

enter image description here

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'),
        ];
    });
}
question from:https://stackoverflow.com/questions/65920749/how-to-make-a-form-validation-in-inertiajs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To access the email error, context needs to be added via the this keyword ie. this.$page.props.errors.email.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...