Default values for symfony2 choice radio box

社会主义新天地 提交于 2019-12-12 20:18:31

问题


My project is written using Silex and Symfony Components (i.e. Form component).

I try to create a battery of radio buttons built from a class and I want to preselect one of those radios.

I create form like that:

$form = $app['form.factory']->createBuilder(new QueryFormType(), $query)

where QueryFormType is my custom type (it extends AbstractType) where I define form fields and $query is an entity with default values for the form.

Form fields within QueryFormType look like that (I omitted builder and other fields for clarity):

->add('contractPeriod', 'filter', array(
  'label' => 'Contract Period:',
  'choices' => array(
    '-1' => 'No period',
    '1' => '1 month',
    '2' => '2 months',
    '3' => '3 months',
    '6' => '6 months',
    '12' => '12 months',
    '18' => '18 months'
  ),
  'expanded' => TRUE,
  'multiple' => FALSE
))

$query is an instance of a class that should preselect radio button. In it's constructor you will find:

$this->contractPeriod = '3';

It has correct getters and setters for this property too.

Now. The form and radio buttons are rendered properly. Radio button is preselected to '3' => '3 months'. When I check radio higher in the definition list (so 2, 1 or -1) form accepts it as a value. But when I check value lower in the defined list (6, 12 or 18) it uses default value set in $query. What the heck?

My question is how to set default value correctly so I can pick whatever other value I want after the form is rendered?

I already tried using 'data' property in the form field definition and it is the same story. I can select only lower values but no higher. I tired to use 'preferred_choices' but it does not work the way I would expect (messed up IDs of the inputs).

I beg you for help.

EDIT

This is how I submit the form:

$form->submit($request->query->get($form->getName()), FALSE);

I found out that if I set clearMissing to TRUE (second argument) it works well but I cannot do partial prefilling (or patch the form). Seem to be a bug.

EDIT

It has something to do with those lines inside Form class of symfony components:

if ($isSubmitted || $clearMissing) {
    $child->submit($isSubmitted ? $submittedData[$name] : null, $clearMissing);
    unset($submittedData[$name]);

    if (null !== $this->clickedButton) {
        continue;
    }

    if ($child instanceof ClickableInterface && $child->isClicked()) {
        $this->clickedButton = $child;

        continue;
    }

    if (method_exists($child, 'getClickedButton') && null !== $child->getClickedButton()) {
        $this->clickedButton = $child->getClickedButton();
    }
}

$child->submit updates viewData of its parent (Form in this case) to submited value only if it's lower/equal to default value set in the form. Why?

EDIT

I think that Form component wont submit children after default value that in that case isSubmited == false and clearMissing == false. That makes only values up to default value available. But I don't know.

来源:https://stackoverflow.com/questions/26524795/default-values-for-symfony2-choice-radio-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!