Skip to content

Instantly share code, notes, and snippets.

@tom--
Created October 1, 2012 14:07
Show Gist options
  • Select an option

  • Save tom--/3811997 to your computer and use it in GitHub Desktop.

Select an option

Save tom--/3811997 to your computer and use it in GitHub Desktop.
Yii model input validation. Checking that at least one of a given set of attributes is set.
<?php
public function rules() {
return array(
array('a', 'needOneOf', 'or' => array('b', 'c', 'd')),
);
}
public function needOneOf($attr, $params) {
array_unshift($params['or'], $attr);
foreach ($params['or'] as $attr) {
if ($this->$attr !== null) {
return;
}
}
$this->addError('summary', Yii::t('app',
'At least one of {attrs} must be set.',
array('{attrs}' => implode('|', $params['or']))
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment