Я создал пользовательский тип формы «статус», расширив поле выбора (following the tutorial in the cookbook). Выбор поля выбора автоматически заполняется из параметров, которые вводятся через службу. Однако я хотел бы настроить это поле статуса для использования простого или расширенного набора параметров, либо простого списка статуса (неактивный, живой), либо более полного списка статуса (неактивные, живые, удаленные, удаленные).Symfony2 - Изменить выбор в пользовательском поле Тип изнутри formBuilder
Мой код ниже успешно изменяет $options['choices']
на основе моих $ options ['customOptions'], но эти изменения не влияют на конечные параметры поля выбора, которые отображаются.
Как вы можете изменить параметры поля choice
с помощью формыBuilder?
parameters.yml
parameters:
gutensite_component.options.status:
0: Inactive
1: Live
gutensite_component.options.status_preview:
2: "Preview Only"
3: "Live Only (delete on sync)"
gutensite_component.options.status_delete:
-1: "Delete (soft)"
-2: Deleted
services.yml
services:
gutensite_component.form.type.status:
class: Gutensite\ComponentBundle\Form\Type\StatusType
arguments:
- "%gutensite_component.options.status%"
- "%gutensite_component.options.status_preview%"
- "%gutensite_component.options.status_delete%"
tags:
- { name: form.type, alias: "status" }
Форма Тип
class StatusType extends AbstractType
{
/**
* $statusChoices are passed in from services.yml that initiates this as a global service
* @var array
*/
private $statusChoices;
private $statusChoicesPreview;
private $statusChoicesDelete;
public function __construct(array $statusChoices, array $statusChoicesPreview,
array $statusChoicesDelete) {
$this->statusChoices = $statusChoices;
$this->statusChoicesPreview = $statusChoicesPreview;
$this->statusChoicesDelete = $statusChoicesDelete;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'customOptions' => array(),
// By default it will use the simple choices
'choices' => $this->statusChoices
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Set extra choices based on customOptions
if(!empty($options['customOptions']['showPreview'])) {
$options['choices'] = $options['choices'] + $this->staftusChoicesPreview;
}
if(!empty($options['customOptions']['showDelete'])) {
$options['choices'] = $options['choices'] + $this->statusChoicesDelete;
}
// The $options['choices'] ARE successfully modified and passed to the parent, but
// have no effect on the options used in the form display
// Include Regular Choice buildForm
parent::buildForm($builder, $options);
}
public function getParent() {
return 'choice';
}
public function getName() {
return 'status';
}
}
контроллер
$builder->add('status', 'status', array(
'label' => 'Status',
'required' => TRUE,
'customOptions' => array(
'showPreview' => true
)
));
Для люди, которые все еще ищут ответ и приземляются здесь, используют загрузчики выбора: http://stackoverflow.co m/questions/35456199/symfony-2-8-dynamic-choicetype-options –