如何验证下拉列表选择

时间:2012-09-21 15:03:08

标签: cakephp select validation

我有一个包含多个输入和下拉选择的表单:

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>"
));

从用户到另一个用户有不同的选择。

我试图验证它:

"data[Post][project_id]" => array(
  array(
    "rule" => "notEmpty",
    "message" => "Veuillez choisir un projet",
    "allowEmpty" => false
  )
)

但是,似乎没有效果。我哪里错了?

1 个答案:

答案 0 :(得分:2)

假设将从帖子模型本身保存,您必须创建如下表单:

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>"
));

不需要属性名称,第一个参数是字段的名称 id 的定义。会产生这个:

<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
        ),
    ),
);

希望这有帮助。