public function isValid($value, $context = null) { if (count($this->getMultiOptions()) == 0) { $this->setMultiOptions(); } return parent::isValid($value, $context); }
public function isValid($value, $context = null) { $this->setValue($value); $value = $this->getValue(); if ('' === $value || null === $value) { if (!$this->isRequired() && $this->getAllowEmpty()) { return true; } else { $this->addError('Value is required and can\'t be empty'); return false; } } // Process $numValue = $value[0]; $selValue = $value[1]; // Validate number if (!in_array($selValue, array('lifetime', 'forever'))) { if (!is_numeric($numValue) || (int) $numValue != $numValue || $numValue <= 0) { $this->addError('Please enter a valid integer greater than zero.'); return false; } } else { $value[0] = $numValue = '0'; } // Make composite options $options = array(); foreach ($this->options as $k => $v) { if (is_array($v)) { $options = array_merge($options, $v); } else { $options[$k] = $v; } } // Validate selection if (!isset($options[$selValue])) { $this->addError('Please select an option.'); return false; } $valid = parent::isValid($value, $context); return $valid; }
/** * Is the value provided valid? * * Autoregisters InArray validator if necessary. * * @param string $value * @param mixed $context * @return bool */ public function isValid($value, $context = null) { if ($this->registerInArrayValidator()) { if (!$this->getValidator('InArray')) { $multiOptions = $this->getMultiOptions(); $options = array(); foreach ($multiOptions as $opt_value => $opt_label) { // check if it`s array if (is_array($opt_label)) { $isGroup = !self::isArrayStyleLabel($opt_label); } else { $isGroup = false; } // optgroup instead of option label if ($isGroup) { $options = array_merge($options, array_keys($opt_label)); } else { $options[] = $opt_value; } } $this->addValidator('InArray', true, array($options)); } } return parent::isValid($value, $context); }