protected function setConfigWidget()
 {
     sfContext::getInstance()->getConfiguration()->loadHelpers(array('Escaping'));
     $application = $this->memberApplication->getApplication();
     $settings = $application->getSettings();
     foreach ($settings as $key => $setting) {
         $param = array();
         $choices = array();
         $validatorBool = new sfValidatorBoolean();
         $param['IsRequired'] = $validatorBool->clean($setting['required']);
         $param['Caption'] = sfOutputEscaper::escape(sfConfig::get('sf_escaping_method'), $setting['displayName']);
         if (empty($setting['datatype']) || $setting['datatype'] == 'HIDDEN') {
             continue;
         }
         switch ($setting['datatype']) {
             case 'BOOL':
                 $param['FormType'] = 'radio';
                 $choices = array('1' => 'Yes', '0' => 'No');
                 break;
             case 'ENUM':
                 $param['FormType'] = 'select';
                 $enumValues = array();
                 if (!is_array($setting['enumValues'])) {
                     continue;
                 }
                 foreach ($setting['enumValues'] as $value) {
                     $enumValues[$value['value']] = $value['displayValue'];
                 }
                 $choices = $enumValues;
                 break;
             default:
                 $param['FormType'] = 'input';
                 $param['ValueType'] = '';
         }
         $this->widgetSchema[$key] = opFormItemGenerator::generateWidget($param, $choices);
         $this->validatorSchema[$key] = opFormItemGenerator::generateValidator($param, array_keys($choices));
         if ($setting['defaultValue']) {
             $this->setDefault($key, $setting['defaultValue']);
         }
     }
     $userSettings = $this->memberApplication->getUserSettings();
     foreach ($userSettings as $name => $value) {
         if (!empty($value)) {
             $this->setDefault($name, $value);
         }
     }
 }
 /**
  * @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;
 }
 /**
  * Cleans the input value.
  *
  * @param  mixed $value  The input value
  * @return mixed The cleaned value
  * @throws sfValidatorError
  */
 protected function doClean($value)
 {
     $polls = $this->getOption('poll_items');
     $poll_app = 'apoll_settings_available_polls_' . $value;
     // checks that the item exists
     if (!isset($polls[$value])) {
         throw new sfValidatorError($this, 'form_field', array('poll' => $poll_app));
     }
     $poll = $polls[$value];
     // checks if form field is defined
     if (!isset($poll['form'])) {
         throw new sfValidatorError($this, 'form_field', array('poll' => $poll_app));
     }
     $form = $poll['form'];
     // checks if form class is instantiated
     if (!class_exists($form)) {
         throw new sfValidatorError($this, 'form_class', array('poll' => $poll_app, 'form' => $form));
     }
     // checks if form class is based on aPollBaseForm
     $object = new $form();
     if (!$object instanceof aPollBaseForm) {
         throw new sfValidatorError($this, 'form_extends', array('poll' => $poll_app, 'form' => $form));
     }
     // checks if view_template has been defined and if the file exist
     if (isset($poll['view_template'])) {
         if (!$this->checkTemplate($poll, 'view_template')) {
             throw new sfValidatorError($this, 'view_template', array('partial' => $poll['view_template']));
         }
     }
     // checks if submit_action has been defined and if the action exist
     if (isset($poll['submit_action'])) {
         $list = $this->getModuleAndAction($poll['submit_action']);
         $controller = sfContext::getInstance()->getController();
         if (!$controller->actionExists($list['module'], $list['action'])) {
             throw new sfValidatorError($this, 'submit_action', array('action' => $poll['submit_action']));
         }
     }
     // checks if view_template has been defined and if the file exist
     if (isset($poll['submit_success_template'])) {
         if (!$this->checkTemplate($poll, 'submit_success_template')) {
             throw new sfValidatorError($this, 'submit_success_template', array('template' => $poll['submit_success_template']));
         }
     }
     // checks if the send_notification contains true or false
     if (isset($poll['send_notification'])) {
         $val = new sfValidatorBoolean();
         try {
             $val->clean($poll['send_notification']);
         } catch (Exception $exc) {
             throw new sfValidatorError($this, 'send_notification');
         }
     }
     // checks if the send_to field defins a valid email or a valid user
     if (isset($poll['send_to'])) {
         $what = aPollToolkit::isUserOrEmail($poll['send_to']);
         if (!in_array($what, array('user', 'email'))) {
             throw new sfValidatorError($this, 'send_email', array('field' => 'send_to', 'global_field' => 'to'));
         }
     }
     // checks if the send_from field defins a valid email or a valid user
     if (isset($poll['send_from'])) {
         $what = aPollToolkit::isUserOrEmail($poll['send_from']);
         if (!in_array($what, array('user', 'email'))) {
             throw new sfValidatorError($this, 'send_email', array('field' => 'send_from', 'global_field' => 'from'));
         }
     }
     // checks if email_title_partial has been defined and if the file exist
     if (isset($poll['email_title_partial'])) {
         if (!$this->checkTemplate($poll, 'email_title_partial')) {
             throw new sfValidatorError($this, 'email_title', array('template' => $poll['email_title_partial']));
         }
     }
     // checks if email_body_partial has been defined and if the file exist
     if (isset($poll['email_body_partial'])) {
         if (!$this->checkTemplate($poll, 'email_body_partial')) {
             throw new sfValidatorError($this, 'email_body', array('template' => $poll['email_body_partial']));
         }
     }
     // checks if emil stylesheets are correctly defined
     if (isset($poll['email_stylesheets'])) {
         $ss = $poll['email_stylesheets'];
         // the definition is in the right format?
         if (!(false === $ss || is_array($ss) || is_string($ss))) {
             throw new sfValidatorError($this, 'email_stylesheets_error');
         }
         // if defined with a single string.
         if (is_string($ss)) {
             if (false === aPollToolkit::getStylesheetPath($ss)) {
                 throw new sfValidatorError($this, 'email_stylesheets_items', array('stylesheet' => $ss));
             }
         }
         // if defined as an array of reports. ~ means default reports and is accepted without further checks
         if (is_array($ss)) {
             $wrong = array();
             foreach ($ss as $stylesheet) {
                 if (false === aPollToolkit::getStylesheetPath($stylesheet)) {
                     $wrong[] = $stylesheet;
                 }
             }
             if (count($wrong)) {
                 throw new sfValidatorError($this, 'email_stylesheets_items', array('stylesheet' => implode(', ', $wrong)));
             }
         }
     }
     // checks if allow_multiple_submissions is defined and if the values are right
     if (isset($poll['captcha_do_display'])) {
         if (!($poll['captcha_do_display'] === true) || $poll['captcha_do_display'] === false) {
             throw new sfValidatorError($this, 'captcha_display');
         }
     }
     // checks if reports is correctly defined
     if (isset($poll['reports'])) {
         $r = $poll['reports'];
         // the definition is in the right format?
         if (!(false === $r || is_array($r) || is_string($r))) {
             throw new sfValidatorError($this, 'reports_error');
         }
         // if defined with a single string. ~ means default reports and is accepted without further checks
         if (is_string($r) && '~' !== $r) {
             if (false === aPollToolkit::getReportSettings($r)) {
                 throw new sfValidatorError($this, 'reports_items', array('reports' => $r));
             }
         }
         // if defined as an array of reports. ~ means default reports and is accepted without further checks
         if (is_array($r)) {
             $wrong = array();
             foreach ($r as $report) {
                 if ('~' !== $report && false === aPollToolkit::getReportSettings($report)) {
                     $wrong[] = $report;
                 }
             }
             if (count($wrong)) {
                 throw new sfValidatorError($this, 'reports_items', array('reports' => implode(', ', $wrong)));
             }
         }
     }
     return $value;
 }
示例#4
0
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <*****@*****.**>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once dirname(__FILE__) . '/../../bootstrap/unit.php';
$t = new lime_test(17);
$v = new sfValidatorBoolean();
// ->clean()
$t->diag('->clean()');
// true values
$t->diag('true values');
foreach ($v->getOption('true_values') as $true_value) {
    $t->is($v->clean($true_value), true, '->clean() returns true if the value is in the true_values option');
}
// false values
$t->diag('false values');
foreach ($v->getOption('false_values') as $false_value) {
    $t->is($v->clean($false_value), false, '->clean() returns false if the value is in the false_values option');
}
// required is false by default
$t->is($v->clean(null), false, '->clean() returns false if the value is null');
try {
    $v->clean('astring');
    $t->fail('->clean() throws an error if the input value is not a true or a false value');
    $t->skip('', 1);
} catch (sfValidatorError $e) {