21487 Posts in 5783 Topics by 2621 members
General Questions
SilverStripe Forums » General Questions » Set a limit for the number of DataObjects that can created
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 186 Views |
-
Set a limit for the number of DataObjects that can created

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 -
Re: Set a limit for the number of DataObjects that can created

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
-
Re: Set a limit for the number of DataObjects that can created

13 March 2013 at 11:09am Last edited: 13 March 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. -
Re: Set a limit for the number of DataObjects that can created

13 March 2013 at 9:37pm
The code that doesn't work
Works for me. The Add button is disabled.
-
Re: Set a limit for the number of DataObjects that can created

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
-
Re: Set a limit for the number of DataObjects that can created

13 March 2013 at 11:45pm
Hi Devlin,
Thanks for your response. So that works for you on SS3?
VWD.
-
Re: Set a limit for the number of DataObjects that can created

14 March 2013 at 12:28am Last edited: 14 March 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/1848d7e90abad4b4b969fa87922a58c97a1026deSomehow this wasn't included in the 3.0 branch.
-
Re: Set a limit for the number of DataObjects that can created

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.
| 186 Views | ||
|
Page:
1
|
Go to Top |



