Ziggi
(Ziggi)
January 16, 2013, 5:43pm
1
Well,
We may use the filter validators in models this way:
public function rules()
{
return array
(
array('myAttribute', 'filter', 'filter' => array($this, 'someFunction')),
}
}
public function someFunction($attribute)
{
return 'Ho ho ho ' . $attribute;
}
This is working well presuming ‘someFunction’ is defined within the model class. But how to use a ‘shared’ library of filter functions - e.g. function stored in a helper file?
Any ideas? - Thanks ahead!
rootbear
(Alex Xm)
January 16, 2013, 6:31pm
2
would behavior concept fits here?
Ziggi
(Ziggi)
January 16, 2013, 9:32pm
3
Perhaps, I need to improve my uderstanding of this concept first
rootbear
(Alex Xm)
January 16, 2013, 10:46pm
4
class extends functions vertically;
behavior reuse functions horizontally;
bivan
(Ivan Buttinoni)
July 11, 2013, 12:59pm
5
Ziggi:
Well,
We may use the filter validators in models this way:
public function rules()
{
return array
(
array('myAttribute', 'filter', 'filter' => array($this, 'someFunction')),
}
}
public function someFunction($attribute)
{
return 'Ho ho ho ' . $attribute;
}
This is working well presuming ‘someFunction’ is defined within the model class. But how to use a ‘shared’ library of filter functions - e.g. function stored in a helper file?
Any ideas? - Thanks ahead!
A possible solution
Define the library as class usting static methods
// An example class with static method
class MyClass {
public static function myCallbackMethod($param) {
return $param . 'Hello World!';
}
}
Then define the rule:
array('myAttribute', 'filter', 'filter' => array('MyClass', 'myCallbackMethod')),
Consider also the possibility to use anonymous function (PHP >= 5.3):
array('myAttribute', 'filter', 'filter' => function($param) {return $param . 'Hello World!';} ),