Yii Framework Forum: Custom Validation Rule Is Not Working For My Cformmodel - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom Validation Rule Is Not Working For My Cformmodel Rate Topic: -----

#1 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 29 October 2013 - 09:49 AM

I am trying to create a custom validation rule which checks if username is already exists in the database.

I dont have a direct access to the database. I have to use the RestClient to communicate with the Database..

My issue is that custom validation rule is not working with my CFormModel

here is my code
    public function rules()
    { 
      return array(

        array('name', 'length', 'max' => 255),
        array('nickname','match','pattern'=> '/^([a-zA-Z0-9_-])+$/' )
        array('nickname','alreadyexists'),  
      );
    }

    public function alreadyexists($attribute, $params)
    {
        $result = ProviderUtil::CheckProviderByNickName($this->nickname);
            if($result==-1)
            {
            $this->addError($attribute,'This Provider handler already exists. Please try with a different one.');
            }
    }

This doesnt seems to work to all, I tried this also to check if the control is coming there,

public function alreadyexists($attribute, $params)
{
            $this->addError($attribute,'This Provider handler already exists. Please try with a different one.');

}


but these too didnt work. Am i doing anything wrong here?
0

#2 User is online   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 29 October 2013 - 10:30 AM

Hi

what is the class ProviderUtil and method CheckProviderByNickName ?

why didn't use the validator 'unique' ?

...
array('nickname','unique'),  

Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 29 October 2013 - 10:33 AM

Can you show the code from your action? It sounds like validation isn't being triggered.

EDIT
Your rules method is wrong, it should be in the following format:

    public function rules()
    {
        return array(
            array(...),
            ...
        );
    }


You're not returning the validation array.
0

#4 User is online   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 29 October 2013 - 11:23 AM

View PostKeith, on 29 October 2013 - 10:33 AM, said:

Can you show the code from your action? It sounds like validation isn't being triggered.

EDIT
Your rules method is wrong, it should be in the following format:

    public function rules()
    {
        return array(
            array(...),
            ...
        );
    }


You're not returning the validation array.


I absolute agree!
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 29 October 2013 - 11:27 AM

View PostKonApaz, on 29 October 2013 - 10:30 AM, said:

Hi

what is the class ProviderUtil and method CheckProviderByNickName ?

why didn't use the validator 'unique' ?

...
array('nickname','unique'),  



ProviderUtil is one of my Component, it is used to communicate with my backend(its in java). i dont have direct access to database since i am restClient. so i cannot use 'unique'.
0

#6 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 29 October 2013 - 11:30 AM

View PostKeith, on 29 October 2013 - 10:33 AM, said:

Can you show the code from your action? It sounds like validation isn't being triggered.

EDIT
Your rules method is wrong, it should be in the following format:

    public function rules()
    {
        return array(
            array(...),
            ...
        );
    }


You're not returning the validation array.


Sorry!! that was my mistake when copyinng code here.. i have corrected it. my all other rules is working. only "alreadyexists" is not working
0

#7 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 29 October 2013 - 12:39 PM

Should return something in the validation code?

public function alreadyexists($attribute, $params)
    {
        $result = ProviderUtil::CheckProviderByNickName($this->nickname);
            if($result==-1)
            {

            $this->addError($attribute,'This Provider handler already exists. Please try with a different one.');
return false;
            }
return true;
    }

0

#8 User is online   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,327
  • Joined: 21-February 11
  • Location:Greece

Posted 29 October 2013 - 02:23 PM

View PostMAHESH.E.U, on 29 October 2013 - 12:39 PM, said:

Should return something in the validation code?

public function alreadyexists($attribute, $params)
    {
        $result = ProviderUtil::CheckProviderByNickName($this->nickname);
         var_dump ($result); //what value will be displayed ??
            if($result==-1)
            {

            $this->addError($attribute,'This Provider handler already exists. Please try with a different one.');
return false;
            }
return true;
    }



check the returned value in result... is it -1 ?
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#9 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 30 October 2013 - 05:50 AM

View PostKonApaz, on 29 October 2013 - 02:23 PM, said:

check the returned value in result... is it -1 ?


Yes, it is -1. even this didnt work.

public function alreadyexists($attribute, $params)
{
            $this->addError($attribute,'This Provider handler already exists. Please try with a different one.');

}

0

#10 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 30 October 2013 - 07:21 AM

Can you post the exact code from your rules() method and from your action?
0

#11 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 04 November 2013 - 08:19 AM

view

$form = $this->beginWidget('CActiveForm', array(
		'id' => 'provider-profile-form',	
		'method' => 'POST',
		'enableClientValidation' => true,
		'clientOptions' => array(
		'validateOnSubmit' => true,
		
		)));

?>

 <div class="grid_8">
                <div class="grid_8"><?php echo $form->labelEx($model, 'name'); ?></div>
                <div class="grid_8"><?php echo $form->textField($model, 'name', array('class' => 'prov_provider_field')); ?>
                <?php echo $form->error($model, 'name'); ?> </div>

                <div class="grid_8"><?php echo $form->labelEx($model, 'nickname'); ?></div>
                <div class="grid_8" style="margin-bottom: 15px;"><?php echo $form->textField($model, 'nickname', array('class' => 'prov_provider_field')); ?>
                <?php echo $form->error($model, 'nickname'); ?> </div>
                <input type="submit"  value="submit">
<?php $this->endwidget(); ?>

</div>
</div>

my model

class ProviderProfileForm extends CFormModel
{
	
	public $name;
	public $nickname;
public function rules()
	{
		return array(
			array('name', 'required', 'message' => 'Business/Individual name cannot be blank'),
			array('name', 'length', 'max' => 255),
			//array('nickname', 'length', 'max' => 25),
			array('nickname','match','pattern'=> '/^([a-zA-Z0-9_-])+$/' ),
		
			array('nickname','alreadyexists','on'=>'insert'),
                           )
       }
public function alreadyexists($attribute, $params)
	{
			$result = ProviderUtil::CheckProviderByNickName($this->nickname);
				if($result==-1)
				{
				$this->addError($attribute,'This Provider handler already exists. Please try with a different one.');
				//console.log("control is here");
				return false;
				}
	}
}


//mycontrolleraction

 public function actionIndex()
	{
	$model = new ProviderProfileForm();
	$model->setScenario('insert');
	$this->render('index',array('model'=>$model));
	}

0

#12 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 04 November 2013 - 10:17 AM

I bet you forgot to set scenario in your create action.
God is real unless declared as integer
0

#13 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 04 November 2013 - 11:52 AM

I suspect that the errors you are seeing are from client validation. Try disabling that while you debug the problem.
0

#14 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 05 November 2013 - 03:07 AM

View PostORey, on 04 November 2013 - 10:17 AM, said:

I bet you forgot to set scenario in your create action.


What do u mean?

Is this not enough ?

$model = new ProviderProfileForm();
        $model->setScenario('insert');

0

#15 User is offline   MAHESH.E.U 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 03-September 11
  • Location:Kerala India

Posted 05 November 2013 - 03:14 AM

View PostKeith, on 04 November 2013 - 11:52 AM, said:

I suspect that the errors you are seeing are from client validation. Try disabling that while you debug the problem.


@Keith .. after disabling client validation , the validation errors are not coming at all..
So do you mean enable client validation will not apply for custom validations ?
0

#16 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,701
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 05 November 2013 - 03:51 AM

View PostMAHESH.E.U, on 05 November 2013 - 03:07 AM, said:

What do u mean?

Is this not enough ?

$model = new ProviderProfileForm();
        $model->setScenario('insert');



I mean I don't see the actual saving code, so if your form is being displayed in actionIndex, and form's action points to, say, actionCreate, you'll have to set scenario in actionCreate too.

But Keith is right, it's all about client validation, because you're probably not submitting the form at all.
God is real unless declared as integer
0

#17 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,670
  • Joined: 04-March 10
  • Location:UK

Posted 05 November 2013 - 04:47 AM

As mentioned above, you seem to be posting back to your index action, and there's no code in there to save the model. Your form will post back to the current action by default unless you override it.
0

#18 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 224
  • Joined: 19-April 13

Posted 07 December 2014 - 02:40 AM

hi
this work :
in view :
$form = $this->beginWidget('CActiveForm',array(
    'id' => 'login-form',
    'enableAjaxValidation'=>TRUE,
    'enableClientValidation'=>true,
    'clientOptions'=>array(
	'validateOnSubmit'=>true,
        
	),
    ));


in action :
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}


View PostMAHESH.E.U, on 04 November 2013 - 08:19 AM, said:

view

$form = $this->beginWidget('CActiveForm', array(
		'id' => 'provider-profile-form',	
		'method' => 'POST',
		'enableClientValidation' => true,
		'clientOptions' => array(
		'validateOnSubmit' => true,
		
		)));

?>

 <div class="grid_8">
                <div class="grid_8"><?php echo $form->labelEx($model, 'name'); ?></div>
                <div class="grid_8"><?php echo $form->textField($model, 'name', array('class' => 'prov_provider_field')); ?>
                <?php echo $form->error($model, 'name'); ?> </div>

                <div class="grid_8"><?php echo $form->labelEx($model, 'nickname'); ?></div>
                <div class="grid_8" style="margin-bottom: 15px;"><?php echo $form->textField($model, 'nickname', array('class' => 'prov_provider_field')); ?>
                <?php echo $form->error($model, 'nickname'); ?> </div>
                <input type="submit"  value="submit">
<?php $this->endwidget(); ?>

</div>
</div>

my model

class ProviderProfileForm extends CFormModel
{
	
	public $name;
	public $nickname;
public function rules()
	{
		return array(
			array('name', 'required', 'message' => 'Business/Individual name cannot be blank'),
			array('name', 'length', 'max' => 255),
			//array('nickname', 'length', 'max' => 25),
			array('nickname','match','pattern'=> '/^([a-zA-Z0-9_-])+$/' ),
		
			array('nickname','alreadyexists','on'=>'insert'),
                           )
       }
public function alreadyexists($attribute, $params)
	{
			$result = ProviderUtil::CheckProviderByNickName($this->nickname);
				if($result==-1)
				{
				$this->addError($attribute,'This Provider handler already exists. Please try with a different one.');
				//console.log("control is here");
				return false;
				}
	}
}


//mycontrolleraction

 public function actionIndex()
	{
	$model = new ProviderProfileForm();
	$model->setScenario('insert');
	$this->render('index',array('model'=>$model));
	}


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users