Yii Framework Forum: Pass return value from function to custom validation rule parameter - Yii Framework Forum

Jump to content

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

Pass return value from function to custom validation rule parameter Rate Topic: -----

#1 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 21 June 2012 - 12:12 PM

Hi,

I wrote a custom validation rule to which I am trying to pass some parameters.

One of the parameters should be a return value from a function. However the value does not get passed along.

If I call the same function within the custom rule then I can get the return value. It just doesn't work for me to set a parameter using this technique…

Here is the code I am using:

// Within the rules:
array('someAttribute', 'customValidation', 'parameter'=>$this->someFunction(), 'anotherParameter'=>'someValue'),
 

// The function which should return something...
public function someFunction()
{
	if ($this->someOtherAttribute == 1)
   		return 2;
	return 1;
}
 

// The custom rule:
public function customValidation($attribute, $params)
{
        print_r($params);
        echo "function " . $this->someFunction();
        DIE();
}

/* The above customValidation prints:
* 
* array (
*    [parameter]=>
*    [anotherParameter]=>someValue
* )
*
* function 1
*/

0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 21 June 2012 - 01:21 PM

are you sure it's not work? pls double-check.

here is what i got by copy & paste your code:

Array
(
    [parameter] => 1
    [anotherParameter] => someValue
)
function:1

I enjoy the Yii.sy coding life here.
0

#3 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 21 June 2012 - 01:48 PM

View Postrootbear, on 21 June 2012 - 01:21 PM, said:

are you sure it's not work? pls double-check.

here is what i got by copy & paste your code:

Array
(
    [parameter] => 1
    [anotherParameter] => someValue
)
function:1



Really? That's strange...
My model is extending CFormModel. Dou you think this would make a difference?
0

#4 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 21 June 2012 - 03:18 PM

i also tested in my contactform model
I enjoy the Yii.sy coding life here.
0

#5 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 21 June 2012 - 03:19 PM

View Postrootbear, on 21 June 2012 - 03:18 PM, said:

i also tested in my contactform model


Can you post your model please? :)
0

#6 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 21 June 2012 - 04:24 PM

sure,

protected/models/FeedbackForm.php
<?php
/**
 * FeedbackForm class.
 * FeedbackForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class FeedbackForm extends CFormModel
{
	public $name;
	public $email;
	public $subject;
	public $body;
	public $verifyCode;
	
	
	public $someAttribute;
	public $someOtherAttribute;

	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			array('name, email, subject, body', 'required'),
			array('email', 'email'),
			array('someAttribute', 'customValidation', 'parameter'=>$this->someFunction(), 'anotherParameter'=>'someValue'),
		);
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
		return array(
			'verifyCode'=>'Verification Code',
		);
	}
	
	public function someFunction()
	{
			if ($this->someOtherAttribute == 1)
					return 2;
			return 1;
	}
	 

	// The custom rule:
	public function customValidation($attribute, $params)
	{
			echo "<pre>";
			print_r($params);
			echo "function:" . $this->someFunction();
			echo "</pre>";
			DIE();
	}
}

I enjoy the Yii.sy coding life here.
0

#7 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 22 June 2012 - 08:07 AM

View Postrootbear, on 21 June 2012 - 04:24 PM, said:

sure,

protected/models/FeedbackForm.php
...
	public function someFunction()
	{
			if ($this->someOtherAttribute == 1)
					return 2;
			return 1;
	}}

...


Could you try to access a public variable in someFunction()? Maybe that is where my problem is. I am trying to get a value which has been set through a POST request.
So simply try to return $this->name or $this->email in someFunction():
public function someFunction()
	{
			if ($this->someOtherAttribute == 1)
					return $this->name;
			return $this->email;
	}}

Does this work for you?

What does print_r($params); show you?
0

#8 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 22 June 2012 - 09:55 AM

for test, hardcode some values:

public $name = "hardcode-name";
public $email = "hardcode-email";


then:

Array
(
    [parameter] => hardcode-email
    [anotherParameter] => someValue
)
function:[value from post]


so i guess your issue resolved, at the moment

public function rules()
{
	return array(
	//~~~
		array('someAttribute', 'customValidation', 'parameter'=>$this->someFunction(), 'anotherParameter'=>'someValue'),
	);
}


function rules runs ahead of processing post request, and function customValidation runs after model receives the post parameters.

so it's all about the sequence. now instead passing [parameter] from rule definition, read it inside validation function.
I enjoy the Yii.sy coding life here.
1

#9 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 22 June 2012 - 10:21 AM

Yes, this has been my workaround so far. Since I can not set the paramater in the rules() method I am reading it manually in the customValidation.

Thanks for your help rootbear! Really appreciate it :)
0

#10 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 22 June 2012 - 10:26 AM

ur welcome, i'm learning from it too. sometimes the issue is not 'important' per say, since we could always find out a workaround. however, getting a clear picture worth more than that.
I enjoy the Yii.sy coding life here.
0

#11 User is offline   Nisha Haridas 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-August 12

Posted 14 January 2013 - 12:35 AM

View Postjrn, on 22 June 2012 - 10:21 AM, said:

Yes, this has been my workaround so far. Since I can not set the paramater in the rules() method I am reading it manually in the customValidation.


How did you read the values in custom validation?
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