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.

Customising the CMS /

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

CheckBox in Admin Area


Go to End


4 Posts   12519 Views

Avatar
Boogiez

Community Member, 17 Posts

4 January 2009 at 12:29pm

Hi,

I would like to create a checkbox in my admin area for my client to indicate whether an item in her online gallery has been sold or not. If an item is sold, then I have a little extra HTML to insert in the page ("SOLD. This item has been sold and is available for viewing only.").

I know how to create images and text fields in the CMS, but how to create a checkbox?
And then how do I call an "if" statement on the checkbox in my template page? Would I need to write a function to query the state of the checkbox or can I check its value in the database directly?

Thanks,
Mark

Avatar
Carbon Crayon

Community Member, 598 Posts

4 January 2009 at 3:15pm

Edited: 04/01/2009 3:18pm

Hi Boogiez, welcome to Silverstripe

I would recommend you do the tutorials to start with, they give you a very good introduction to how both the templates and admin/db work. You can find them here: http://doc.silverstripe.com/doku.php

To answer your question you can easily add a checkbox to a page in the admin by adding this to your Page.php file:

class Page extends SiteTree {
	
       static $db = array(
                'CheckboxValue' => 'Boolean'
	);

	
	function getCMSFields() {
	$fields = parent::getCMSFields();
	
	$fields->addFieldToTab("Root.Content.Main", new CheckboxField ("CheckboxValue"));
	
	return $fields;	
	}
	
}

then in your template you simply do this:

<% if CheckboxValue %>
      <p>SOLD</p>
<% end_if %>

Hope that helps :)

Avatar
Boogiez

Community Member, 17 Posts

5 January 2009 at 8:33am

Thanks very much aram! Works like a charm. I checked the tutorials but I didn't see how to create a checkbox. Do you know if there's a page that lists all of the controls we can offer in the CMS?

Avatar
Carbon Crayon

Community Member, 598 Posts

5 January 2009 at 8:51am


yep, check these pages out, I found them very useful when I started and still refer to them often:

Form types: http://doc.silverstripe.com/doku.php?id=form-field-types

Built in page controls: http://doc.silverstripe.com/doku.php?id=built-in-page-controls

DataModel: http://doc.silverstripe.com/doku.php?id=datamodel

Recipies (various how too's): http://doc.silverstripe.com/doku.php?id=recipes:start

I think the tutorials are invaluable if you want to start developing sites in SS beacuse they give you a really good introduction to how it all works and so once you have done them you can really start descovering the beauty of SS (with the help of the docs and forum of course)

Anyway good luck and hope to see you around the forums :)