/** * Check we either have an associated block already or if BlockType has been passed in as value on extended model * e.g. from CMS form submission. * * @param \ValidationResult $validationResult * @return array|void * @throws \ValidationException */ public function validate(ValidationResult $validationResult) { parent::validate($validationResult); if (!($blockType = $this->getBlockType())) { $validationResult->error($this->fieldDecoration(self::BlockTypeFieldName, 'missing', '{model} requires a block type', ['model' => $this()->i18n_singular_name()])); throw new ValidationException($validationResult); } }
/** * Skip validation for Pages if not already saved so we can create new pages with DateFields as CMS saves early. * * @param \ValidationResult $result * @throws \ValidationException * @return null */ public function validate(\ValidationResult $result) { if ($this() instanceof \SiteTree) { if (!$this()->isInDB()) { return null; } } parent::validate($result); }
/** * Prevent duplicate code being entered. * * @param \ValidationResult $result * @return array|void * @throws \ValidationException */ public function validate(ValidationResult $result) { // this could throw an exception, let it parent::validate($result); $code = $this->owner->{self::CodeFieldName}; if ($this->owner->isInDB()) { // code should be read-only in CMS but check anyway that doesn't exist on another ID $existing = Model::get($this->owner->class)->exclude('ID', $this->owner->ID)->filter(self::CodeFieldName, $code)->first(); } else { // check code doesn't exist already $existing = Model::get($this->owner->class)->filter(self::CodeFieldName, $code)->first(); } if ($existing) { $message = $this->fieldDecoration(self::CodeFieldName, 'Duplicate', "Code must be unique, the {singular} '{title}' already uses '{code}'", ['code' => $code, 'title' => $existing->Title ?: $existing->Name]); $result->error($message); throw new ValidationException($result); } }