2012-09-21 55 views
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 
) 
) 

但是,似乎不工作。我錯在哪裏?

回答

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

希望這有助於。

+0

謝謝,似乎工作得很好:) – Axiol

相關問題