Giter Club home page Giter Club logo

Comments (14)

yassilah avatar yassilah commented on September 23, 2024 1

This has been fixed as of 1.0.4.

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

@yassipad This doesn't look like it worked. It still creates the main resource if the child resource fails validation on 1.0.4 for me

from laravel-nova-nested-form.

yassilah avatar yassilah commented on September 23, 2024

Can you send me your fields array with validation rules? It works fine even with deeply nested fields by me...

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

Parent

return [
            Text::make('Name')
                ->rules('required'),
            Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:members,email')
                ->updateRules('unique:members,email,{{resourceId}}'),

            HasMany::make('Memberships'),

            NestedForm::make('Memberships')
            ->max(1),
];

Nested

return [
            BelongsTo::make('Member')
                ->hideWhenCreating(),

            BelongsTo::make('Plan')
                ->rules('required'),

            Date::make('Start Date')
                ->rules('required'),

            Date::make('End Date')
                ->hideWhenCreating(),
];

from laravel-nova-nested-form.

yassilah avatar yassilah commented on September 23, 2024

Well, to be honest I have no idea why you’re getting that behaviour… I just tested it with your exact resource fields and it works as expected. Besides, the parent could not be created, since the nested logic happens after calling the « $resource::fill() » method of the parent resource and validation exceptions are thrown before the « $model->save(); » is called… Can you send a gif or something so I can see your creation flow?

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

Heres a video. Thanks for looking into this!

https://www.dropbox.com/s/2hy1zzdr5i4i8ef/Untitled1.mov?dl=0

from laravel-nova-nested-form.

yassilah avatar yassilah commented on September 23, 2024

Can you make sure this is the latest version of the plugin?

composer remove yassi/nova-nested-form
composer clearcache
composer require yassi/nova-nested-form 1.0.4

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

I tried the above and no luck. I also tried clearing laravel cache, restarting local web server and performing in incognito windows. I'll play around some more and see what I can figure out.

My composer.lock has the version e5a64e6587f4d8de428d3d9eb5b4192952ad0af2 which is correct.

from laravel-nova-nested-form.

yassilah avatar yassilah commented on September 23, 2024

What about you nova version?

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

1.1.7

from laravel-nova-nested-form.

yassilah avatar yassilah commented on September 23, 2024

Well, then I don’t know... sorry!

from laravel-nova-nested-form.

tobeycodes avatar tobeycodes commented on September 23, 2024

I use https://github.com/hyn/multi-tenant so perhaps this is the issue. My field definition is really this as it's not looking in the default database. Either way, it appears that this is an issue isolated to my project. I will give this a try on a fresh install of Nova and see. But for now, I'm closing this.

Text::make('Email')
                ->sortable()
                ->rules('required', 'email', 'max:254')
                ->creationRules('unique:tenant.members,email')
                ->updateRules('unique:tenant.members,email,{{resourceId}}'),

from laravel-nova-nested-form.

alberto-bottarini avatar alberto-bottarini commented on September 23, 2024

The problem exists in creation form when a unique rule is applied:

protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
    {
        if ($model->exists) {
            $newRequest = NovaRequest::createFrom($request);
            $children = collect($newRequest->get($requestAttribute));
            $newRequest->route()->setParameter('resource', $this->resourceName);
            $this->deleteChildren($newRequest, $model, $children);
            $this->createOrUpdateChildren($newRequest, $model, $children, $requestAttribute, $this->getRelatedKeys($newRequest));
        } else {
            $model::saved(function ($model) use ($request, $requestAttribute, $attribute) {
                $this->fillAttributeFromRequest($request, $requestAttribute, $model, $attribute);
            });
        }
    }

If the model does not exists else statement gets executed.
It add a listener on saved event of the model.

If the validationException occurred creating children, the main model gets created

from laravel-nova-nested-form.

alberto-bottarini avatar alberto-bottarini commented on September 23, 2024

Ok, I discovered a lot of things:

Nova uses transactions in StoreController, so this is what happens if a child is not valid:

  • nova create a transaction
  • parent is inserted
  • child throw validation
  • nova rollback transaction

You can easily test this scenario watching your parent resource id. Every time a child is not valid, a new "autoincremented" id get burned. I tried about 20 times and my autoincrement counter increased without having saved record.

I don't like so much this implementation but it worked except when you have unique validation rules.

@yassipad I think you/we should change fillAttributeFromRequest in order to pre-validate children before parent model creation.

from laravel-nova-nested-form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.