コード例 #1
0
 /**
  * @see sfValidatorBase
  */
 protected function doClean($values)
 {
     if (null === $values) {
         $values = array();
     }
     if (!is_array($values)) {
         throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
     }
     $clean = array();
     $unused = array_keys($this->fields);
     $errorSchema = new sfValidatorErrorSchema($this);
     // check that post_max_size has not been reached
     if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > $this->getBytes(ini_get('post_max_size')) && ini_get('post_max_size') != 0) {
         $errorSchema->addError(new sfValidatorError($this, 'post_max_size'));
         throw $errorSchema;
     }
     // pre validator
     try {
         $values = $this->preClean($values);
     } catch (sfValidatorErrorSchema $e) {
         $errorSchema->addErrors($e);
     } catch (sfValidatorError $e) {
         $errorSchema->addError($e);
     }
     // validate given values
     foreach ($values as $name => $value) {
         // field exists in our schema?
         if (!array_key_exists($name, $this->fields)) {
             if (!$this->options['allow_extra_fields']) {
                 $errorSchema->addError(new sfValidatorError($this, 'extra_fields', array('field' => $name)));
             } else {
                 if (!$this->options['filter_extra_fields']) {
                     $clean[$name] = $value;
                 }
             }
             continue;
         }
         unset($unused[array_search($name, $unused, true)]);
         // validate value
         try {
             $clean[$name] = $this->fields[$name]->clean($value);
         } catch (sfValidatorError $e) {
             $clean[$name] = null;
             $errorSchema->addError($e, (string) $name);
         } catch (Exception $e) {
             $class = get_class($e);
             throw new $class($e->getMessage() . ' of "' . $name . '" field');
         }
     }
     // are non given values required?
     foreach ($unused as $name) {
         // validate value
         try {
             $clean[$name] = $this->fields[$name]->clean(null);
         } catch (sfValidatorError $e) {
             $clean[$name] = null;
             $errorSchema->addError($e, (string) $name);
         }
     }
     // post validator
     try {
         $clean = $this->postClean($clean);
     } catch (sfValidatorErrorSchema $e) {
         $errorSchema->addErrors($e);
     } catch (sfValidatorError $e) {
         $errorSchema->addError($e);
     }
     if (count($errorSchema)) {
         throw $errorSchema;
     }
     return $clean;
 }
コード例 #2
0
 /**
  * @see sfValidatorBase
  */
 protected function doClean($values)
 {
     if (is_null($values)) {
         $values = array();
     }
     if (!is_array($values)) {
         throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
     }
     $clean = array();
     $unused = array_keys($this->fields);
     $errorSchema = new sfValidatorErrorSchema($this);
     // pre validator
     try {
         $this->preClean($values);
     } catch (sfValidatorErrorSchema $e) {
         $errorSchema->addErrors($e);
     } catch (sfValidatorError $e) {
         $errorSchema->addError($e);
     }
     // validate given values
     foreach ($values as $name => $value) {
         // field exists in our schema?
         if (!array_key_exists($name, $this->fields)) {
             if (!$this->options['allow_extra_fields']) {
                 $errorSchema->addError(new sfValidatorError($this, 'extra_fields', array('field' => $name)));
             } else {
                 if (!$this->options['filter_extra_fields']) {
                     $clean[$name] = $value;
                 }
             }
             continue;
         }
         unset($unused[array_search($name, $unused, true)]);
         // validate value
         try {
             $clean[$name] = $this->fields[$name]->clean($value);
         } catch (sfValidatorError $e) {
             $clean[$name] = null;
             $errorSchema->addError($e, (string) $name);
         }
     }
     // are non given values required?
     foreach ($unused as $name) {
         // validate value
         try {
             $clean[$name] = $this->fields[$name]->clean(null);
         } catch (sfValidatorError $e) {
             $clean[$name] = null;
             $errorSchema->addError($e, (string) $name);
         }
     }
     // post validator
     try {
         $clean = $this->postClean($clean);
     } catch (sfValidatorErrorSchema $e) {
         $errorSchema->addErrors($e);
     } catch (sfValidatorError $e) {
         $errorSchema->addError($e);
     }
     if (count($errorSchema)) {
         throw $errorSchema;
     }
     return $clean;
 }
コード例 #3
0
$es1->addError($e1, 'e1');
$es1->addError($e2, 'e2');
$es2 = new sfValidatorErrorSchema($v1);
$es2->addError($e1);
$es2->addError($e1, 'e1');
$es2->addError($es1, 'e2');
$es->addError($es2, 'e2');
$t->is($es->getCode(), 'max_length e1 [max_length] e2 [max_length max_length e1 [max_length max_length] e2 [min_length max_length e1 [max_length] e2 [min_length]]]', '->addError() adds an error to the error schema');
// ->addErrors()
$t->diag('->addErrors()');
$es1 = new sfValidatorErrorSchema($v1);
$es1->addError($e1);
$es1->addError($e1, 0);
$es1->addError($e2, '1');
$es = new sfValidatorErrorSchema($v1);
$es->addErrors($es1);
$t->is($es->getGlobalErrors(), array($e1), '->addErrors() adds an array of errors to the current error');
$t->is($es->getNamedErrors(), array(0 => $e1, '1' => $e2), '->addErrors() merges a sfValidatorErrorSchema to the current error');
// ->getGlobalErrors()
$t->diag('->getGlobalErrors()');
$e = new sfValidatorErrorSchema($v1);
$e->addError($e1);
$e->addError($e2, 'e2');
$e->addError($e1, '2');
$t->is($e->getGlobalErrors(), array($e1), '->getGlobalErrors() returns all globals/non named errors');
// ->getNamedErrors()
$t->diag('->getNamedErrors()');
$t->is($e->getNamedErrors(), array('e2' => $e2, '2' => $e1), '->getNamedErrors() returns all named errors');
// ->getValue()
$t->diag('->getValue()');
$t->is($e->getValue(), null, '->getValue() always returns null');
コード例 #4
0
 /**
  * @see sfValidatorBase
  */
 protected function doClean($values)
 {
     if (is_null($values)) {
         $values = array();
     }
     if (!is_array($values)) {
         throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
     }
     $c_field = $this->getOption('control_field');
     $c_value = isset($values[$c_field]) ? $values[$c_field] : null;
     $validator_schema = $this->getOption('validator_schema') ? $this->getOption('validator_schema') : null;
     $validator_schema->setOption('allow_extra_fields', true);
     foreach ($validator_schema->getFields() as $field_name => $validator) {
         $validator_schema[$field_name]->setOption('required', true);
     }
     $boolVal = new sfValidatorBoolean();
     $errorSchema = new sfValidatorErrorSchema($this);
     try {
         if ($this->getOption('callback')) {
             $values[$c_field] = call_user_func($this->getOption('callback'), $c_value);
         } else {
             $values[$c_field] = $boolVal->clean($c_value);
         }
     } catch (sfValidatorError $e) {
         throw new sfValidatorErrorSchema($this, array($c_field => $e));
     }
     if (!$values[$c_field]) {
         return $values;
     } else {
         try {
             $clean = $validator_schema->clean($values);
         } catch (sfValidatorErrorSchema $e) {
             $errorSchema->addErrors($e);
         } catch (sfValidatorError $e) {
             $errorSchema->addError($e);
         }
         if (count($errorSchema)) {
             throw $errorSchema;
         }
     }
     return $values;
 }