module development to use jform for frontend module form

427 views
Skip to first unread message

nant

unread,
Dec 13, 2013, 12:08:46 PM12/13/13
to joomla-de...@googlegroups.com
Hi,

Are there any examples on how to properly make a Joomla MVC module and how to use jform for module frontend form?


Matt Thomas

unread,
Dec 13, 2013, 4:36:11 PM12/13/13
to joomla-de...@googlegroups.com

Hello,

I think the core login module might be helpful. Take a look at https://github.com/joomla/joomla-cms/tree/master/modules/mod_login

Best,

Matt Thomas
@betweenbrain
http://matt-thomas.me/
http://betweenbrain.com/
https://github.com/betweenbrain

Sent from mobile. Please pardon any typos or brevity.

On Dec 13, 2013 12:09 PM, "nant" <nant...@gmail.com> wrote:
Hi,

Are there any examples on how to properly make a Joomla MVC module and how to use jform for module frontend form?


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Ove

unread,
Dec 14, 2013, 8:41:11 AM12/14/13
to joomla-de...@googlegroups.com
I don't think you can use JForm without an underlying component. i.e.
your module has to call methods in a model of a component.

Matt's login example does not use JForm as far as I know, but it could
be rewritten to use the frontend com_users login model and login.xml form.

The article modules use the com_content models but not combined with JForm.

Hope this give you a first idea.

Ove

hoochicken

unread,
Dec 14, 2013, 4:06:39 PM12/14/13
to joomla-de...@googlegroups.com
Hi,

of course jform can be used in an module.

Tip: Have a close look at mod_qlform and its class "modelQlForm" in modules/mod_qlform/classes/modelQlForm.php
See here: http://extensions.joomla.org/extensions/contacts-and-feedback/forms/20770?qh=YToyOntpOjA7czo2OiJxbGZvcm0iO2k6MTtzOjc6InFsZm9ybXMiO30%3D

mod_qlform does exactly, what you want to code - except that you will feed it with a real xml file, while mod_qlform is fed from a param with xml data.
Maybe you don't need an extra class and you can use jModelForm directly. I think, that might work, too:-)

I wanted to look up the JModelForm in the developer's documentation, but somehow they've changed the docs; so I cannot get to the description of the jModelForm class with all its method.
Question: Does anyone know, where to find the the description of the classes and methods or at least the old docu?

Greetings
Mareike

<?php
/*somewhere include the class bla bla*/
/*1* calling the form*/
$obj_form=new modelYourClassName();
$obj_form->form_name='someFormName';
$obj_form->str_xml=$str_xml;
$form=$obj_form->getForm();

/*following you see the class to be included*/

/**
 * @package        mod_qlform
 * @copyright    Copyright (C) 2013 ql.de All rights reserved.
 * @author         Mareike Riegel
 * @license        GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
jimport('joomla.application.component.modelform'); /*import the JModelForm class*/

class modelYourClassName extends JModelForm
{

    /**
     * Method for getting the form from the model.
     *
     * @param   array    $data      Data for the form.
     * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
     * @return  mixed  A JForm object on success, false on failure
     */
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        if (!isset($this->form_name))$this->form_name='form'.md5(rand(0,1000));
        $form = $this->loadForm($this->form_name, $this->str_xml, array('control' => 'jform', 'load_data' => false));
        if (empty($form)) return false;
        return $this->form=$form;
    }

    /**
     * Method to check data via jForm->validate
     *
     * @param    array    $data        An array of data (post data) to be validated
     * @return    bool              true on success, false on failure
     * @since    1.6
     */
    function check($data)
    {
        $form=$this->getForm();
        if (1==$form->validate($data)) return true;
        else
        {
            $this->formErrors=$form->getErrors();
            return false;   
        }
       
    }
    /**
     * Method to bind data back to form after failed validation
     *
     * @param    array    $data        An array of data (post data) to be filled in form
     * @return    bool              true on success, false on failure
     * @since    1.6
     */
    public function fillForm($data)
    {
        //echo "<pre>";print_r($data);
        $form=$this->getForm();
        if ($form->bind($data)) return $this->form=$form; else return false;
    }
}

hoochicken

unread,
Dec 18, 2013, 10:17:34 AM12/18/13
to joomla-de...@googlegroups.com
Hi,
it's quite easy, 3 lines in module file; and some in the templates file, see below:-)

(1) module core file:
$filepath=dirname(__FILE__).'/form/form.xml'; /*put your file in ROOT/modules/mod_yourModule/form/form.xml*/
$form=new JForm('mod_yourModule');
$form->loadFile($filepath);

(2) template file
<form action="<?php echo JRoute::_('index.php', true, $params->get('usesecure')); ?>" method="post" id="login-form">
        <?php
        foreach ($form->getFieldsets() as $fieldset):
            $fields = $form->getFieldset($fieldset->name);
            echo '<fieldset id="'.$fieldset->name.'"';
            if (isset($fieldset->class)) echo ' class="'.$fieldset->class.'"';
            echo '>';
            if (isset($fieldset->label) AND ""!=$fieldset->label) echo '<legend id="legend'.$fieldset->name.'">'.$fieldset->label.'</legend>';
            foreach($fields as $field):
                if ($field->hidden): echo $field->input;
                else: ?>
                    <div class="control-group <?php echo $field->id;?>">
                        <?php echo preg_replace('/class="/', 'class="control-label ',$field->label,1); ?>
                        <div class="controls <?php echo $field->id;?>">
                            <?php echo $field->input;?>
                        </div>
                    </div>
                    <?php endif;
            endforeach;
            echo '</fieldset>';
        endforeach; ?>
</form>

(3) Further links
http://docs.joomla.org/API17:JForm
http://docs.joomla.org/API17:JForm::loadFile
http://docs.joomla.org/API17:JForm::load
http://docs.joomla.org/Standard_form_field_and_parameter_types

(4) Enjoy
Mareike

nant

unread,
Dec 20, 2013, 2:59:01 AM12/20/13
to joomla-de...@googlegroups.com
Thanks for your reply!

The qjform module looks promising.

I tried to install the latest version on J2.5.16 and when I try to save the parameters to publish it I get a popup invalid form error.

nant

unread,
Dec 20, 2013, 3:03:08 AM12/20/13
to joomla-de...@googlegroups.com
Nevermind - I read your FAQ message on the webpage.

nant

unread,
Dec 20, 2013, 4:27:54 AM12/20/13
to joomla-de...@googlegroups.com
Greetings
Mareike!

Thanks for feedback - if possible can you contact me on skype - nickanti - I have some suggestions and code that might enhance your module.

Nick

hoochicken

unread,
Dec 20, 2013, 6:50:29 AM12/20/13
to joomla-de...@googlegroups.com
Hi Nick,
just contact me via the website with info@...
(I think it is appreciated not to abuse the forum for support purposes;-)
Greetings
Mareike

nant

unread,
Dec 20, 2013, 8:20:35 AM12/20/13
to joomla-de...@googlegroups.com
Ok, this seems to load the form and display it in the module.

But now submit button or method to get data.
Reply all
Reply to author
Forward
0 new messages