[ASK]How to check the duplicate username?

In my project, i have a USER table structured like this:

IDUSER int, pk,

NAME varchar,

USERNAME varchar,

PASSWORD varchar.

I'm using the RBAC method to manage this table. How to add the check button in the create user page, and the function to check that the new username is already exist or not? Thx.

In your model of your User table you can specify a rule

array('NAME', 'unique'),

Quote

In your model of your User table you can specify a rule

array('NAME', 'unique'),

It's works, thx.

But how to add an error message? Thx.

What does your rules() method look like?

I already fix it Mr.Qiang.  :)

Now i would like to add the error message if the username already exist. How to add this error message? ???

In your view, you can use CHtml::errorSummary() or CHtml::error() to display validation errors. Please check the following cookbook tutorial on how to customize error messages:

http://www.yiiframew…doc/cookbook/1/

I’ll try. thank’s… ;D

I create the register.php like this:



<h2>Register</h2>





<?php echo $this->renderPartial('reg', array(


	'user'=>$user,


	'update'=>false,


)); ?>


the reg.php:



<div class="yiiForm">





<p>


Fields with <span class="required">*</span> are required.


</p>





<?php echo CHtml::beginForm(); ?>





<?php echo CHtml::errorSummary($user); ?>





<div class="simple">


<?php echo CHtml::activeLabelEx($user,'IDROLE'); ?>


<?php echo CHtml::activeDropDownList($user,'IDROLE',User::model()->statOpt); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($user,'NAMA'); ?>


<?php echo CHtml::activeTextField($user,'NAMA',array('size'=>20,'maxlength'=>20)); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($user,'EMAIL'); ?>


<?php echo CHtml::activeTextField($user,'EMAIL',array('size'=>30,'maxlength'=>30)); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($user,'USERNAME'); ?>


<?php echo CHtml::activeTextField($user,'USERNAME',array('size'=>15,'maxlength'=>15)); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($user,'PASSWORD'); ?>


<?php echo CHtml::activePasswordField($user,'PASSWORD',array('size'=>15,'maxlength'=>15)); ?>


</div>


<div class="simple">


<?php echo CHtml::activeLabelEx($user,'WEBSITE'); ?>


<?php echo CHtml::activeTextField($user,'WEBSITE',array('size'=>50,'maxlength'=>50)); ?>


</div>





<div class="action">


<?php echo CHtml::submitButton($update ? 'Save' : 'Create'); ?>


</div>





<?php echo CHtml::endForm(); ?>





</div><!-- yiiForm -->


and modify the usercontroller:



public function actionRegister()


	{


		$user=new User;


		if(isset($_POST['User']))


		{


			$user->attributes=$_POST['User'];


			if($user->save())


				$this->redirect(array('success','id'=>$user->IDUSER));


		}


                $this->render('register',array('user'=>$user));


	}


But why the error message doesn't appears in the register form?

But in the create form it's work…

can anybody help me?  :-\

Did you run validator() before save()?

Quote

Did you run validator() before save()?

I already do  that… :(

Somebody have a suggestion code maybe?

Are you sure there are errors ? try doing a var_dump($user->getErrors());

Can you paste your rules() and tell us what is supposed to be a error ?

This is my rules code:



public function rules()


	{


		return array(


                        array('IDROLE,NAMA,EMAIL,USERNAME,PASSWORD','required'),


			array('USERNAME','unique',


                            'message'=>'username is already exist, please change'),


                        array('NAMA','length','max'=>20),


			array('EMAIL','email'),


			array('USERNAME','length','max'=>15),


			array('PASSWORD','length','max'=>15),


			array('WEBSITE','length','max'=>50),


			array('IDROLE', 'in', 'range'=>array(1, 2, 3)),


                 );


	}


I found the error. With my current code, the Chtml::errorSummary() returning error only if i already login. But when i not login, there's no error message. How to fix it? Please help. thx.

Is there nobody who can help me to fix this problems?  ???

Do you mean when not logged in, even if you didn't enter anything in the form, there is still not error message? What action are you executing in this case? Still actionRegister?

Quote

Do you mean when not logged in, even if you didn't enter anything in the form, there is still not error message?

Yes, this is that I mean.

I still executing the actionRegister class. But even i try to executing the Create User class (I already given permission to all user * to execute the create user class), this problem still happening.

I've tried something like this with no-registered users, the problem was solved just bypassing the action in filters()

like

filters()

array(

    …rbacfilter  -register -login

)

hope it helps

Quote

I've tried something like this with no-registered users, the problem was solved just bypassing the action in filters()

like

filters()

array(

    …rbacfilter  -register -login

)

hope it helps

can you make it more detail? i would like to try this path… :)

sure, just to point my case:

1st - I’m using the extension RBAC -> http://www.yiiframew…opic,905.0.html

2nd - I've tried to create an AJAX autocomplete with CAutoComplete in a view. All the code was fine, but the result was never returned to the ajax caller.

3rd - I realized that I didn't have permission to execute the action in the controller. So, I used the bypass feature of RBAC, which is meant to avoid permission checks over one or more actions.

My Controller:

class ArtistController extends CController

{

/**


 * @return array action filters


 */


public function filters()


{


	return array(


		array(

/here I tell Yii to use the permission system I told you above and tell it that no permission is required to perform the autoCompleteLookup action./

			&#039;application.filters.RbacFilter -autoCompleteLookup&#039;,


		),


	);


}

public function actionAutoCompleteLookup()

    {

      if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))

      {

…here I recover all rows and send to the caller

          echo $returnVal;

      }

    }

note that the -autoCompleteLookup option in my filters() action tells the RBAC extension not to check permissions over the specified action, so the action can be called by any user, even the guest

You could try to remove the filters() and accessRules() from you controller just to check if there's something wrong with permissions. If so, try to bypass the actions that don't need permission checks.

please, try this hint and let us know what happens, ok?

regards