Skip to content

Use CRUD form in CollectionField #5210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

michaelKaefer
Copy link
Contributor

@michaelKaefer michaelKaefer commented May 1, 2022

Implements what @furang wrote here: #4766.

Needs Symfony 6.1 because it uses prototype_options (See symfony/symfony#45605).

I createad a reproducer here: https://github.com/michaelKaefer/ea-reproducer/tree/issue/4766-create-update-relations.

Without this PR you have to create a form type and set it as entry type for associated entities when using CollectionField. This doesn't allow using EA fields and could sometimes kind of duplicates the CRUD form of the associated entity. This new option would enable you to use the CRUD form (if you need different form fields and you cannot reuse the CRUD form you still can implement a custom form type).

Related to #3304.

@michaelKaefer michaelKaefer force-pushed the issue/4766-create-update-relations branch from 914106c to 0f787a5 Compare May 1, 2022 15:38
@michaelKaefer michaelKaefer force-pushed the issue/4766-create-update-relations branch 3 times, most recently from 1869712 to c1a8336 Compare May 7, 2022 13:47
@michaelKaefer michaelKaefer force-pushed the issue/4766-create-update-relations branch from c1a8336 to 24a99a1 Compare May 7, 2022 13:49
@sxsws
Copy link

sxsws commented May 12, 2022

Thanks, this looks great and seems it will solve many headaches I have. Would it be possible to either specify a crud form class name, or the name of a function in the crud form, so that a separate form could be utilised? e.g. it could be school -> pupils, with only basic values wanted in a collection field of pupils (name, date of birth), but full fields with tabs etc when editing pupil directly.

@michaelKaefer
Copy link
Contributor Author

@sxsws Sorry, I think it will not solve your headaches.. It seems that you want to maintain a collection of pupils in the form of another entity school. The problem is that your users cannot add existing pupils to the collection, even after this commit this is not possible. After this commit your users can only edit pupils which already are associated to the school, or they can add a new pupil. If this really is sufficient, then, ok, the headaches are solved :)

What you are asking for is aleady possible in this PR:

class SchoolCrudController extends AbstractCrudController
{
    public function configureFields(string $pageName): iterable
    {
        return [
            CollectionField::new('pupils')->useCrudForm(
                true,
                PupilCrudController::class,
                'new_pupil_on_school_page',
                'edit_pupil_on_school_page'
            );
        ];
    }
}

class PupilCrudController extends AbstractCrudController
{
    public function configureFields(string $pageName): iterable
    {
        switch ($pageName) {
            case 'new':
            case 'edit':
                return [TextField::new('foo'), TextField::new('bar'), TextField::new('baz')];
            case 'new_pupil_on_school_page':
            case 'edit_pupil_on_school_page':
                return [TextField::new('foo')];
        }
        throw new \InvalidArgumentException();
    }
}

@7underlines
Copy link

This is gold 🏆 - please merge!

@parijke
Copy link
Contributor

parijke commented May 17, 2022

Since it needs SF6.1 this can be quite difficult to merge. @javiereguiluz want to preserve BC as much as possible.

@michaelKaefer
Copy link
Contributor Author

@parijke It only needs SF6.1 to work, with SF6.0 there is no problem at all, you just will get an exception if you want to still use the new feature. See https://github.com/EasyCorp/EasyAdminBundle/pull/5210/files#diff-100aaf7a11a06c600ef2c48bb42aa90c85ed6253721bf953770dbed7bdda8224 :

// ...
throw new \RuntimeException('To enable CRUD form usage for collection fields Symfony 6.1 or higher is required.', 0, $exception);

So AFAICS there is no problem to merge it.

@parijke
Copy link
Contributor

parijke commented May 17, 2022

@parijke It only needs SF6.1 to work, with SF6.0 there is no problem at all, you just will get an exception if you want to still use the new feature. See https://github.com/EasyCorp/EasyAdminBundle/pull/5210/files#diff-100aaf7a11a06c600ef2c48bb42aa90c85ed6253721bf953770dbed7bdda8224 :

// ...
throw new \RuntimeException('To enable CRUD form usage for collection fields Symfony 6.1 or higher is required.', 0, $exception);

So AFAICS there is no problem to merge it.

Nice!

@michaelKaefer
Copy link
Contributor Author

@javiereguiluz What do you think about this PR? If it doesn't get merged I would like to close it. Maybe it would help if I try to add tests?

@javiereguiluz
Copy link
Collaborator

Michael, I'm sorry I haven't found the time to review this PR 🙏 I'll try to find time for it soon. Thanks!

@michaelKaefer
Copy link
Contributor Author

@javiereguiluz Ok, sorry, no need to hurry!

@devzenfr
Copy link

This is great! I just switched to EasyAdmin and needed exactly this feature. Used your commit inside my project until this gets merged and this is working perfectly. Thanks for your work.

@asanikovich
Copy link
Contributor

asanikovich commented May 30, 2022

@Lustmored @javiereguiluz Please look at this pull, we all need it.

@javiereguiluz
Copy link
Collaborator

Michael, thanks for this feature and for the reproducer. I could play with the sample app that you provided and I liked it.

I need a bit more time to think about the design of the feature and the naming of some methods, but I hope to merge this soon. Thanks!

@javiereguiluz javiereguiluz added feature priority: important Bugs to fix and features to implement labels Jun 1, 2022
@javiereguiluz javiereguiluz added this to the 4.x milestone Jun 1, 2022
@javiereguiluz
Copy link
Collaborator

Thanks Michael! I'm merging this nice feature and I'll send a following PR with some minor tweaks.

@javiereguiluz javiereguiluz merged commit ea57131 into EasyCorp:4.x Jun 4, 2022
javiereguiluz added a commit that referenced this pull request Jun 5, 2022
…on fields (javiereguiluz)

This PR was merged into the 4.x branch.

Discussion
----------

Minor tweaks in the feature to use CRUD forms in collection fields

This PR proposes some minor tweaks in the #5210 feature contributed by `@michaelKaefer`.

Commits
-------

19387e0 Minor tweaks in the feature to use CRUD forms in collection fields
@asanikovich
Copy link
Contributor

Hello. Based on this future, how to display relation in Action::DETAIL?
@michaelKaefer

@michaelKaefer
Copy link
Contributor Author

@allok You are working for a company in Minsk, I for sure will not answer your questions, because Belarus supports an illegal war.

javiereguiluz added a commit that referenced this pull request Nov 5, 2022
…efer)

This PR was merged into the 4.x branch.

Discussion
----------

Add CRUD form option for to-one associations

Without this PR the association field renders a a dropdown and you cannot edit the associated entity but just select an already existing entity. If you want to edit an associated entity you cannot use `AssociationField` and so you have to create a custom EA field. Creating a custom EA field doesn't allow using EA fields and could sometimes kind of duplicate the CRUD form of the associated entity. This PR would enable you to use the CRUD form for `AssociationField` if it is a to-one association (for to-many associations `CollectionField` can be used).

I createad a reproducer: https://github.com/michaelKaefer/ea-reproducer/tree/association-field-use-crud-form.

Similar to #5210.

Commits
-------

acd06da Add CRUD form option for to-one associations
kiler129 added a commit to kiler129/EasyAdminBundle that referenced this pull request Dec 23, 2022
Functionality introduced in EasyCorp#5353
(AssociationType) and in EasyCorp#5210
(CollectionType) rendered sub-CRUD controller with context of the primary CRUD
controller. With invalid context, some functions, like default fields generation,
aren't working properly. See EasyCorp#5512
kiler129 added a commit to kiler129/EasyAdminBundle that referenced this pull request Dec 23, 2022
Functionality introduced in EasyCorp#5353
(AssociationType) and in EasyCorp#5210
(CollectionType) rendered sub-CRUD controller with context of the primary CRUD
controller. With invalid context, some functions, like default fields generation,
aren't working properly. See EasyCorp#5512
kiler129 added a commit to kiler129/EasyAdminBundle that referenced this pull request Dec 23, 2022
Functionality introduced in EasyCorp#5353
(AssociationType) and in EasyCorp#5210
(CollectionType) rendered sub-CRUD controller with context of the primary CRUD
controller. With invalid context, some functions, like default fields generation,
aren't working properly. See EasyCorp#5512
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature priority: important Bugs to fix and features to implement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants