要处理动态添加的值,请使用选择类型的'choice_loader' 选项。 It's new in symfony 2.7,遗憾的是根本没有任何文档。
基本上它是一个实现ChoiceLoaderInterface 的服务,它定义了三个功能:
-
loadValuesForChoices(array $choices, $value = null)
-
loadChoiceList($value = null)
-
loadChoicesForValues(array $values, $value = null)
现在的想法是在选择加载器中保留ArrayChoiceList 作为私有属性。在构建表单loadValuesForChoices(...) 被调用,这里我们将所有预设选项添加到我们的选项列表中,以便它们可以显示给用户。在构建视图 loadChoiceList(...) 被调用,但我们不加载任何东西,我们只是返回我们之前创建的私有选择列表。
现在用户与表单交互,一些额外的选择通过自动完成加载并放入 HTML。在提交表单时,选择的值被提交,并且在我们的控制器操作中首先创建表单,然后在$form->handleRequest(..) 上调用loadChoicesForValues(...),但提交的值可能与开头包含的值完全不同。因此,我们将内部选择列表替换为仅包含提交值的新选择列表。
我们的表单现在完美地保存了自动完成添加的数据。
棘手的部分是,每当我们使用表单类型时,我们都需要一个新的选择加载器实例,否则内部选择列表将包含所有选择。
由于目标是编写一个新的自动完成选择类型,您通常会使用依赖注入将您的选择加载器传递给类型服务。
但是对于类型,如果您总是需要一个新实例,这是不可能的,相反我们必须通过选项包含它。在默认选项中设置选择加载器不起作用,因为它们也被缓存了。要解决这个问题,您必须编写一个匿名函数,该函数需要将选项作为参数:
$resolver->setDefaults(array(
'choice_loader' => function (Options $options) {
return AutocompleteFactory::createChoiceLoader();
},
));
编辑:
这是选择加载器类的简化版本:
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
class AutocompleteChoiceLoader implements ChoiceLoaderInterface
{
/** @var ChoiceListInterface */
private $choiceList;
public function loadValuesForChoices(array $choices, $value = null)
{
// is called on form creat with $choices containing the preset of the bound entity
$values = array();
foreach ($choices as $key => $choice) {
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value
if (is_callable($value)) {
$values[$key] = (string)call_user_func($value, $choice, $key);
}
else {
$values[$key] = $choice;
}
}
// this has to be done by yourself: array( label => value )
$labeledValues = MyLabelService::getLabels($values);
// create internal choice list from loaded values
$this->choiceList = new ArrayChoiceList($labeledValues, $value);
return $values;
}
public function loadChoiceList($value = null)
{
// is called on form view create after loadValuesForChoices of form create
if ($this->choiceList instanceof ChoiceListInterface) {
return $this->choiceList;
}
// if no values preset yet return empty list
$this->choiceList = new ArrayChoiceList(array(), $value);
return $this->choiceList;
}
public function loadChoicesForValues(array $values, $value = null)
{
// is called on form submit after loadValuesForChoices of form create and loadChoiceList of form view create
$choices = array();
foreach ($values as $key => $val) {
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value
if (is_callable($value)) {
$choices[$key] = (string)call_user_func($value, $val, $key);
}
else {
$choices[$key] = $val;
}
}
// this has to be done by yourself: array( label => value )
$labeledValues = MyLabelService::getLabels($values);
// reset internal choice list
$this->choiceList = new ArrayChoiceList($labeledValues, $value);
return $choices;
}
}