cakephp:模型验证规则,表单字段,===

时间:2012-03-01 06:43:23

标签: php validation cakephp field models

在研究了Miles Jones蛋糕论坛插件之后,我有几个问题:

1)每个字段(在模型的验证规则中出现)是否必须是数据库表中的字段?我在cupcake论坛插件的User模型中找到了以下验证规则。 oldPassword和newPassword不是users表中的字段。我很困惑因为'我认为我应该只为表的字段制定验证规则。

public $validate = array(
    'username' => array(
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => 'That username has already been taken',
            'on' => 'create'
        ),
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter a username'
        )
    ),
    'password' => array(
        'rule' => 'notEmpty',
        'message' => 'Please enter a password'
    ),
    'oldPassword' => array(
        'rule' => array('isPassword'),
        'message' => 'The old password did not match'
    ),
    'newPassword' => array(
        'isMatch' => array(
            'rule' => array('isMatch', 'confirmPassword'),
            'message' => 'The passwords did not match'
        ),
        'custom' => array(
            'rule' => array('custom', '/^[-_a-zA-Z0-9]+$/'),
            'message' => 'Your password may only be alphanumeric'
        ),
        'between' => array(
            'rule' => array('between', 6, 20),
            'message' => 'Your password must be 6-20 characters in length'
        ),
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter a password'
        )
    ),
    'email' => array(
        'isUnique' => array(
            'rule' => 'isUnique',
            'message' => 'That email has already been taken',
            'on' => 'create'
        ),
        'email' => array(
            //'rule' => array('email', true),//boolean true as second parameter verifies that the host for the address is valid -- to be uncommented once website is uploaded
            'rule' => array('email'),
            'message' => 'Your email is invalid'
        ),
        'notEmpty' => array(
            'rule' => 'notEmpty',
            'message' => 'Your email is required'
        )
    )
);

2)每个表单字段是否必须是数据库表中的字段?

例如,当我要求用户注册时,会有:用户名,电子邮件地址,密码和确认密码。但确认密码字段不需要是表中的字段吗? 这是一个好习惯吗?

我在form_app_model.php中找到了以下isMatch函数:

/**
 * Validates two inputs against each other
 * @access public
 * @param array $data
 * @param string $confirmField
 * @return boolean
 */
public function isMatch($data, $confirmField) {
    $data = array_values($data);
    $var1 = $data[0];
    $var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : '';

    return ($var1 === $var2);
}

有人可以告诉我上面代码的最后一行是什么===

谢谢。

2 个答案:

答案 0 :(得分:2)

这意味着完全相同(没有类型转换)。例如:如果y = 25,则y === 25为真且y =='25'为真,但y ==='25'不为真。

答案 1 :(得分:0)