Patrigan
(Patrick Provinciael)
1
To the creator of atleastvalidator
http://www.yiiframework.com/extension/atleastvalidator/
I got an error regarding your code:
Declaration of AtLeastValidator::validate() should be compatible with that of CValidator::validate()
This is using:
PHP 5.3.8
Yii 1.1.9
After checking the code the problem was in the declaration of validate.
Your declaration:
public function validate(CModel $object, $attributes)
Official declaration:
public function validate($object, $attributes=null)
There are some other problems, but these could be mine to fix. I will add to this if I find something else.
DarkNSF
2
I fixed it just now, I think I made some other changes. I think you just need to fix the function signature for validate()
<?php
/**
* AtLeastValidator
*
**/
class AtLeastValidator extends CValidator
{
/** match value */
public $matchValue = null;
/** @var string */
protected $defaultMessage = 'Fill at least one of the following attributes: {attributes}';
/**
* @param CModel $object
* @param array $attributes
*/
public function validate($object, $attributes=null)
{
parent::validate($object, $attributes);
$object->onAfterValidate = array($this, 'afterValidate');
}
/**
* @param CModel $object
* @param string $attribute
*/
public function validateAttribute($object, $attribute)
{
}
public function afterValidate($event)
{
/** @var $sender CModel */
$sender = $event->sender;
$valid = false;
foreach($this->attributes as $attribute)
{
$value = $sender->$attribute;
if ($value != null && $value != '' && $value != '0')
{
if ($matchValue == null || $value == $matchValue)
{
$valid = true;
break;
}
}
}
if (!$valid)
{
$message = empty($this->message) ? Yii::t('application', $this->defaultMessage) : $this->message;
$this->addError($sender, $this->attributes[0], $message, array('{attributes}' => implode(', ', $this->attributes)));
}
}
}
Clem
(Duclik)
3
Fonctional validator:
<?php
/**
* AtLeastValidator
*
**/
class AtLeastValidator extends CValidator
{
/** match value */
public $matchValue = null;
/** @var string */
protected $defaultMessage = 'Fill at least one of the following attributes: {attributesLabel}';
/**
* @param CModel $object
* @param array $attributes
*/
public function validate($object, $attributes=null)
{
parent::validate($object, $attributes);
$object->onAfterValidate = array($this, 'afterValidate');
}
/**
* @param CModel $object
* @param string $attribute
*/
public function validateAttribute($object, $attribute)
{
}
public function afterValidate($event)
{
/** @var $sender CModel */
$sender = $event->sender;
$valid = false;
foreach($this->attributes as $attribute)
{
$value = $sender->$attribute;
if ($value != null && $value != '')
{
$valid = true;
break;
}
}
if (!$valid)
{
$attributesLabel = array();
foreach($this->attributes as $attribute) {
$attributesLabel[] = $sender->getAttributeLabel($attribute);
}
$message = empty($this->message) ? Yii::t('app', $this->defaultMessage) : $this->message;
$this->addError($sender, $this->attributes[0], $message, array('{attributesLabel}' => implode(', ', $attributesLabel)));
}
}
}
NabiKAZ
(Nabikaz)
4
Thanks for great extension.
I need client side of this rule, Do you can provide the clientValidate version?