/** * {@inheritdoc} * If no record is found in the auto complete list, the raw value will be * validated against any custom field's constraints. */ public function validate($validator) { $parentValid = parent::validate($validator); // If this field is required but doesn't have a record, then check it // at least has a raw value. if ($this->Required()) { if (!$this->getRecord()) { $requiredValid = $this->getRawField()->validate($validator); $rawValue = $this->getRawField()->dataValue(); if (!($requiredValid && !empty($rawValue))) { $validator->validationError($this->getRawField()->getName(), _t('AutocompleteField.VALIDATION', '{title} is required', null, array('title' => $this->getRawField()->Title())), 'bad'); } } } if (!$this->getRecord()) { return $this->getRawField()->validate($validator) && $parentValid; } return $parentValid; }
/** * Determine if a list of values is valid for this field's options. * @param $needle array a one- or two-dimensional array of options to test * @return array a list of the valid options */ public function validate($needle) { $return_array = false; // validate against a list of keys from haystack. haystack may be // array(1, 2, 3) or array(array(1, Something), array(2, Other)) $keys = array(); if (is_array($this->options[0])) { foreach ($this->options as $a) { $keys[] = $a[0]; } } else { $keys = $this->options; } // check input if (is_array($needle)) { $return_array = true; } else { $return_array = false; $needle = array($needle); } // find which of the input was valid $valid = array(); foreach ($needle as $v) { if (in_array($v, $keys)) { $valid[] = $v; } } // return array, if necessary if ($return_array) { return $valid; } // otherwise, return string if (count($valid) == 0) { // validation failed, no match return ''; } // validation succeeded, return one value return parent::validate($valid[0]); }
public function validate($data) { $num = $data[$this->name]; if (!is_numeric($num)) { $this->error("That is not a valid number."); } else { if (!is_null($this->min) && $num < $this->min) { $this->error("That number is too small"); } else { if (!is_null($this->max) && $num > $this->max) { $this->error("That number is too large"); } else { parent::validate($data); } } } return !$this->hasError; }