Joomla component builder: managing checkbox in form definitions

 

The checkbox problem and form pages: html and Joomla!

The origin of the problem is known: the standard html checkbox is sent to server only if it is checked (checkboxes are the only input type with this behavior).

This article is being written and is under revision, errors may occur. sorry!Pant, pant...I'm working hard to finish it.

Joomla does not handle the checkbox if it is not checked and so Joomla requires the programmer takes care of this, as written for the specific field in the official documentation in http://docs.joomla.org/Checkbox_form_field_type. Here, there is a nice box with a warning inviting us to pay attention to a "common mistake", ie thinking that is the framework of Joomla to take charge of what.

But what is the problem?

The problem is that if one thinks to put the check mark and he saves the field as checked, it works, but if, on the other side, he thinks to remove the check mark to save the unchecked field, it does not work! The checkbox itself is not transmitted and Joomla doesn't bind the field with the other fields definited in the xml form.

This code applies only to Joomla 3+.
The solutions for Joomla! 1.5 and Joomla! 2.5 have already been discussed many times, and here is a summary of the possible solutions http://docs.joomla.org/Talk:Checkbox_form_field_type.

How to save the checkbox also if it not sent by the form

here how to solve the Joomla checkbox problem

Joomla! 3 has extended the functionalities available in the classes JFormField and JForm, allowing to easy explore the JForm object and the fields therein.

It is therefore possible to iterate over all the fields in the xml form, looking for any checkbox, and, if found, make sure they are also included in the data received by the submission of the editing form. If checkboxes are not present, we will insert them with the appropriate value for the unchecked state.

Edit the line that defines the checkbox in the definition of data in /admin/models/forms/[object].xml:

<field name = "show_title" type = "checkbox" label = "Show title" description = "Show the title of the item" value = "1" unchecked = "0" default = "0" />

We added the unchecked attribute; this attribute will contain the value to be saved if the field is not sent by the form.

If the value for the unchecked checkbox is '0' you can also avoid changing the definition. This is just an added feature to handle special cases.

Now we will write a function doing the job required.

Data received from the web are available inside the method JModelLegacy::validate($form, $data, $group = null); to act here will also allow us to operate only after the data have been properly filtered and verified on the basis of the rules defined in the form definition: we act at the very last stage of the process, and only when it is necessary.

Let us now proceed with the override of the method, using the following code:

public function validate($form, $data, $group = null){
  $data =  parent::validate($form, $data, $group = null);
  if (version_compare( JVERSION, '3.0', '<' )) return $data; 
 
  foreach($form->getFieldsets() as $fset){
    foreach($form->getFieldset($fset->name) as $f){
      if ( (JString::strcasecmp($f->type, 'Checkbox') == 0) &amp;&amp; !isset($data[$f->fieldname]) ) {
        $data[$f->fieldname] = $f->getAttribute('unchecked', '0');
      }
    }
  }
  return $data;
}
 

 

This code is already present in templates from Marco's Component Builder 2.5.04+. but it's commented, if you need it remove the comment block.

That's it! If in the $form variable (the form of xml definition) is defined a field of type checkbox, and this field is not present in the hash table $data containing the data associated with the fields defined in the XML, then the function will put the relative field in the hash table with the value for the condition unchecked.

This code is absolutly independent from the name of the fields and the complexity of the form, and can be inserted in any implementation of JModelLegacy, as long as it is Joomla! 3.

 

 

 

Aggiungi commento

Please note: URL in text are not linked and user's site address is only for internal use and is not published.

Comments are human checked. All spam will be removed, so don't waste your time and, especially, mine!

Codice di sicurezza
Aggiorna