Search code examples
phpjoomlajoomla1.7joomla1.6joomla2.5

Joomla PHP: Checkbox won't uncheck


Making a simple component. If I save a new record with the box checked, it saves fine. If I go back and uncheck a previously checked item it reverts back to checked. I thought it had something to do with a hidden field, played with putting one of those in manually but that didn't fix it either so took it out.

Here's where I'm at: An xml snippet:

<fieldset name="checks">
  <field name="checkbox1"
    type="checkbox"
    label="First Checkbox"
    value="1"
    filter="intval"         
  /> 

   <field name="checkbox2"
    type="checkbox"
    label="Second Checkbox"
    value="1"
    filter="intval"         
  /> 

 ...

</fieldset>

The edit.php file:

<div class="width-45 fltlft">
  <fieldset class="adminform">
    <legend>Checkboxes</legend>
    <ul class="adminformlist">
      <?php foreach ($this->form->getFieldset('checks') as $field): ?>
        <li>
          <?php echo $field->label; ?>
          <?php echo $field->input; ?>
        </li>
      <?php endforeach ?>
    </ul>
  </fieldset>
</div>

Also added this towards the end before the form.token:

<input type="hidden" name="task" id="task" value="completion.edit" />

I tried deleting the value="1" in the xml but then I had the opposite problem where the check wouldn't save at all.

Any ideas?

Thanks!

=============================

Edit:

Model:

<?php
defined( '_JEXEC' ) or die;

jimport('joomla.application.component.modeladmin');

class AssessModelCompletion extends JModelAdmin
{
//tells it what kind of record and the prefix

    public function getTable($type = 'Completion', $prefix = 'AssessTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

    //Load the data into the edit form
    protected function loadFormData()
    {
        $data = JFactory::getApplication()->getUserState('com_assess.edit.completion.data', array()); //first try to get the data from the session, not db

        if (empty($data)) {
            $data = $this->getItem(); //this gets the data
        }

        return $data;

    }

    //Stores data in a session in case a field is missed
    public function getForm($data = array(), $loadData = true)
    {
        $form = $this->loadForm('com_assess.completion', 'completion', array('control' => 'jform', 'load_data' => $loadData));

        return $form;
    }
}

And table:

<?php 
defined ( '_JEXEC' ) or die;

class AssessTableCompletion extends JTable
{
    public function __construct(&$db)
    {
        parent::__construct('#__tablename_completions', 'completion_id', $db);
    }
}

======================================

References: https://stackoverflow.com/questions/6964333/joomla-1-6-admin-form-processing-grouped-checkboxes-in-form

Tutorial code is not working any more in Joomla 2.5

http://docs.joomla.org/Developing_a_Model-View-Controller_%28MVC%29_Component_for_Joomla!1.7_-_Part_09#Adding_a_toolbar


Solution

  • So I had a lot of fields and wanted to simply loop through them in my edit.php field to keep it clean. While all the answers offered were right they weren't easy to implement - got really messy really quickly, had trouble getting it to work, or couldn't figure out a cleaner way around it. I chewed on this for awhile and then today came across what is basically a field override.

    The key:

    The standard form field types are located in joomla/libraries/joomla/form/fields/. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.

    The custom field types that belong to your component are usually located in administrator/components//models/fields. You can specify this or another path in your code

    So, I copied checkbox.php to models/fields. Then, towards the end of the file I added the empty field before the checkbox tag:

    <input type="hidden" name="'.$this->name.'" id="'.$this->id.'" value="0" /><input type="checkbox" .....
    

    Now, whenever I need a checkbox the empty field is also written. May not be the most efficient solution but simple to implement and can hopefully help someone else.

    [Edit]

    As a note, with every Joomla update you would probably need to compare the versions in the core in case there was a change.