silverstripe-elemental: Validation not compatible with adding blocks inline in 4.0

In the past i’ve relied on some light validation for elements to ensure some particular content is present.

For example:

public function validate()
{
    $validation = parent::validate();

    if (!$this->ImageID) {
        $validation->addError('Image is required');
    }

    return $validation;
}

When adding an element inline with validation like above above in 4.0, it throws a console error - Uncaught (in promise) Error: GraphQL error: Image is required. This does makes sense as you are in fact saving the element.

I’ve also tried leveraging getCMSValidator:

public function getCMSValidator()
{
    return RequiredFields::create([
        'ImageID',
    ]);
}

However this doesn’t trigger on save/publish. It does however looks like it gets initialised when adding a block.

A scenario such as above could easily be swapped out for template logic but I’m sure there will be a requirement where validation may actually be desired.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I see… Okay - interesting conundrum. Elemental 4 has an approach a bit like SiteTree where the block is saved as soon as you press the add button. I suppose that means we should skip the validation only once for creating blocks?