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.

General Questions

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

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

Set a limit for the number of DataObjects that can created


Reply

8 Posts   455 Views

Avatar
vwd

12 March 2013 at 5:44am Community Member, 158 Posts

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

13 March 2013 at 9:34am Forum Moderator, 1796 Posts

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

13 March 2013 at 11:09am (Last edited: 13 March 2013 11:10am), Community Member, 158 Posts

Hi Swaiba,

Thanks for your response. When you say "Validator" do you mean [url href=http://doc.silverstripe.org/framework/en/topics/datamodel#validation-and-constraints]overriding the validate() function[/url] 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

13 March 2013 at 9:37pm Community Member, 215 Posts

The code that doesn't work

Works for me. The Add button is disabled.

Avatar
swaiba

13 March 2013 at 10:11pm Forum Moderator, 1796 Posts

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

Avatar
vwd

13 March 2013 at 11:45pm Community Member, 158 Posts

Hi Devlin,

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

VWD.

Avatar
Devlin

14 March 2013 at 12:28am (Last edited: 14 March 2013 1:59am), Community Member, 215 Posts

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

14 March 2013 at 11:29am Community Member, 158 Posts

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.