/** * Add a regular expression check before saving the value. * * @param mixed $value Attribute value. * @return string Validated string value. * @throws InvalidValueException Value does not match pattern. */ protected function convert($value) { $value = (string) parent::convert($value); if (!preg_match($this->regexp, $value)) { $msg = sprintf('Invalid value for the [%s] attribute.', $this->name); throw new InvalidValueException($msg); } return $value; }
/** * Add a value comparison check before saving the value. * * @param mixed $value Attribute value. * @return string Validated string value. * @throws InvalidValueException Value not allowed. */ protected function convert($value) { $value = (string) parent::convert($value); if (!in_array($value, $this->values, true)) { $msg = sprintf('Value "%s" is not allowed for the [%s] attribute.', $value, $this->name); throw new InvalidValueException($msg); } return $value; }