-
Notifications
You must be signed in to change notification settings - Fork 296
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
Comments
Is |
i can even use Auth::user() and it is still not workin.... or should i pass a model through the main form explicitly? |
Since Child form is form type like any other, you can pass it as 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 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 |
will give this a try later |
public function buildForm()
{
$this
->add('User', 'form', [
'class' => 'App\Forms\UserForm',
'formOptions' => [],
'value' => $user,
])->add('Company', 'form', [
'class' => 'App\Forms\CompanyForm',
]);
} This way works. |
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',
]);
} |
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. |
Oh, it doesn't? I will check it. Thanks. |
@tormit It's fixed in latest release (1.6.30). Do a composer update and check if it works. Thanks for reporting. |
Still does not work. |
According to your snippet, your child form is not named: $userForm = $this->formBuilder->create('App\Forms\UserForm', ['model' => $user]); |
Yes, but in my real project both forms are named. |
You should mention that. I will check it out. |
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 |
@ikostic what exactly is not working? Do you have any exception, something's missing, or what? |
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. |
@ikostic ok, i'll check it out and let you know. Thanks for reporting. |
@ikostic I tested it out, it works ok here. Edit: Did you updated from |
$user_arr: Array
) AdminCredentialQuery::create()->find() is returning object: [data:protected] => Array
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 |
@ikostic if you published the views, check them out and replace all |
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 |
@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 |
I expect the field name to be like parent_form_name[child_form_name][field_name]. |
@tormit I pushed a fix to a master branch. I would really appreciate if you could test it with |
@tormit This is released in 1.6.31. |
OK. I'll check it out soon. |
@tormit what is the status of this issue? |
Passing model seems to be working now. |
I already explained problematic in #184 . You can temporarily pass the model with |
When testing with latest dev version, model is passed as object and everything seems to be working. |
@tormit it works when u pass it as data or normally? |
When passing normally as model. Like this: $this->add(
$locale,
'form',
[
'class' => $this->getFormBuilder()->create(ContentFormTranslation::class, ['model' => $this->getModel()->translate($locale)]),
]
); |
Great! I'm closing this than. Thanks for reporting. |
How do i pass a model reference to childs forms?
'model'=>$user is not working...form is rendered, but empty
The text was updated successfully, but these errors were encountered: