diff --git a/src/Models/Link.php b/src/Models/Link.php index 87d2de0f..c419e1e3 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -16,6 +16,8 @@ use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\View\Requirements; +use SilverStripe\Forms\Form; +use SilverStripe\ORM\ValidationResult; /** * A Link Data Object. This class should be a subclass, and you should never directly interact with a plain Link @@ -110,6 +112,28 @@ public function getCMSCompositeValidator(): CompositeValidator return $validator; } + /** + * This method is used to validate the dataobject before saving as part of DataObject::write() + * Linkfield works a bit differently from normal forms, because where the modal data is turned + * into JSON and saved into a single JsonField and in saveInto() the JSON is loaded into the + * dataobject using DataObject::setData($data) + * Because of this alternate method of saving, the getCMSCompositeValidator() isn't called + * so we need to call it manually here by loading it into a temporary Form + * + * @return ValidationResult + */ + public function validate() + { + $parentResult = parent::validate(); + $validator = $this->getCMSCompositeValidator(); + $form = Form::create(null, Form::DEFAULT_NAME, $this->getCMSFields()); + $form->setValidator($validator); + $form->loadDataFrom($this); + $formResult = $form->validationResult(); + $combinedResult = $parentResult->combineAnd($formResult); + return $combinedResult; + } + /** * Form hook defined in @see Form::saveInto() * We use this to work with an in-memory only field