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

Multiple checkboxes in plain subform get double [][] #324

Open
rudiedirkx opened this issue Feb 14, 2017 · 3 comments
Open

Multiple checkboxes in plain subform get double [][] #324

rudiedirkx opened this issue Feb 14, 2017 · 3 comments
Labels

Comments

@rudiedirkx
Copy link
Collaborator

My form simplified:

// Prepare subform
$taco_test = $this->formBuilder->plain();
$taco_test->add('professions', 'choice', [
  'label' => trans('report.taco_test.professions'),
  'choices' => $favorites_options,
  'multiple' => TRUE,
  'expanded' => TRUE,
]);

// Add subform to parent
$this->add('taco_test', 'form', [
  'label' => trans('report.taco_test.taco_test'),
  'class' => $taco_test, // the plain form object
]);

So the checkboxes live at form.taco_test.professions, so a single checkbox should be at eg. form.taco_test.professions.66.

The resulting checkboxes have name="taco_test[professions][][]", with a double [][]. Their values are correct.

I know the last [] are added in ChoiceType::buildCheckableChildren(), but maybe it's the first [] that's actually wrong. I've no idea where that comes from.

I'm using 1.6.50 and I'm having some dependency problems, so I can't update in this context to try newer version, but the ChoiceType code seems to be the same in HEAD.

@rudiedirkx
Copy link
Collaborator Author

rudiedirkx commented Feb 14, 2017

If I dd($taco_test); before adding it to the parent form, I can see all checkboxes' names:

#name: "professions[]"

That [] comes from buildCheckableChildren() and I think it's correct, so it seems another is added after it... << That doesn't seem to be true, but I really don't get it.

@rudiedirkx
Copy link
Collaborator Author

Temporary local 'fix':

  public function buildForm() {
    // ...
    $this->fixChildFormCheckboxes($this->taco_test->professions);
  }

  protected function fixChildFormCheckboxes(FormField $field) {
    foreach ($field->getChildren() as $child) {
      $child->setName($field->getName());
    }
  }

because form.taco_test.professions's name is correct: taco_test[professions][]. The children shouldn't all get an extra [].

@rudiedirkx
Copy link
Collaborator Author

Still broken, but too complex for me. Work-around: explicit setName() after every plain form creation:

$career_choice_test = $this->formBuilder->plain()->setName('career_choice_test');

and in a subform:

'class' => $this->formBuilder->plain()->setName("career_choice_test[$name]")

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

No branches or pull requests

1 participant