Yii password, repeat password fields

Standard
class User extends CActiveRecord
{

    public $repeat_password;

    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'users';
    }

    public function rules()
    {
        return array(
            array('username', 'required'),
            array('password, repeat_password', 'required', 'on' => 'create'),
            array('password, repeat_password', 'length', 'min' => 6, 'max' => 30, 'on' => array('create', 'update')),
            array('password', 'compare', 'compareAttribute' => 'repeat_password', 'on' => array('create', 'update')),
            array('password, repeat_password', 'length', 'min' => 8),
            array('username', 'length', 'max' => 60),
            array('username', 'unique'),
            array('id, username', 'safe', 'on' => 'search'),
        );
    }

    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'username' => 'Username',
            'password' => 'Password',
        );
    }

    public function search()
    {
        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('username', $this->username, true);
        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function encrypt($parameter)
    {
        return md5($parameter);
    }

    public function beforeSave()
    {
        if (!empty($this->password) && strlen($this->password))
        {
            $this->password = $this->encrypt($this->password);
        } else
        {
            if (empty($this->password))
                $this->password = $this->findByPk($this->id)->password;
        }
        return parent::beforeSave();
    }

}
Advertisements

One thought on “Yii password, repeat password fields

  1. When you make a website, all your web pages are served from the
    server residing somewhere on the internet. Good providers instead
    hire highly trained specialists from their own country to ensure you receive the specialized care you deserve.
    Elements such as text, graphics, images, font sizes and colors are
    used in designing and producing pages for a web site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: