Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation can prevent the creation of new block #1149

Closed
2 tasks done
maxime-rainville opened this issue Feb 23, 2024 · 3 comments
Closed
2 tasks done

Validation can prevent the creation of new block #1149

maxime-rainville opened this issue Feb 23, 2024 · 3 comments

Comments

@maxime-rainville
Copy link
Contributor

maxime-rainville commented Feb 23, 2024

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.

<?php

use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\Forms\FieldList;
use SilverStripe\LinkField\Form\LinkField;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\LinkField\Models\Link;

/**
 * @property Link $MyTestLink
 */
class TitleBlock extends BaseElement
{

    private static $table_name = 'TitleRequriedBlock';

    private static $singular_name = 'Block with required title';

    private static $plural_name = 'test link blocks';

    private static $description = 'A block with a that requires a Title';

    public function getSummary()
    {
        return '';
    }

    protected function provideBlockSchema()
    {
        $blockSchema = parent::provideBlockSchema();
        $blockSchema['content'] = $this->getSummary();
        return $blockSchema;
    }

    public function getType()
    {
        return 'Title Required Block';
    }

    public function validate()
    {
        $validator = parent::validate();
        // You need to add silly logic to allow the block to be created in the first place
        if ($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.

Additional Context

Related card:

Validations

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)

Acceptance criteria

  • A content block with validation rules can be created by a user even if the default values do not meet the validation rules.
  • Further attempts to save or publish the content block are prevented unless the data is updated to pass validation.

PRs

@maxime-rainville maxime-rainville added this to the Silverstripe CMS 5.3 milestone Feb 23, 2024
@michalkleiner michalkleiner changed the title Block ceration should bypass validation Block creation should bypass validation Feb 25, 2024
@GuySartorelli
Copy link
Member

@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)?

@maxime-rainville maxime-rainville changed the title Block creation should bypass validation Validation can prevenet the creation of new block Mar 12, 2024
@maxime-rainville
Copy link
Contributor Author

I added extra details.

@maxime-rainville maxime-rainville changed the title Validation can prevenet the creation of new block Validation can prevent the creation of new block Mar 13, 2024
@maxime-rainville
Copy link
Contributor Author

I'm assuming this can be done without API change. But I'm happy to push it to CMS 6 if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants