如何验证下拉列表选择 [英] How to validate a dropdown select

查看:132
本文介绍了如何验证下拉列表选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

希望这有帮助。

这篇关于如何验证下拉列表选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆