Getting rid of jform[]

191 views
Skip to first unread message

hoochicken

unread,
Nov 6, 2012, 3:06:34 PM11/6/12
to joomla-de...@googlegroups.com
Hi,
in a little module a form is generated via getForm() etc.
The xml hat field name like "currency" and "amount".
In the form itself these field names are transformed to "jform[curreny]" and "jform[amount]".
If there a way of supressing the addition of "jform[]"?
Of course I can use a preg_replace() when getting the inputs, but is there a clean way of doing it?
Grateful for any help:-)
Mareike

subtextproductions

unread,
Nov 6, 2012, 5:22:57 PM11/6/12
to joomla-de...@googlegroups.com
I guess it depends on how exactly you retrieve the JForm object. There are two methods for JModelForm that are used to retrieve a form object. getForm is an abstract method that you must implement in your model. loadForm is the method that actually loads the XML file and returns the form object. Generally your getForm method looks something like this:

public function getForm($data = array(), $loadData = true)
{
if($form = $this->loadForm('com_slideshow.images', 'images', array('control'=>'jform', 'load_data'=>$loadData))){
return $form;
}
JError::raiseError(0, JText::sprintf('JLIB_FORM_INVALID_FORM_OBJECT', 'images'));
return null;
}

The loadForm method accepts five variables: $name, $source, $options, $clear, and $xpath. In the example above, you can see that I have populated the first three variables. The $name is a dot notation namespace string. The $source variable is the name of your xml file without the extension. The $options variable is an array of directives with keys as the directive name and values as the setting for that directive. In the options, 'control' is what adds the jform namespace to all the variables in your form. So, you could get rid of it if you use this code:

$form = $this->loadForm('mycomponent.context', 'context', array( 'control'=>'', 'load_data'=>$loadData ) );

However, I would suggest you leave it as is. At first, when I started working with JForm, I too wanted to get rid of the extra control used on the form variable names. But, I have since discovered that it is really helpful to just leave it and use as is. JControllerForm and JControllerAdmin will use the jform control to automatically collect the data submitted from a form and process it without you having to write any additional CRUD functions, but only if you use the jform control.

It works like this:

$data = JRequest::get('jform', 'array');

That gets all the data from your form while stripping away any other data that is submitted at the same time.




SpiralScripts

unread,
Nov 7, 2012, 4:06:35 AM11/7/12
to joomla-de...@googlegroups.com
I agree, it is much better to leave the 'jform' as it is. To be compatible with Joomla 3.0 you should use JInput rather than JRequest, eg

        $jinput = JFactory::getApplication()->input;   
        $formdata    = $jinput->get('jform', array(),'array');

Then you can access the individual fields as $formdata["currency"], $formdata["amount"]

It is much easier this way.

hoochicken

unread,
Nov 7, 2012, 5:20:17 AM11/7/12
to joomla-de...@googlegroups.com
Hi,
thank you very much; this is just what I needed.
Explanation: I can't kleep jform in this case. There is an external application that requests special fieldsnames ("amount", "currency").
Thank you once again,
Greetings
Mareike

Reply all
Reply to author
Forward
0 new messages