Skip to content
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

Pass Model to ChildForm #157

Closed
mstaack opened this issue Sep 5, 2015 · 33 comments
Closed

Pass Model to ChildForm #157

mstaack opened this issue Sep 5, 2015 · 33 comments

Comments

@mstaack
Copy link

mstaack commented Sep 5, 2015

How do i pass a model reference to childs forms?

    public function buildForm()
    {
        $this
            ->add('User', 'form', [
                'class' => 'App\Forms\UserForm',
                'formOptions' => [],
                'model' => $user,
            ])->add('Company', 'form', [
                'class' => 'App\Forms\CompanyForm',
            ]);
    }

'model'=>$user is not working...form is rendered, but empty

@kristijanhusak
Copy link
Owner

Is $user the same model as the one that is passed to the main form, or it is something different?

@mstaack
Copy link
Author

mstaack commented Sep 6, 2015

i can even use Auth::user() and it is still not workin....
attributes names are okay

or should i pass a model through the main form explicitly?

@kristijanhusak
Copy link
Owner

Since Child form is form type like any other, you can pass it as value:

    public function buildForm()
    {
        $this
            ->add('User', 'form', [
                'class' => 'App\Forms\UserForm',
                'formOptions' => [],
                'value' => $user,
            ])->add('Company', 'form', [
                'class' => 'App\Forms\CompanyForm',
            ]);
    }

Another way that is also possible, is to pass model from formOptions:

    public function buildForm()
    {
        $this
            ->add('User', 'form', [
                'class' => 'App\Forms\UserForm',
                'formOptions' => [
                    'model' => $user
                ],
            ])->add('Company', 'form', [
                'class' => 'App\Forms\CompanyForm',
            ]);
    }

This 2nd solution will not work if the model does not have all user data namespaced by User, until i fix it, which will be done now, but only on master branch.

@mstaack
Copy link
Author

mstaack commented Sep 15, 2015

will give this a try later

@tormit
Copy link

tormit commented Sep 18, 2015

public function buildForm()
    {
        $this
            ->add('User', 'form', [
                'class' => 'App\Forms\UserForm',
                'formOptions' => [],
                'value' => $user,
            ])->add('Company', 'form', [
                'class' => 'App\Forms\CompanyForm',
            ]);
    }

This way works.
Still, when I pass instance of Form(with model attached) to 'class', it should take existing values from there.

@kristijanhusak
Copy link
Owner

You are passing path to UserForm class as string, not an instance. You can pass an instance like this:

public function buildForm()
    {
        $userForm = $this->formBuilder->create('App\Forms\UserForm', ['model' => $user]);
        $this
            ->add('User', 'form', [
                'class' => $userForm
            ])->add('Company', 'form', [
                'class' => 'App\Forms\CompanyForm',
            ]);
    }

@tormit
Copy link

tormit commented Sep 18, 2015

I'm sorry. I just copied the snippet. My working code is something like this:

public function buildForm()
    {
        $userForm = $this->formBuilder->create('App\Forms\UserForm', ['model' => $user]);
        $this
            ->add('User', 'form', [
                'class' => $userForm,
                'value' => $user
            ]);
    }

The problem is that it should take the 'value' automatically from $userForm model.

@kristijanhusak
Copy link
Owner

Oh, it doesn't? I will check it. Thanks.

@kristijanhusak
Copy link
Owner

@tormit It's fixed in latest release (1.6.30). Do a composer update and check if it works. Thanks for reporting.

@tormit
Copy link

tormit commented Sep 22, 2015

Still does not work.
Maybe there is something with named forms, because both of my form are named(parent and child forms).

@kristijanhusak
Copy link
Owner

According to your snippet, your child form is not named:

  $userForm = $this->formBuilder->create('App\Forms\UserForm', ['model' => $user]);

@tormit
Copy link

tormit commented Sep 22, 2015

Yes, but in my real project both forms are named.

@kristijanhusak
Copy link
Owner

You should mention that. I will check it out.

@ikostic
Copy link

ikostic commented Sep 23, 2015

Hi Kristijan,

my previosly working code stopped working after composer update.

This is working code:

in controller initializing parent form with model

$form = $formBuilder->create('Something\Forms\AdminUserForm', [
            'method' => 'PATCH',
            'action' => ['AdminUserController@update', $id],
            'model' => $user_arr
        ]);

in parent form class:

public function buildForm()
    {
        $this
            ->add('Id', 'hidden')
            ->add('Name', 'text')
            ->add('Login', 'text')
            ->add('Email', 'email')
            ->add('Status', 'select', [
                'choices' => ['NEW' => 'NEW','APPROVED' => 'APPROVED','ON HOLD' => 'ON HOLD','DELETED' => 'DELETED'],
            ])
            ->add('Credentials', 'form',[
                'wrapper' => [
                    'class' => 'form-group credential-padding'
                ],
                'class' => $this->formBuilder->create('Something\Forms\AdminCredentialForm')
            ])
            ->add('LanguageId', 'select', [
                'label' => 'Default Language',
                'choices' => $languages,
            ]);
    }

in child form class:

public function buildForm()
    {
        $credentials = AdminCredentialQuery::create()->find();
        $credentials_arr = array();
        foreach($credentials as $credential){
            $name = $credential->getName();
            $id = $credential->getId();
            $this
                ->add($id, 'choice', [
                    'label' => $name,
                    'choices' => ['read' => 'read', 'write' => 'write', 'exec' => 'exec'],
                    'help_block' => ['credential_tag' => true],
                    'expanded' => true,
                    'multiple' => true
                ]);
        }
    }

Can you help me on this matter, not sure how to resolve it because it worked 3-4 months ago and after update it stopped working.

Regards

@kristijanhusak
Copy link
Owner

@ikostic what exactly is not working? Do you have any exception, something's missing, or what?

@ikostic
Copy link

ikostic commented Sep 23, 2015

Parent form values are populated from model but child form values are not populated. Checkboxes are empty. No errors, no exceptions, simply checkboxes are not checked.

@kristijanhusak
Copy link
Owner

@ikostic ok, i'll check it out and let you know. Thanks for reporting.

@kristijanhusak
Copy link
Owner

@ikostic I tested it out, it works ok here.
I guess Credentials are not populated. Can you paste data from $user_arr that you pass when creating form?
Also what is returned from AdminCredentialQuery::create()->find() could be useful.

Edit: Did you updated from 1.5 or before? You need to rename default_value to value in all forms and fields. I noticed you are using some custom options in forms, so i believe you have created custom field, or modified views. There is a notice about the update here and in the Changelog

@ikostic
Copy link

ikostic commented Sep 23, 2015

$user_arr:

Array
(
[Id] => 1
[Name] => Igor Kostic
[Login] => ikostic
[Email] => igorkg@gmail.com
[Status] => APPROVED
[LanguageId] => 1
[Credentials] => Array
(
[1] => Array
(
[read] => read
[write] => write
)

        [2] => Array
            (
                [read] => read
                [write] => write
            )

        [3] => Array
            (
                [read] => read
                [write] => write
            )

        [4] => Array
            (
                [read] => read
                [write] => write
            )

        [5] => Array
            (
                [read] => read
                [write] => write
            )

        [6] => Array
            (
                [read] => read
                [write] => write
            )

        [7] => Array
            (
                [read] => read
                [write] => write
            )

        [8] => Array
            (
                [read] => read
                [write] => write
            )

        [9] => Array
            (
                [read] => read
                [write] => write
            )

        [10] => Array
            (
                [read] => read
                [write] => write
            )

        [11] => Array
            (
                [read] => read
                [write] => write
            )
    )

)

AdminCredentialQuery::create()->find() is returning object:

[data:protected] => Array
(
[0] => Something\AdminCredential Object
(
[new:protected] =>
[deleted:protected] =>
[modifiedColumns:protected] => Array
(
)

                [virtualColumns:protected] => Array
                    (
                    )

                [id:protected] => 1
                [group_id:protected] => 1
                [name:protected] => ac_manage
                [title:protected] => USERS
                [sequence:protected] => 10
                [created_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-28 20:38:42.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [updated_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-28 20:38:42.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [aAdminCredentialGroup:protected] => 
                [collAdminUserCredentials:protected] => 
                [collAdminUserCredentialsPartial:protected] => 
                [alreadyInSave:protected] => 
                [adminUserCredentialsScheduledForDeletion:protected] => 
            )

        [1] => Something\AdminCredential Object
            (
                [new:protected] => 
                [deleted:protected] => 
                [modifiedColumns:protected] => Array
                    (
                    )

                [virtualColumns:protected] => Array
                    (
                    )

                [id:protected] => 2
                [group_id:protected] => 1
                [name:protected] => translation
                [title:protected] => TRANSLATIONS
                [sequence:protected] => 20
                [created_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-28 20:53:54.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [updated_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-28 20:53:54.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [aAdminCredentialGroup:protected] => 
                [collAdminUserCredentials:protected] => 
                [collAdminUserCredentialsPartial:protected] => 
                [alreadyInSave:protected] => 
                [adminUserCredentialsScheduledForDeletion:protected] => 
            )

        [2] => Something\AdminCredential Object
            (
                [new:protected] => 
                [deleted:protected] => 
                [modifiedColumns:protected] => Array
                    (
                    )

                [virtualColumns:protected] => Array
                    (
                    )

                [id:protected] => 3
                [group_id:protected] => 2
                [name:protected] => api_test
                [title:protected] => API_TEST
                [sequence:protected] => 100
                [created_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-30 15:54:59.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [updated_at:protected] => DateTime Object
                    (
                        [date] => 2015-04-30 15:54:59.000000
                        [timezone_type] => 3
                        [timezone] => UTC
                    )

                [aAdminCredentialGroup:protected] => 
                [collAdminUserCredentials:protected] => 
                [collAdminUserCredentialsPartial:protected] => 
                [alreadyInSave:protected] => 
                [adminUserCredentialsScheduledForDeletion:protected] => 
            )

I was developing this code during month may. In that moment everything worked ok and after that i didn't change anything on the code or database.

Thanks

@kristijanhusak
Copy link
Owner

@ikostic if you published the views, check them out and replace all default_value with value.

@ikostic
Copy link

ikostic commented Sep 23, 2015

You are right. I have checked form field templates in resources folder and replaced default_value with value. Now functionality is working ok.

Thanks on your support!!!

Regards,

Igor Kostic

@kristijanhusak
Copy link
Owner

@tormit when you give a name to a child form, what do you expect to get as a name in fields of those child form? For example, in your case when you do not give name to a child form, you will get something like User[email] or Company[name]. What you expect to happen when you name it?

@tormit
Copy link

tormit commented Sep 24, 2015

I expect the field name to be like parent_form_name[child_form_name][field_name].
Also deeper nesting would be appreciated parent_form_name[child_form_name][child_child_form_name][...][field_name] in case child form itself has an child form and so on.

@kristijanhusak
Copy link
Owner

@tormit I pushed a fix to a master branch. I would really appreciate if you could test it with dev-master to see if it's working.

@kristijanhusak
Copy link
Owner

@tormit This is released in 1.6.31.

@tormit
Copy link

tormit commented Nov 17, 2015

OK. I'll check it out soon.
I'm sorry that I haven't got time to give comprehensive feedback on this issue, though there were still some problems with it.

@kristijanhusak
Copy link
Owner

@tormit what is the status of this issue?

@tormit
Copy link

tormit commented Jan 11, 2016

Passing model seems to be working now.
But, it is passed as array. It needs to be original object. For example when using Laravel Form Builder with Laravel-Translatable(https://github.com/dimsav/laravel-translatable), the translation form expects model as an instance.

@kristijanhusak
Copy link
Owner

I already explained problematic in #184 . You can temporarily pass the model with data and use it how you need.

@tormit
Copy link

tormit commented Jan 11, 2016

When testing with latest dev version, model is passed as object and everything seems to be working.

@kristijanhusak
Copy link
Owner

@tormit it works when u pass it as data or normally?

@tormit
Copy link

tormit commented Jan 11, 2016

When passing normally as model.

Like this:

$this->add(
    $locale,
    'form',
    [
        'class' => $this->getFormBuilder()->create(ContentFormTranslation::class, ['model' => $this->getModel()->translate($locale)]),
    ]
);

@kristijanhusak
Copy link
Owner

Great! I'm closing this than. Thanks for reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants