在yii-user模块中创建一个唯一字段

时间:2014-10-17 11:20:05

标签: php yii yii-components

嗨,现在我正在使用Yii用户模块。我是新手,遇到了一些问题。我在这里创建user/profileField/create的agentKey个人资料字段。现在我想让它变得与众不同。我需要改变以使其独特并在不满足条件时抛出错误。

以下是配置文件模型中的规则。

public function rules()
{
    if (!$this->_rules) {
        $required = array();
        $numerical = array();
        $float = array();       
        $decimal = array();
        $rules = array();

        $model=$this->getFields();

        foreach ($model as $field) {
            $field_rule = array();
            if ($field->required==ProfileField::REQUIRED_YES_NOT_SHOW_REG||$field->required==ProfileField::REQUIRED_YES_SHOW_REG)
                array_push($required,$field->varname);
            if ($field->field_type=='FLOAT')
                array_push($float,$field->varname);
            if ($field->field_type=='DECIMAL')
                array_push($decimal,$field->varname);
            if ($field->field_type=='INTEGER')
                array_push($numerical,$field->varname);
            if ($field->field_type=='VARCHAR'||$field->field_type=='TEXT') {
                $field_rule = array($field->varname, 'length', 'max'=>$field->field_size, 'min' => $field->field_size_min);
                if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                array_push($rules,$field_rule);
            }
            if ($field->other_validator) {
                if (strpos($field->other_validator,'{')===0) {
                    $validator = (array)CJavaScript::jsonDecode($field->other_validator);
                    foreach ($validator as $name=>$val) {
                        $field_rule = array($field->varname, $name);
                        $field_rule = array_merge($field_rule,(array)$validator[$name]);
                        if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                        array_push($rules,$field_rule);
                    }
                } else {
                    $field_rule = array($field->varname, $field->other_validator);
                    if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                    array_push($rules,$field_rule);
                }
            } elseif ($field->field_type=='DATE') {
                $field_rule = array($field->varname, 'type', 'type' => 'date', 'dateFormat' => 'yyyy-mm-dd', 'allowEmpty'=>true);
                if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                array_push($rules,$field_rule);
            }
            if ($field->match) {
                $field_rule = array($field->varname, 'match', 'pattern' => $field->match);
                if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                array_push($rules,$field_rule);
            }
            if ($field->range) {
                $field_rule = array($field->varname, 'in', 'range' => self::rangeRules($field->range));
                if ($field->error_message) $field_rule['message'] = UserModule::t($field->error_message);
                array_push($rules,$field_rule);
            }
        }

        array_push($rules,array(implode(',',$required), 'required'));
        array_push($rules,array(implode(',',$numerical), 'numerical', 'integerOnly'=>true));
        array_push($rules,array(implode(',',$float), 'type', 'type'=>'float'));
        array_push($rules,array(implode(',',$decimal), 'match', 'pattern' => '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'));
        $this->_rules = $rules;
    }
    return $this->_rules;
}

1 个答案:

答案 0 :(得分:0)

这通常是afaik(你需要修改以适应你的功能/模块)

array('field_name','unique','message'=>'Error message in case of duplicate value entered'),

另外看看这些链接,它可能对你有用,

http://www.yiiframework.com/extension/unique-attributes-validator/

http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/