一个蛋糕 php FormHelper 如何设置 select 多个 select 选项?
How can one set selected options for a cake php FormHelper multiple select?
模板表单元素为:
$ingredientOptions = array('Potato','Peanut','Parsley');
echo $this->Form->input('FavouriteIngredients', array('multiple' => true, 'options' => $ingredientOptions);
echo $this->Form->input('AllergicIngredients', array('multiple' => true, 'options' => $ingredientOptions);
这会创建两个(丑陋的)多个 select 框,每个框都有一个由选项数组中的元素组成的列表,none 其中 select 编辑。
为了设置初始值,我在控制器中尝试了一些但没有成功:
$this->set('FavouriteIngredients',array(1,2));
$this->request->data['FavouriteIngredients'] = array(1,2);
这些多个 select 用于客户模型中定义的两个 HABTM 关系:
class Customer extends AppModel {
public $hasAndBelongsToMany = array(
'FavouriteIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_favourite'
);
'AllergicIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_allergic'
);
);
}
实际上生产中的问题是选项数组中不存在这些值。所以这有效:
$this->request->data['Customer']['FavouriteIngredients'] = array(1,2);
模板表单元素为:
$ingredientOptions = array('Potato','Peanut','Parsley');
echo $this->Form->input('FavouriteIngredients', array('multiple' => true, 'options' => $ingredientOptions);
echo $this->Form->input('AllergicIngredients', array('multiple' => true, 'options' => $ingredientOptions);
这会创建两个(丑陋的)多个 select 框,每个框都有一个由选项数组中的元素组成的列表,none 其中 select 编辑。
为了设置初始值,我在控制器中尝试了一些但没有成功:
$this->set('FavouriteIngredients',array(1,2));
$this->request->data['FavouriteIngredients'] = array(1,2);
这些多个 select 用于客户模型中定义的两个 HABTM 关系:
class Customer extends AppModel {
public $hasAndBelongsToMany = array(
'FavouriteIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_favourite'
);
'AllergicIngredients' =>
array(
'className' => 'Ingredient',
'joinTable' => 'customer_ingredients_allergic'
);
);
}
实际上生产中的问题是选项数组中不存在这些值。所以这有效:
$this->request->data['Customer']['FavouriteIngredients'] = array(1,2);