Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Set a limit for the number of DataObjects that can created


Go to End


8 Posts   1380 Views

Avatar
vwd

Community Member, 166 Posts

12 March 2013 at 5:44am

Hi,

I'm trying to set an upper limit to the number of (a particular) DataObject that can be created. Reading a couple of previous threads ([a href=http://www.silverstripe.org/general-questions/show/21226]1[/a], [a href=http://www.silverstripe.org/general-questions/show/5130]2[/a]), I tried to implement the limit via the canCreate function. However, it appears that this function isn't executed when I create & save the DataObject via the CMS (GridField).

The code that doesn't work:

    function canCreate($member = null)
    {
        $returnVal = false;
        if (ServiceItem::get()->Count() < 3) {    // don't allow adding more than 3 ServiceItems.
            $returnVal = true;
        }
        return $returnVal;
        
    }

How should I implement this limit in SS3?

Thank you.
VWD

Avatar
swaiba

Forum Moderator, 1899 Posts

13 March 2013 at 9:34am

You could put the code in a Validator (my preference) or the onBeforeWrite to prevent the write, not ideal but functional.

You could also modify getCMSFeilds to return only a literal "sorry you can only have 3 records" etc

Avatar
vwd

Community Member, 166 Posts

13 March 2013 at 11:09am

Edited: 13/03/2013 11:10am

Hi Swaiba,

Thanks for your response. When you say "Validator" do you mean overriding the validate() function of the DataObject?

Also would there be a way of (in getCMSFields) disabling the "Add" button when the limit has been reached and then adding the literal as you suggested? Is there a way of disabling the add button in the GridField? That would be ideal. Better than letting the user go to all the effort of filling out the details, trying to add/save the DO and then being told that they can't because its reached the limit.

Thanks.
VWD.

Avatar
Devlin

Community Member, 344 Posts

13 March 2013 at 9:37pm

The code that doesn't work

Works for me. The Add button is disabled.

Avatar
swaiba

Forum Moderator, 1899 Posts

13 March 2013 at 10:11pm

I should add I'd have expected it to work for me too, int he context of ModelAdmin on 2.4.x

Avatar
vwd

Community Member, 166 Posts

13 March 2013 at 11:45pm

Hi Devlin,

Thanks for your response. So that works for you on SS3?

VWD.

Avatar
Devlin

Community Member, 344 Posts

14 March 2013 at 12:28am

Edited: 14/03/2013 1:59am

I actually tried this with SS 2.4.x and 3.1b1. But you're right, this doesn't work in 3.0.x.

Edit:
See commit for 3.1
https://github.com/silverstripe/sapphire/commit/1848d7e90abad4b4b969fa87922a58c97a1026de

Somehow this wasn't included in the 3.0 branch.

Avatar
vwd

Community Member, 166 Posts

14 March 2013 at 11:29am

Hi Devlin,

Thank you very much for letting me know. Good to know that that functionality will be back in 3.1.

Kind regards,
VWD.