Saving Append Textbox Value To Database (Yiistrap)

I am trying to set up a form in which the username consists of user input along with a fixed postfix at the end. e.g.

user@dept1

Yiistrap has textFieldControlGroup which allows you to prepend/append a value to the textfield. I noted that this is just for the presentation i.e. view part. The appended value is never saved in the database.

In normal php, it was going to be carried out with concatenation i.e.


user input . postfix

This is how the field looks like in _form in the views.


<?php echo $form->textFieldControlGroup($model, 'username',

    array('help' => 'Enter employee name without department.', 'append'=>'@dept1')); ?>

Kindly guide me how to save the username along with postfix in the database.

I tried the beforeSave() function in the user model. Kindly let me know if it is correct way of doing this.


        public function beforeSave() {

            if(strpos($this->username,'_dept1') === false)

            {

            $this->username = $this->username . "_dept1";

            }

            return parent::beforeSave();

        }

The username column in the database has index ‘UNIQUE’.

unique’ value in the rules in controller applies checks only the textbox username input with the username column without the ‘_dept1’ postfix. An exception is thrown for duplicate entry.

How do i ensure that the username along with the postfix is unique so that a duplicate entry can’t be made.

for that purpose beforeValidate() method comes in hand. use the beforeValidate() method of AR like this


public function beforeValidate()

{

  if(parent::beforeValidate())

  {

    if($this->username!=null)

       $this->username .= '_dept1';

  return true;

  }

  else

  {

   return false;

  }

}

Thanks for the support Ahamed Rifaideen. This is how i achieved it.

I added these two functions to the model.


        public function checkUsername()

        {

            $user = User::model()->find("username = '".trim($this->username."_dept1'"));

            if(!empty($user)){

                $this->addError('username', 'Username already exists! Please choose a different one');

                return true;

            }

        }

        public function beforeSave()

        {

             

                if(strpos($this->username,'_dept1') === false)

                     {

                        $this->username = $this->username . "_dept1";

                     }

                    return parent::beforeSave();

         }

I also added the following to the rules() function in the model.


array('username','checkUsername','on'=>'insert'),

      array('username', 'match','pattern'=>'/^[a-zA-Z][a-zA-Z0-9]*$/', 'message'=>'{attribute} should start with an alphabet and contain only alphabets and numbers.', 'on'=>'insert'),

Also disabled the username textbox in the update scenario.


<?php echo $form->textField($model,'username',array('size'=>20,'maxlength'=>20,'readonly'=>($model->scenario == 'update')? true : false)); ?>

Please suggest a better solution if you have.

try beforeValidate() thats a right choice