| | 1 | <?php |
| | 2 | /** |
| | 3 | * sfValidatorSchemaAndOr validates if all validators (each one associated |
| | 4 | * with different fields) passes, or any of them (it depends on the logic |
| | 5 | * specified in the options). |
| | 6 | * |
| | 7 | * @author JoeZ <jzarate@gmail.com> |
| | 8 | * @version SVN: $Id$ |
| | 9 | */ |
| | 10 | class sfValidatorSchemaAndOr extends sfValidatorSchema |
| | 11 | { |
| | 12 | const LOGIC_AND = 'AND'; |
| | 13 | const LOGIC_OR = 'OR'; |
| | 14 | |
| | 15 | protected |
| | 16 | $field_validator_array = array(); |
| | 17 | |
| | 18 | /** |
| | 19 | * Constructor. |
| | 20 | * |
| | 21 | * The first argument can be: |
| | 22 | * |
| | 23 | * * null |
| | 24 | * * an associative array |
| | 25 | * with field names as keys and sfValidatorBase instances as values |
| | 26 | * |
| | 27 | * @param mixed $validators Initial field=>validator array |
| | 28 | * @param array $options An array of options |
| | 29 | * @param array $messages An array of error messages |
| | 30 | * |
| | 31 | */ |
| | 32 | public function __construct($field_validator_array = null,$options = array(), $messages = array()) |
| | 33 | { |
| | 34 | if(is_array($field_validator_array)) |
| | 35 | { |
| | 36 | foreach($field_validator_array as $field => $validator) |
| | 37 | { |
| | 38 | $this->field_validator_array[$field] = $validator; |
| | 39 | } |
| | 40 | } |
| | 41 | else if(!is_null($field_validator_array)) |
| | 42 | { |
| | 43 | throw new InvalidArgumentException('sfValidatorSchemaAndOr constructor takes a field=>sfValidatorBase array.'); |
| | 44 | } |
| | 45 | parent::__construct($options,$messages); |
| | 46 | } |
| | 47 | |
| | 48 | /** |
| | 49 | * Configures the current validator. |
| | 50 | * |
| | 51 | * Available options: |
| | 52 | * |
| | 53 | * * logic: Logic to use "AND" , "OR" |
| | 54 | * |
| | 55 | * @param array $options An array of optiones |
| | 56 | * @param array $messages An array of error messages |
| | 57 | * |
| | 58 | */ |
| | 59 | protected function configure($options = array(),$messages = array()) |
| | 60 | { |
| | 61 | $this->addOption('logic_to_use',sfValidatorSchemaAndOr::LOGIC_AND); |
| | 62 | $this->addMessage('enabled', 'ennnnabbb'); |
| | 63 | $this->setMessage('enabled','sisisisisisi'); |
| | 64 | } |
| | 65 | |
| | 66 | protected function doClean($values) |
| | 67 | { |
| | 68 | $clean = array(); |
| | 69 | $valid = true; |
| | 70 | $errors = array(); |
| | 71 | foreach($this->field_validator_array as $field => $validator) |
| | 72 | { |
| | 73 | try |
| | 74 | { |
| | 75 | $clean[$values[$field]] = $validator->clean($values[$field]); |
| | 76 | } |
| | 77 | catch(sfValidatorError $e) |
| | 78 | { |
| | 79 | $errors[$field] = $e; |
| | 80 | throw $e; |
| | 81 | } |
| | 82 | } |
| | 83 | throw new sfValidatorError($this,'invalid'); |
| | 84 | if( (count($errors) && $this->getOption('logic_to_use') == sfValidatorSchemaAndOr::LOGIC_AND) || |
| | 85 | (count($errors) == count($this->field_validator_array) && $this->getOption('logic_to_use') == sfValidatorSchemaAndOr::LOGIC_OR)) |
| | 86 | { |
| | 87 | throw new sfValidatorErrorSchema($this,array_values($errors)); |
| | 88 | } |
| | 89 | return $values; |
| | 90 | } |
| | 91 | |
| | 92 | } |
| | 93 | No newline at end of file |