I'm using Kohana 3.3 and trying to write a custom validation rule to ensure that users username and e-mail address are unique. I'm following the instructions from an SO question here, and the Kohana documentation here, but whenever I try to add in array(array($this, 'unique_email'))
I get syntax error, unexpected '$this' (T_VARIABLE), expecting ')'
.
If I put array(array('Model_User', 'unique_email'))
I don't get any errors, but why would using $this
cause an error? For completeness I've posted the full class below.
class Model_User extends ORM {
protected $_rules = array(
'email' => array(
array(array($this, 'unique_email')),
)
);
public function unique_email()
{
return TRUE;
}
}
When declaring class properties, you can only use constant values.
See: http://php.net/manual/en/language.oop5.properties.php
So you can't use $this
when first declaring your class property.
You can use $this
in the constructor. So you could do something like this:
public function __construct() {
$this->_rules['email'] = array(
array(array($this, 'unique_email'))
);
}
Edit: kingkero points out in the comments that Kohana provides you with a rules() method, which you should probably use instead of the constructor.