Display Form in Frontend

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
zockerwurf
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Thu Apr 28, 2016 6:51 am

Display Form in Frontend

Post by zockerwurf » Thu Apr 28, 2016 7:16 am

Hi guys.

I tried to display a form in the frontend. But i always get the error:

Code: Select all

Fatal error: Call to undefined method mvcModelmvc::loadForm() in /home/usr/bksolution/public_html/components/com_mvc/models/mvc.php on line 19
Let me show you my structure:
  • com_mvc
    com_mvc/mvc.xml
    com_mvc/admin
    com_mvc/site
    com_mvc/site/controller.php
    com_mvc/site/mvc.php
    com_mvc/site/models
    com_mvc/site/models/mvc.php
    com_mvc/site/models/forms
    com_mvc/site/models/forms/form.xml
    com_mvc/site/views
    com_mvc/site/views/mvc
    com_mvc/site/views/mvc/view.html.php
    com_mvc/site/views/mvc/tmpl
    com_mvc/site/views/mvc/tmpl/default.php
And the files:
com_mvc/site/controller.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla controller library
jimport('joomla.application.component.controller');
 
/**
 * Workshop Component Controller
 */
class MvcController extends JController
{
}

?>
com_mvc/site/mvc.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import joomla controller library
jimport('joomla.application.component.controller');
 
// Get an instance of the controller prefixed by Workshop
$controller = JController::getInstance('Mvc');
 
// Perform the Request task
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();

?>
com_mvc/site/models/mvc.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

class mvcModelmvc extends JModelItem
{
 
        /**
         * Method to get the form.
         *
         * @access      public
         * @return      mixed   JForm object on success, false on failure.
         */
        public function getForm($data = array(), $loadData = true) 
        {
                $form = $this->loadForm(
                                        'com_mvc.mvc',
                                        'form',
                                        array('control' => 'jform', 'load_data' => $loadData)
                                       );
                return $form;
        }
}

?>
com_mvc/site/models/forms/form.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<form>
        <fieldset name="primary_fieldset">
                <field
                        id="id"
                        name="id"
                        type="hidden"
                />
                <field
                	id="primary"
                        name="primary"
                        type="list"
                        label="Primary Selection"
                        default="red">
                        <option value="red">Red</option>
                        <option value="blue">Blue</option>
                </field>
        </fieldset>
        <fieldset name="secondary_fieldset">
                <field
                        id="id"
                        name="id"
                        type="hidden"
                />
                <field
                	id="secondary"
                        name="secondary"
                        type="list"
                        label="Secondary Selection"
                        default="green">
                        <option value="green">Green</option>
                        <option value="yellow">Yellow</option>
                </field>
        </fieldset>
</form>
com_mvc/site/views/mvc/view.html.php

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('joomla.application.component.view');


class mvcViewmvc extends JView
{
        /**
         * View form
         *
         * @var         form
         */         
        protected $form = null;
        /**
         * display method of MVC view
         * @return void
         */
        public function display($tpl = null) 
        {
                // get the Form
                $form = & $this->get('Form');
                // get the Data
                if (count($errors = $this->get('Errors'))) 
                {
                        JError::raiseError(500, implode("<br />", $errors));
                        return;
                }
                // Assign the form
                $this->form = $form;
                // Display the template
                parent::display();
        }
}

?>
com_mvc/site/views/mvc/tmpl/default.php

Code: Select all

<div class="first-fieldset">
    <form name="formname">
        <fieldset class="adminform">
            <legend>Primary Fields</legend>
            <?php foreach ($this->form->getFieldset('primary_fieldset') as $field): ?>
                <?php echo $field->label; ?>
                <?php echo $field->input; ?>
            <?php endforeach; ?>
        </fieldset>
    </form>
</div>

Can anyone tell me, why I get this error? I did exactly how it was in the documentation: https://docs.joomla.org/API16:JForm/getFieldset
Last edited by toivo on Sun May 01, 2016 7:49 am, edited 1 time in total.
Reason: mod note: moved to 2.5 Coding

User avatar
toivo
Joomla! Exemplar
Joomla! Exemplar
Posts: 9838
Joined: Thu Feb 15, 2007 5:48 am
Location: Oxford, UK
Contact:

Re: Display Form in Frontend

Post by toivo » Sun May 01, 2016 10:02 am

The method loadForm() will become available if your model class extends JModelForm.

This tutorial, even if it is out-of-date, shows how: https://docs.joomla.org/J2.5:Developing ... e_function

As a general comment, rather than investing time in 2.5, it would be better to focus on 3.x.
Toivo Talikka, Global Moderator
my first programs were assembled and run in 16KB :)
troubleshooting smtp and other articles https://talikka.com/joomla


Locked

Return to “Joomla! 2.5 Coding”