我有一个创建自定义表单字段下拉列表,按年过滤.我想要做的一件事是允许用户按年份过滤,这是默认选项.我将此添加为empty_value.但是,当我渲染表单时,它默认在第一个不是空值的项目上.空值就在那里,就在列表的正上方.如何在页面初始加载的情况下将页面默认设置为"全部"?代码如下.
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class YearType extends AbstractType
{
private $yearChoices;
public function __construct()
{
$thisYear = date('Y');
$startYear = '2012';
$this->yearChoices = range($thisYear, $startYear);
}
public function getDefaultOptions(array $options)
{
return array(
'empty_value' => 'All',
'choices' => $this->yearChoices,
);
}
public function getParent(array $options)
{
return 'choice';
}
public function getName()
{
return 'year';
}
}
Run Code Online (Sandbox Code Playgroud)
我用简单的方式在树枝上渲染我的表格 {{ form_widget(filter_form) }}
gre*_*emo 21
尝试添加empty_data选项null,所以它首先.我有很多这种类型的字段,它正在工作,例如:
class GenderType extends \Symfony\Component\Form\AbstractType
{
public function getDefaultOptions(array $options)
{
return array(
'empty_data' => null,
'empty_value' => "Non specificato",
'choices' => array('m' => 'Uomo', 'f' => 'Donna'),
'required' => false,
);
}
public function getParent(array $options) { return 'choice'; }
public function getName() { return 'gender'; }
}
Run Code Online (Sandbox Code Playgroud)
编辑:另一种可能性(我想)将是设置preferred_choices.这样你就可以获得顶部的"全部"选项.但我不知道它是否可以使用null empty_data,但你可以改变empty_data你想要的任何东西:
public function getDefaultOptions(array $options)
{
return array(
'empty_value' => 'All',
'empty_data' => null,
'choices' => $this->yearChoices,
'preferred_choices' => array(null) // Match empty_data
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21547 次 |
| 最近记录: |