如何验证下拉列表选择
问题描述:
我有一个包含多个输入和下拉选择的表单:
I have a form with multiple inputs and a dropdown select :
echo $this->Form->input("field",array(
"name" => "data[Post][project_id]",
"options" => $proTab,
"empty" => "Sélectionnez un projet",
"div" => "control-group",
"label" => array(
"class" => "control-label",
"text" => "Projet : "
),
"between" => "<div class='controls'>",
"after" => "</div>"
));
从用户到另一个用户有不同的选项。
Which has different option from an user to another.
我已经尝试验证它:
"data[Post][project_id]" => array(
array(
"rule" => "notEmpty",
"message" => "Veuillez choisir un projet",
"allowEmpty" => false
)
)
但是,似乎不工作。
答
假设将从 Post 模型本身保存,创建如下所示的表单:
Assuming that will be saved from the Post model itself, you must create your form like this:
echo $this->Form->input("project_id", array(
"options" => $proTab,
"empty" => "Sélectionnez un projet",
"div" => "control-group",
"label" => array(
"class" => "control-label",
"text" => "Projet : "
),
"between" => "<div class='controls'>",
"after" => "</div>"
));
不需要属性 name ,第一个参数是字段的名称和 ID 。将生成此:
Do not need the attribute name, the first parameter is the definition of the name and id of field. Will generate this:
<select name="data[Post][project_id]" id="PostProjectId">
并验证您的发布模式:
public $validate = array(
'project_id' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Veuillez choisir un projet',
'allowEmpty' => false
),
),
);
希望这有帮助。