您需要编写一个自定义验证器,假设要验证的属性是一个数组,并将另一个验证器的结果应用于其每个元素。
我使用这个验证器作为起点;在稍微清理和尽可能简化之后,我得到了这个用于服务器端验证的代码:
protected function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
if (!is_array($value) ) {
$this->addError($object, $attribute, "TODO: error message");
return;
}
else if ($this->isEmpty($value)) {
if (!$this->allowEmpty) {
$this->addError($object, $attribute, "TODO: error message");
}
return;
}
// $this->validator and $this->parameters are supposed to be
// attributes of your custom validator class that you set from
// inside rules().
$validator = self::createValidator(
$this->validator,
$object,
array($attribute),
$this->parameters);
$errors = array();
// Iterate over $value, validating each item in turn.
// Since $validator may be a filtering validator, we need to make
// sure that any changes it makes to the validated item stick, so
// we iterate by reference and do a bit of back and forth shuffling.
foreach($value as $key => &$item) {
$object->$attribute = $item; // the only way
$validator->validateAttribute($object, $attribute);
$item = $object->$attribute; // make any changes stick
if ($object->hasErrors($attribute)) {
$errors[$key] = $object->gerErrors($attribute);
$object->clearErrors($attribute);
}
}
unset($item); // always a good idea after foreach by reference
$object->$attribute = $value; // undo the damage
// And now decide what to do with $errors, which is per-item.
// This should be good:
foreach ($errors as $key => $error) {
$object->addError("$attribute[$key]", $error);
}
}
好的,但是客户端验证呢?看来这应该是可能的,但我还没有测试过:
public function clientValidateAttribute($object,$attribute)
{
// Since this is copy/pasted from above, it's an obvious candidate
// for refactoring into a private method. I 'm keeping it simple.
$validator = self::createValidator(
$this->validator,
$object,
array($attribute),
$this->parameters);
$js = '';
// No need for filtering support here (I think...)
foreach($value as $key => $item) {
$object->$attribute = $item;
$js .= $validator->clientValidateAttribute($object, $attribute);
}
return $js;
}