我正在尝试为我的配置模块制作一些输入复选框,但选择小部件不起作用。
所以它只适用于单选按钮和菜单,而对于多个菜单和复选框我有一个错误:
给定“数组”、“字符串”类型的预期参数
编码:
$builder->add('value', 'choice', array
(
'label' => $this->item->getTitle() . ':',
'choices' => array('1' => 'one', '2' => 'two'),
'multiple' => true,
'expanded' => true,
)
);
这是一个例子。
编辑:
因此,在我的配置包中,我有很多不同类型的输入来更好地描述配置值,对于每一个,我都会呈现正确的小部件。我对所有小部件都没有任何问题,除了用于选择小部件的复选框和菜单多个,收音机和菜单工作正常。
这是表单类:
class ItemType extends AbstractType {
/**
* The configuration item (one row's data)
* @var Item
*/
private $item;
/**
* Widget type
* @var string
*/
private $widget;
/**
* widget's options
* @var array
*/
private $options = array();
/**
* Class constructor
*
* @param Item $item The configuration item (one row's data)
* @return void
*/
public function __construct(Item $item, $widget, $options = array())
{
$this->item = $item;
$this->widget = $widget;
$this->options = $options;
}
/**
* Builds the form
*
* @param FormBuilder $builder The form builder
* @param array $options The options
* @return void
*
* @see Symfony\Component\Form\AbstractType::buildForm()
*/
public function buildForm(FormBuilder $builder, array $options)
{
$fieldOptions = array('attr' => array('class' => 'text'));
switch($this->widget)
{
case 'menu':
case 'MultipleMenu':
case 'radio':
case 'checkbox':
$choices = $this->getWidgetOptions($this->options['values'], ':');
if($this->widget == 'radio')
{
$fieldOptions = array('attr' => array('class' => 'configurationFormRadioButton'));
}
$builder->add('value', 'choice', array_merge(
$fieldOptions,
$this->getChoiceOption($this->widget),
array
(
'label' => $this->item->getTitle() . ':',
'choices' => $choices
)
)
);
break;
case 'text':
case 'password':
case 'email':
case 'url':
case 'number':
case 'integer':
case 'textarea':
$builder->add('value', $this->widget, array_merge($fieldOptions, array('label' => $this->item->getTitle() . ':')));
break;
case 'date':
$builder->add('value', $this->widget, array_merge(
array(
'attr' => array('class' => 'date'),
'label' => $this->item->getTitle() . ':',
'input' => 'string',
'widget' => 'single_text'
),
$this->getWidgetOptions($this->options['options'], '=')
)
);
break;
case 'datetime':
$builder->add('value', $this->widget, array_merge(
array(
'attr' => array('class' => 'date'),
'label' => $this->item->getTitle() . ':',
'input' => 'string',
'date_widget' => 'choice',
'time_widget' => 'choice'
),
$this->getWidgetOptions($this->options['options'], '=')
)
);
break;
case 'time':
$builder->add('value', $this->widget, array_merge(
array(
'attr' => array('class' => 'date'),
'label' => $this->item->getTitle() . ':',
'input' => 'string',
'widget' => 'choice'
),
$this->getWidgetOptions($this->options['options'], '=')
)
);
break;
case 'datepicker':
$builder->add('value', 'text', array_merge(
array(
'attr' => array('class' => 'datepicker text'),
'label' => $this->item->getTitle() . ':'
),
$this->getWidgetOptions($this->options['options'], '=')
)
);
break;
}
$builder->setData($this->item);
}
/**
* Returns the name of this type
* @return string
*/
public function getName()
{
return 'configurationItemForm';
}
/**
* Set widget type for field
* @return array
*/
private function getChoiceOption($inputType)
{
switch($inputType)
{
case 'menu':
return array('multiple' => false, 'expanded' => false);
break;
case 'MultipleMenu':
return array('multiple' => true, 'expanded' => false);
break;
case 'radio':
return array('multiple' => false, 'expanded' => true);
break;
case 'checkbox':
return array('multiple' => true, 'expanded' => true);
break;
}
}
/**
* Get widget options from database
* @return array
*/
private function getWidgetOptions($from, $explodeChar)
{
$options = array();
if($from != '')
{
$pairs = explode('|', $from);
foreach($pairs as $pair)
{
$values[] = explode($explodeChar, $pair);
foreach($values as $value)
{
$options[$value[0]] = $value[1];
}
}
}
return $options;
}
}