我使用的模型不包含属性'国家'因为我通过多对多关系将它保存在关系模型中。当我在视图中创建表单时,我会使用多个select for custom field' countries'。如何在var result = nextWhile(cell, cell_is_something);
上的模型中验证它?
答案 0 :(得分:0)
// protected/extensions/validators/CountryValidator.php
class CountryValidator extends CValidator
{
/**
* @inheritdoc
*/
protected function validateAttribute($object, $attribute)
{
/* @var $object CFormModel */
// for example check exist countryId in db or no
// you can use any other logic
$country = Country::model()->findByPk($object->$attribute);
if (null != $country) {
$object->addError($attribute, 'country not found');
}
}
...
// in your model
public function rules()
{
return array(
array('countryId', 'ext.validators.CountryValidator'),
...
// in config
'import' => array(
'ext.validators.*',
...
使用方法:
$yourModel = new YourModel();
$yourModel->countryId = -1;
$yourModel->validate();
print_r($yourModel->getErrors()); die();