You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unlike Dataobject managed in a ModelAdmin, Elemental blocks have to be saved before the user can interact with them.
This means that it is some times necessary to suppress validation on block creation.
This is an inconvenience for developers and is counter-intuitive.
How to reproduce
This code snippet illustrate the weird logic one has to go through to apply validation rules to a block while still allowing it to be created.
<?phpuseDNADesign\Elemental\Models\BaseElement;
useSilverStripe\Forms\FieldList;
useSilverStripe\LinkField\Form\LinkField;
useSilverStripe\LinkField\Form\MultiLinkField;
useSilverStripe\LinkField\Models\Link;
/** * @property Link $MyTestLink */classTitleBlockextendsBaseElement
{
privatestatic$table_name = 'TitleRequriedBlock';
privatestatic$singular_name = 'Block with required title';
privatestatic$plural_name = 'test link blocks';
privatestatic$description = 'A block with a that requires a Title';
publicfunctiongetSummary()
{
return'';
}
protectedfunctionprovideBlockSchema()
{
$blockSchema = parent::provideBlockSchema();
$blockSchema['content'] = $this->getSummary();
return$blockSchema;
}
publicfunctiongetType()
{
return'Title Required Block';
}
publicfunctionvalidate()
{
$validator = parent::validate();
// You need to add silly logic to allow the block to be created in the first placeif ($this->isInDB() && trim($this->Title ?? '') === '') {
$validator->addFieldError('Title', 'Title is required (this message is coming from block level)');
}
return$validator;
}
}
Possible Solution
Update BaseElement to skip validation on creation.
@maxime-rainville You've said what should happen, and you have steps to reproduce.... but I assume what you really mean is that those are steps to reproduce a bug? Can you please update the description to talk about what the bug actually is (i.e. what the current behaviour is)?
Module version(s) affected
4 and 5
Description
Unlike Dataobject managed in a ModelAdmin, Elemental blocks have to be saved before the user can interact with them.
This means that it is some times necessary to suppress validation on block creation.
This is an inconvenience for developers and is counter-intuitive.
How to reproduce
This code snippet illustrate the weird logic one has to go through to apply validation rules to a block while still allowing it to be created.
Possible Solution
Update BaseElement to skip validation on creation.
Additional Context
Related card:
Validations
silverstripe/installer
(with any code examples you've provided)Acceptance criteria
PRs
DataObject::write()
developer-docs#499The text was updated successfully, but these errors were encountered: