validate before publish
Closed this issue · 2 comments
mi32dogs commented
Hi is there a way to validate before publish?
For example, I have a dataobject that needs a date before it can be published
But I wand the ability to always be able to save it even without a date.
stecman commented
I haven't tested this, but I suspect that because the reading mode is switched to the appropriate value when saving as draft or publishing, you might be able to do something like this:
class SomeDataObject extends DataObject
{
// ...
protected function validate()
{
$result = parent::validate();
if (Versioned::current_stage() == 'Live') {
if (empty($this->Date)) {
$result->combineAnd(
new ValidationResult(false, 'Date cannot be empty when publishing'));
}
}
return $result;
}
}
mi32dogs commented
This works perfectly, thank you so much