0

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

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

但是,似乎不起作用。我哪里错了?

4

1 回答 1

2

假设将从Post模型本身保存,您必须像这样创建表单:

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

并验证您的Post模型:

public $validate = array(
    'project_id' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Veuillez choisir un projet',
            'allowEmpty' => false
        ),
    ),
);

希望这可以帮助。

于 2012-09-21T16:42:51.823 回答