Example #1
0
 /**
  * @see sfValidatorBase
  */
 protected function doClean($value)
 {
     $choices = sfWidgetFormTree::normalizeChoices($this->getOption('choices'), $this->getOption('label_key'), $this->getOption('value_key'), $this->getOption('level_key'), $this->getOption('children_key'));
     $restrict_select_below = $this->getOption('restrict_select_below');
     if ($restrict_select_below instanceof sfCallable) {
         $restrict_select_below = $restrict_select_below->call();
     }
     if ($restrict_select_below !== false && !is_array($restrict_select_below)) {
         $restrict_select_below = array($restrict_select_below);
     }
     if ($choices instanceof sfCallable) {
         $choices = $choices->call();
     }
     if ($this->getOption('multiple')) {
         if (!is_array($value)) {
             $value = array($value);
         }
         foreach ($value as $v) {
             if (!self::inChoices($v, $choices)) {
                 throw new sfValidatorError($this, 'invalid', array('value' => $v));
             }
             if ($restrict_select_below !== false) {
                 if (self::valueIsParentOf($v, $restrict_select_below, $choices)) {
                     throw new sfValidatorError($this, 'restricted', array('value' => $v));
                 }
             }
         }
     } else {
         if (!self::inChoices($value, $choices)) {
             throw new sfValidatorError($this, 'invalid', array('value' => $value));
         }
         if ($restrict_select_below !== false) {
             if (self::valueIsParentOf($value, $restrict_select_below, $choices)) {
                 throw new sfValidatorError($this, 'restricted', array('value' => $value));
             }
         }
     }
     return $value;
 }
 /**
  * @param  string $name        The element name
  * @param  string $value       The value selected in this widget
  * @param  array  $attributes  An array of HTML attributes to be merged with the default HTML attributes
  * @param  array  $errors      An array of errors for the field
  *
  * @return string An HTML tag string
  *
  * @see sfWidgetForm
  */
 public function render($name, $value = null, $attributes = array(), $errors = array())
 {
     if ('[]' != substr($name, -2)) {
         $name .= '[]';
     }
     $choices = sfWidgetFormTree::normalizeChoices($this->getOption('choices'), $this->getOption('label_key'), $this->getOption('value_key'), $this->getOption('level_key'), $this->getOption('children_key'));
     if ($choices instanceof sfCallable) {
         $choices = $choices->call();
     }
     return $this->formatChoices($name, $value, $choices, $attributes);
 }