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

Link as Required Field doesn't appear to work #69

Closed
chrispenny opened this issue Jun 14, 2023 · 4 comments
Closed

Link as Required Field doesn't appear to work #69

chrispenny opened this issue Jun 14, 2023 · 4 comments

Comments

@chrispenny
Copy link
Collaborator

chrispenny commented Jun 14, 2023

class MyBlock extends BaseElement
{
    private static array $has_one = [
        'CtaLinkPrimary' => Link::class,
    ];

    private static bool $inline_editable = false;

    public function getCMSFields(): FieldList
    {
        $fields = parent::getCMSFields();

        $fields->addFieldsToTab(
            'Root.Main',
            [
                TextField::create('Title'),
                LinkField::create('CtaLinkPrimary'),
            ]
        );

        return $fields;
    }

    public function getCMSCompositeValidator(): CompositeValidator
    {
        $composite = parent::getCMSCompositeValidator();

        $composite->addValidator(RequiredFields::create([
            'Title',
            'CtaLinkPrimaryID',
            // Also tried:
            'CtaLinkPrimary',
        ]));

        return $composite;
    }
}

Result

Title field correctly displays error, but LinkField does not.

Screen Shot 2023-06-14 at 12 57 44 PM

PRs

@GuySartorelli
Copy link
Member

That's quite possibly a problem with RequiredFields rather than with linkfield. I imagine you'd get the same result if you were using https://github.com/silvershop/silverstripe-hasonefield and tried to set a HasOneField's value as required.

@sabina-talipova
Copy link
Contributor

Hi @chrispenny,
I don't know, but probably it could helps to find some idea. I add new class that extends LinkField's FormFactory and implements getValidator(). Please, see test steps in this PR.

@maxime-rainville
Copy link

We'll validate if this is still a problem and fix it if it is.

@GuySartorelli
Copy link
Member

PRs merged

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

5 participants