inertiajs / inertia-laravel

The Laravel adapter for Inertia.js.
https://inertiajs.com
MIT License
2.07k stars 232 forks source link

I can't validate array of fields #189

Closed IgorDePaula closed 3 years ago

IgorDePaula commented 3 years ago

If I try validate an array of fields, like multiple upload,

 if (! this.form.hasErrors()) {

return 0, but

this.$page.errorBags['teamInvite']

return all erros from validator (from backend)

albertojm8 commented 3 years ago

Same thing happened to me, form.hasErrors() was always empty, even when i have one or multiple validation errors:

Inertia form:

data() {
            return {
                form: this.$inertia.form({
                    '_method': 'PUT',
                    name: this.product.name,
                    description: this.product.description,
                    price: this.product.price,
                    discount: this.product.discount,
                    code: this.product.code,
                    created_at: this.product.created_at,
                    updated_at: this.product.updated_at,
                    image: this.product.image,
                }, {
                    bag: 'updateProductInformation',
                    resetOnSuccess: false,
                }),

                imagePreview: null,
            }
},

VueJs post method:

updateProductInformation() {

          this.form.post(route('products.update', this.product.id), {
              preserveScroll: true
          });
},

Controller:

public function update(Request $request, $id)
{
        $product = Product::findOrFail($id);

        $product->update(
            Request::validate([
                'name' => 'required|max:100',
                'code' => 'required|max:10',
                'description' => 'required|max:300',
                'price' => 'required|integer|max:1000',
                'discount' => 'required|integer|max:100',
                'image' => 'sometimes',
            ])
        );

        return Redirect::back()->with('success', 'Product updated.');

}

Response:

props: {errors: {price: "The price may not be greater than 1000.",…},…} currentRouteName: "products.show" errorBags: {default: {price: ["The price may not be greater than 1000."],…}} default: {price: ["The price may not be greater than 1000."],…} errors: {price: "The price may not be greater than 1000.",…} jetstream: {canCreateTeams: true, canManageTwoFactorAuthentication: true, canUpdatePassword: true,…} product: {id: 21, name: "cultivate sticky e-markets123",…} `

@IgorDePaula Apparently it has something to do with the bag definition. I just deleted it from the form and now it works (I don't know how to respond to the correct bag / errorBag.

data() {
            return {
                form: this.$inertia.form({
                    '_method': 'PUT',
                    name: this.product.name,
                    description: this.product.description,
                    price: this.product.price,
                    discount: this.product.discount,
                    code: this.product.code,
                    created_at: this.product.created_at,
                    updated_at: this.product.updated_at,
                    image: this.product.image,
                }, {
                    resetOnSuccess: false,
                }),

                imagePreview: null,
            }
        },
claudiodekker commented 3 years ago

Hi,

Since this is related to Laravel Jetstream's form helper (which has since been superseded/replaced by Inertia's official first-part form helper) I'll be closing this issue for now.

If the problem still occurs with that (new) form helper, please re-create an issue on https://github.com/inertiajs/inertia/issues instead, as this is the issue tracker for the PHP-side Laravel Adapter.

Thank you!

IgorDePaula commented 3 years ago

@albertojm8 @claudiodekker Imagine a form with 3 uploads fields, and I must have validate which one. I can't validate it as array.

And yes, I using with Laravel Jetstream. With another forms the bag is working fine.