Results 1 to 7 of 7

Thread: Frontend Checkbox & Radio Defaults

  1. #1
    New user
    Join Date
    07-01-10.
    Location
    Midlands, UK
    Posts
    26

    Default Frontend Checkbox & Radio Defaults

    I'm having some issues implimenting checkboxes and radio fields in the front end with default selections. I have a situation where I need single checkbox to be ticked by default. I can just about get one ticked if there are 2 options but this isn't any good for my requirements. I thought about using radio buttons where I could have 2 options (yes/no) but I still found the default selection to be odd. Really I want a single checkbox that is ticked by default.

    Any ideas anyone?

  2. #2
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: Frontend Checkbox & Radio Defaults

    Checkbox menu with 1 option with a value of 1 and the default checkbox checked.

  3. #3
    New user
    Join Date
    07-01-10.
    Location
    Midlands, UK
    Posts
    26

    Default Re: Frontend Checkbox & Radio Defaults

    Quote Originally Posted by thyon
    Checkbox menu with 1 option with a value of 1 and the default checkbox checked.
    Thanks Thyon but I'm not sure I follow (the last bit).

    I have something like this in my dca:

    Code:
    blah...
    'default' => '1', //does nothing for me here
    'options' => array('1'),
    'reference' => array('1'=>'label'),
    ...blah

  4. #4
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default Re: Frontend Checkbox & Radio Defaults

    Lol. I thought you were talking about the form generator.

    For a single checkbox, model your DCA after tl_news => addImage, then simply add a default => true.

    I use that dca override all the time in dcaconfig.php to auto-check the addImage for the user to remember to complete the image section.

  5. #5
    New user
    Join Date
    07-01-10.
    Location
    Midlands, UK
    Posts
    26

    Default Re: Frontend Checkbox & Radio Defaults

    I've looked at that dca (tl_news) and implemented for my situation as follows:

    Code:
    'myfield' => array(
    			'label' => &$GLOBALS['TL_LANG']['tl_my_table']['myField'],
    			'inputType' => 'checkbox',
    			'default' => true, 
    			'eval' => array('feEditable'=>true, 'tl_class'=>'w50')
    		),
    This single checkbox remains unchecked by default in the frontend though. Interestingly, when looking at it in the backend, it works as you've described. So it sppears I've implemented the dca as required (for the backend at least) but something is not working in the frontend.

  6. #6
    Experienced user
    Join Date
    01-12-10.
    Posts
    814

    Default Re: Frontend Checkbox & Radio Defaults

    For the frontend Contao checks each options individual value (assuming a checkbox widget can have more then one) then checkes the ones that matches the value of the widget. So if you set the value of the widget to whatever the value is of the option you want to select by default Contao will select it.

    For example:
    Code:
    $varValue = 1;
    // Set value when you create the widget
    $objWidget = new FormCheckBox($this->prepareForWidget($yourOptions, 'widget_name', $varValue));
    // Set the value after creation
    $objWidget->value = $varValue;
    // Result is a checked checkbox
    $result = $objWidget->parse();

  7. #7
    New user
    Join Date
    07-01-10.
    Location
    Midlands, UK
    Posts
    26

    Default Re: Frontend Checkbox & Radio Defaults

    Thanks Ruud. From your posting (and possibly having benefitted from getting some sleep - a rarity), I tracked down my issue to how I've created the widget. It was the 3rd parameter and because in the set of forms I've created, this was the only default value I wanted so I hadn't noticed that I wasn't setting one, unless the form was being refiwed (and the value was set from session data). I had this:

    Code:
    $objWidget = new $strClass($this->prepareForWidget($arrData, $table['field'], (strlen($_SESSION[$strSessionProductNode][$strSessionProductElement][$table['field']]) ? $_SESSION[$strSessionProductNode][$strSessionProductElement][$table['field']] : $arrDefault[$table['field']])));
    and
    Code:
    $arrDefault[$table['field']]
    was always empty. The dangers of copying and pasting blocks of code!!!

    As you can see, I was passing it the $arrData array (which is the dca array for the current field) so I changed the line to read:

    Code:
    $objWidget = new $strClass($this->prepareForWidget($arrData, $table['field'], (strlen($_SESSION[$strSessionProductNode][$strSessionProductElement][$table['field']]) ? $_SESSION[$strSessionProductNode][$strSessionProductElement][$table['field']] : $arrData['default'])));
    ...notice the:
    Code:
    $arrData['default']
    at the end.

    = Result!

    Thanks for your help guys in helping me track this down :D

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •