I have created my own Model. And when I wanted to validate a form I created I got
RegisterPublisherForm and its behaviors do not have a method or closure named "getDbConnection".
/var/www/yii/framework/base/CComponent.php(266)
264 if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure)
265 return call_user_func_array($this->$name, $parameters);
266 throw new CException(Yii::t('yii','{class} and its behaviors do not have a method or closure named "{name}".',
267 array('{class}'=>get_class($this), '{name}'=>$name)));
How should I implement a uniqe field validator in a model that is not an Active Record?
please tell me if there is something else that I need to post here in order to get a more focused answer
You have to specify the model name and the attribute name by ‘className’ and ‘attributeName’ when you want to use CUniqueValidator in CFormModel or in another CActiveRecord model.
I’m sorry, but I don’t understand what you mean by that.
I don’t think it makes any difference regarding the validation whether you will use CHtml::activeTextField or CHtml::textField, as long as the massive assignment of the attributes are properly processed.
Just to double check, did you assign a valid CActiveRecord class name other than ‘RegisterPublisherForm’ for ‘className’?
Sorry, but somthing is just wrong… I thought I understood it - but appearantly I didn’t:
I have the ‘RegisterPublisherForm’ Class who extends CModel,
It has it’s validators, one of them is ‘uniqe’… is that a validator that only CActiveRecord can use?
It keep giving me an exception
'Property "RegisterPublisherForm._md" is not defined.'
from
/var/www/yii/framework/db/ar/CActiveRecord.php(379)
the model validation function is always failing. Why would it try to use an ActiveRecord when I don’t want to use it, and I didn’t even extend from it…
just add method in model and use it as a validator. Then put all needed logic in this method:
public function rules()
{
return array(
array('email', 'myUniqueValidator');
);
}
public function myUniqueValidator( $attributeName, $params ) {
if( $this->$atributeName is not unique ) {
$this->addError( $attributeName, 'must be unique!!!' );
}
}
easy as that. just remember that validation method does not need to return anything (especially boolean as validation result). It is just required that you add error with addError method.
So you are building your app with no CActiveRecord models at all … I’m sorry, I couldn’t imagine the possibility of it.
But, it might be a hard way, I’m afraid.
IMO, there’s no need to avoid using CActiveRecord … it is quite handy and do 90% of your database job sufficiently effective. And it’s also very easy to use DAO or plain SQL within CActiveRecord extended class when you really need to achieve good performance.