Validating

Hi Guys,

Thanks for reading this.

I want to make validate one attribute but I need to show/clear the error on another attributed

here is the code so you can understand what I need:

In the model:

array(‘showMsg’,‘myValidator’),

public function myValidator($attribute,$params)

{

if($this->$attribute=="Yes")


{


	if($this->bdate=="")


	{


		$this->addError('BirthDayDate', 'Can't say Happy Birthday');


	}


}

}

Look here I want to set the erro message to BirthDayDate not the showMsg, am I am not getting my error message :(

I guess 4 things to try:

[list=1]

[*]Correct here ([font="Courier New"]$this->attribute[/font] instead of [font="Courier New"]$this->[color="#FF0000"]$[/color]attribute[/font]) :


if($this->$attribute=="Yes")

[*]You should escape the single quote inside your string like this


$this->addError('BirthDayDate', 'Can\'t say Happy Birthday');

or of course, double-quote your string, and leave your single quote as it is

[*]If it’s not enough, remove all the ifs in your validation function, and see if it works. If it’s the case, check back what you’d have removed for errors

[*]If nothing works, please post your model and form

[/list]

if($this->$attribute=="Yes") - is right as I passed $attribute as parameter and want to use its value.

$attribute="bDate"

then " $this->$attribute " means $this->bDate

but

$this->attribute is different.

Note that when you write


array('showMsg','myValidator'),

in your model’s validation rules, that means

$attribute = ‘showMsg’,

$params = null.

Can you rephrase, and post your model rules and your view?

Thanks!

Here I attached my model and the view.

Please check my validator:

array(‘boption’,‘bValidator’),

function for this validator

public function bValidator($attribute,$params)

{

if($this->$attribute=="Yes")

	{


		$this->addError('bdate','BirthDay');


	}


}

I told you in my first post [font="Courier New"]$this->attribute[/font], I guess that was wrong. Please add the last line in the code below, and also try the alternate syntax [font="Courier New"]$this[$attribute][/font] instead of [font="Courier New"]$this->$attribute[/font]


public function bValidator($attribute,$params)

{

    if($this[$attribute]=="Yes") // alternate syntax if $this->$attribute doesn't work

    {

        $this->addError('bdate','BirthDay');

    }

    return false; // that's important

}

Code not working… :(

I think this is problem for invoking rather than current attribute.

No, there must be some error somewhere. I use custom validators in my projects and I output errors where I like, and they work very well.

Which Yii version? I have also create and using custom validators, but don’t get error on others attributes. Can you share me your files?

Thanks!

Hmmm I’ve just downloaded the codes you’ve put (I’d have preferred inline code :slight_smile:

You have to make these changes:

_form.php


…

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(

  'model'=>$model,

  'attribute'=>'bdate',

  'name'=>'Test[bdate]', //addError('bdate',…) looks for a field named "Test[bdate]"

  …

Test.php


…

  public function bValidator($attribute,$params)

  {

    if($this[$attribute]=="Yes")

    {

      $this->addError('bdate','BirthDay');

    }

    return false;

  }

…

Edit: You may want also try with normal input field instead of CJuiDatePicker. In that case, you don’t have to make that kind of change in _form.php, but you need the change(s) in your model anyway (see also my last post).

Thanks for your reply. Still not working.

In firebug I see that it pass jason value as Test_bdate : "Birthday"

http://www.yiiframework.com/forum/index.php/topic/24292-update-multiple-attributes-on-client-validation/page__p__117866#entry117866

please check this image

I don’t understand what you’re trying to achieve. Are we talking about the same issue you had in the beginning?

Yes the same thing (clear/add more than a single attribute’s messages on ajax validation). But I give an image so member can understand what I need. Thanks!