コード例 #1
0
 public function execute($filterChain)
 {
     $context = $this->context;
     if ($this->isFirstCall() && $context->getRequest()->getMethod() == sfRequest::POST) {
         $actionInstance = $this->context->getActionStack()->getLastEntry()->getActionInstance();
         $formcfg = self::getFormConfig($context);
         if ($formcfg === null) {
             $edit = $actionInstance->getRequestParameter('edit');
             $apikey = $context->getRequest()->hasParameter('af_apikey');
             if (!is_array($edit) || !$apikey) {
                 // Normal AJAX POST requests and plain forms don't have
                 // validators from the XML config.
                 $validators = array();
                 $formcfg = array();
             } else {
                 self::renderErrors(array(), 'The form is outdated. Please, refresh it.');
             }
         } else {
             $validators = $formcfg['validators'];
             $fileTypes = $formcfg['fileTypes'];
         }
         if (ArrayUtil::get($formcfg, 'wizard', false)) {
             afWizard::checkStepOrRenderError();
         }
         $errors = array();
         $errorMessage = null;
         $requestParameters = sfToolkit13::arrayDeepMerge($context->getRequest()->getParameterHolder()->getAll(), $context->getRequest()->getFiles());
         foreach ($validators as $field => $fieldValidators) {
             $tmp_field = $field;
             if ($fileTypes[$field] == 'combo') {
                 $tmp_field = substr($field, 0, -1) . "_value]";
             }
             foreach ($fieldValidators as $class => $args) {
                 $params = ArrayUtil::get($args, 'params', array());
                 $validator = afValidatorFactory::createValidator($class, $params);
                 $value = afValidatorFactory::prepareValue($tmp_field, $validator, $requestParameters);
                 try {
                     $validator->clean($value);
                 } catch (sfValidatorError $e) {
                     $errors[] = array($tmp_field, $e->getMessage());
                 }
             }
         }
         if (!empty($errors)) {
             self::renderErrors($errors, $errorMessage);
         }
         if (ArrayUtil::get($formcfg, 'wizard', false)) {
             $this->updateWizardState();
         }
         self::removeIterationNumber($this->context->getRequest()->getParameterHolder());
     }
     return $filterChain->execute();
 }
コード例 #2
0
{
    global $t;
    try {
        $validator->clean($value);
        $t->fail('expected: ' . $expectedMsg);
    } catch (sfValidatorError $e) {
        $t->is($e->getMessage(), $expectedMsg);
    }
}
$validator = afValidatorFactory::createValidator('requiredValidator', array('required_error' => 'myRequiredError'));
$t->is($validator->clean('full'), 'full');
assertError($validator, '', 'myRequiredError');
$validator = afValidatorFactory::createValidator('immValidatorHostname', array());
assertError($validator, '', 'Required.');
$validator = afValidatorFactory::createValidator('immValidatorHostname', array('required_error' => 'myRequiredError'));
assertError($validator, '', 'myRequiredError');
$validator = afValidatorFactory::createValidator('immValidatorHostname', array('required' => 'false'));
$t->is($validator->clean(''), '');
$validator = afValidatorFactory::createValidator('sfValidatorSchemaCompare', array('left_field' => 'password', 'right_field' => 're_password', 'operator' => '==', 'invalid_error' => 'myInvalidError'));
$t->is($validator->getOption('left_field'), 'password');
$t->is($validator->getOption('right_field'), 're_password');
$t->is($validator->getOption('operator'), '==');
assertError($validator, array('password' => 'hello', 're_password' => 'hello2'), 'myInvalidError');
$paramHolder = new sfParameterHolder13();
$paramHolder->set('edit', array(array('password' => 'hello', 're_password' => 'hello2')));
$values = afValidatorFactory::prepareValue('edit[0][re_password]', $validator, $paramHolder);
$t->is($values, array('password' => 'hello', 're_password' => 'hello2'));
$paramHolder = new sfParameterHolder13();
$paramHolder->set('edit', array('password' => 'hello', 're_password' => 'hello2'));
$values = afValidatorFactory::prepareValue('edit[re_password]', $validator, $paramHolder);
$t->is($values, array('password' => 'hello', 're_password' => 'hello2'));