Creating Form Component Development

Creating a form component in Joomla 5 involves a systematic approach to defining, structuring, and coding the component files. In this guide, we will walk through the process of developing a simple form component named "com_hello." This includes setting up the necessary folders and files, writing the XML manifest, and coding the component's controller, model, and view. By following these steps, you will be able to create a functional Joomla componenet that can handle form submissions and display data.

 

Create a folder "com_hello" in Administrator/Components and create

  • com_hello/hello.xml
  • com_hello/hello.php
  • com_hello/controllers/hello_controller.php
  • com_hello/models/forms/hello.xml
  • com_hello/models/hello.php
  • com_hello/views/hello/view.html.php
  • com_hello/views/hello/tmpl/default.php
  • com_hello/languages/en-GB/en-GB.com_hello.ini

After creating the files, you have to add the following codes in the related files:

in "com_hello/hello.xml"

<?xml version="1.0" encoding="UTF-8" ?>
<extension type="component" version="5.0.0" method="upgrade">
    <name>com_hello</name>
    <author>Your Name</author>
    <creationDate>2024-06-20</creationDate>
    <copyright>Your Company</copyright>
    <license>GPL</license>
    <version>1.0.0</version>
    <description>Hello Component for Joomla 5</description>
    <files>
        <folder>controllers</folder>
        <folder>models</folder>
        <folder>views</folder>
        <filename>hello.php</filename>
    </files>
    <languages>
        <language tag="en-GB">languages/en-GB/en-GB.com_hello.ini</language>
    </languages>
    <administration>
        <menu>Hello Component</menu>
    </administration>
</extension>

 

in "com_hello/hello.php"

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

use Joomla\CMS\Factory;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Controller\BaseController;

$app = Factory::getApplication();
$input = $app->input;

ComponentHelper::getComponent('com_hello');

$controllerName = $input->getCmd('controller', 'hello');
$controllerName = $controllerName ?: 'hello';
$controllerClass = 'HelloController' . ucfirst($controllerName);

$controllerPath = __DIR__ . '/controllers/' . strtolower($controllerName) . '_controller.php';

if (file_exists($controllerPath)) {
    require_once $controllerPath;
}

if (!class_exists($controllerClass)) {
    $controllerClass = 'HelloController';
    require_once __DIR__ . '/controllers/hello_controller.php';
}

$controller = new $controllerClass();
$controller->execute($input->get('task'));
$controller->redirect();

 

in "com_hello/controllers/hello_controller.php"

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

use Joomla\CMS\MVC\Controller\BaseController;

class HelloController extends BaseController
{
    public function display($cachable = false, $urlparams = array())
    {
        $viewName = $this->input->getCmd('view', 'hello');
        $this->input->set('view', $viewName);
        parent::display($cachable, $urlparams);
    }
}

 

 

in "com_hello/models/forms/hello.xml"

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fieldset name="hello_form">
        <field
            name="name"
            type="text"
            class="inputbox w-25"
            label="COM_HELLO_NAME_LABEL"
            description="COM_HELLO_NAME_DESC"
            required="true"
        />
        <field
            name="detail"
            type="textarea"
            class="inputbox w-25"
            label="COM_HELLO_DETAIL_LABEL"
            description="COM_HELLO_DETAIL_DESC"
            required="true"
            rows="5"
        />
    </fieldset>
</form>

 

in "com_hello/models/hello.php"

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

use Joomla\CMS\MVC\Model\FormModel;

class HelloModelHello extends FormModel
{
    public function getForm($data = array(), $loadData = true)
    {
        $options = array('control' => 'jform', 'load_data' => $loadData);

        $form = $this->loadForm('com_hello.hello', 'hello', $options);

        if (empty($form)) {
            return false;
        }

        return $form;
    }
}

 

in "com_hello/views/hello/view.html.php"

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

use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\ToolbarHelper;
use Joomla\CMS\Factory;

class HelloViewHello extends BaseHtmlView
{
    function display($tpl = null)
    {
        $this->form = $this->get('Form');
        $this->addToolbar();
        parent::display($tpl);
    }

    function addToolbar()
    {
        ToolbarHelper::save();
        ToolbarHelper::cancel();
    }
}

 

 in "com_hello/views/hello/tmpl/default.php"

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

use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Router\Route;

HTMLHelper::_('behavior.keepalive');
HTMLHelper::_('behavior.formvalidator');
?>

<script>
    Joomla.submitbutton = function(task) {
        if (task == 'cancel' || document.formvalidator.isValid(document.getElementById('adminForm'))) {
            Joomla.submitform(task, document.getElementById('adminForm'));
        } else {
            alert('Form Invalid');
        }
    }
</script>

<form action="<?php echo Route::_('index.php?option=com_hello&view=hello'); ?>" name="adminForm" id="adminForm" method="post" class="form-validate form-horizontal">
    <fieldset>
        <?php foreach ($this->form->getFieldset('hello_form') as $field) { ?>
            <div class="control-group">
                <div class="control-label">
                    <?php echo $field->label; ?>
                </div>
                <div class="controls">
                    <?php echo $field->input; ?>
                </div>
            </div>
        <?php } ?>
        <input type="hidden" name="task" />
        <?php echo HTMLHelper::_('form.token'); ?>
    </fieldset>
</form>

 

in "com_hello/languages/en-GB/en-GB.com_hello.ini"

COM_HELLO_NAME_LABEL="Name"
COM_HELLO_NAME_DESC="Enter your name"
COM_HELLO_DETAIL_LABEL="Detail"
COM_HELLO_DETAIL_DESC="Enter details"
COM_HELLO_SUBMIT="Submit"
COM_HELLO_CANCEL="Cancel"

 

After you have written these codes and installed the "com_hello" component, you will see the result,

 

Result Form
Result Form

 

By following the steps outlined in this guide, you have successfully created a custom Joomla 5 components named "com_hello." This component includes a structured folder layout, a well-defined XML manifest, and properly coded MVC elements. Upon installation, the "com_hello" component will display a functional form that handles user input. This foundational knowledge equips you to develop more complex components and extend the functionality of Joomla CMS to meet your specific needs.

 


 

Title
Joomla_5_chapter_12 Sample Code
Categories
Joomla Extension Dev, Joomla Extension Dev
Hits
434
Create by
Raja Pdl
Create date
2024-08-16 14:29:18
Description

Joomla is a popular content management system (CMS) that allows you to build websites and powerful online applications. In Joomla , we can create our custom component . This is the sample codes of creating form in custom component development of Joomla 5 Chapter 12 - Creatin Form Coomponent Development tutorial

Joomla_5_chapter_12.zip

Files extension zip

Files size 5.79 KB

Upload by Raja Pdl

Upload date 2024-08-16 07:28:49


 I recommend placing the com_hello file in the components folder on the admin side and using the "Discover" method instead.

Chapter 13 - Saving Data Into Database In Joomla 5


References:

How to Create Joomla! 5 Component - https://www.techfry.com/resources/how-to-create-joomla-component 

Joomla! Coding Standards - https://developer.joomla.org/coding-standards/php-code.html 

Absolute Basics of How a Component Functions - https://docs.joomla.org/Absolute_Basics_of_How_a_Component_Functions 

Recent Topics
Subject
Post Reply
Open
Recent
Question about the deduction calculator
By donnywagner Mon 17 Feb 2025 8:30 am Board English Language
1
125
Mon 17 Feb 2025 3:37 pm By mindphp View Topic Question about the deduction calculator
MooZiicart How can I set canonical in 5.2.4 without using a plugin?
By Anonymous Mon 17 Feb 2025 2:50 am Board English Language
0
100
Mon 17 Feb 2025 2:50 am By Anonymous View Topic MooZiicart How can I set canonical in 5.2.4 without using a plugin?
Custom component Joomla - Add a Tab (Association) in my custom component
By Anonymous Fri 10 Jan 2025 5:12 am Board English Language
1
235
Fri 10 Jan 2025 11:54 am By mindphp View Topic Custom component Joomla - Add a Tab (Association) in my custom component
Q - How to prevent from auto date, time selection in calendar input field of Joomla 5 custom component ?
By Raja Pdl Tue 07 Jan 2025 11:43 pm Board English Language
0
259
Tue 07 Jan 2025 11:43 pm By Raja Pdl View Topic Q - How to prevent from auto date, time selection in calendar input field of Joomla 5 custom component ?
Q - How to display date and time value in Joomla 5 xml form filed in custom component ?
By Raja Pdl Sun 05 Jan 2025 9:59 pm Board English Language
8
679
Wed 05 Feb 2025 10:13 pm By Raja Pdl View Topic Q - How to display date and time value in Joomla 5 xml form filed in custom component ?
Error 404 after installing Chapter 12 example - joomla5 component-development
By christian Sun 05 Jan 2025 7:38 pm Board English Language
4
348
Mon 06 Jan 2025 9:42 pm By christian View Topic Error 404 after installing Chapter 12 example - joomla5 component-development
Q - How to display frontend menu in Joomla 5 custom component ?
By Raja Pdl Thu 02 Jan 2025 12:14 am Board English Language
5
574
Mon 06 Jan 2025 11:41 pm By Raja Pdl View Topic Q - How to display frontend menu in Joomla 5 custom component ?
Q: How to display a select box (type: list) in a Joomla XML file to meet Joomla 5 standards?
By Raja Pdl Thu 26 Dec 2024 11:18 pm Board English Language
2
633
Fri 27 Dec 2024 9:39 pm By Raja Pdl View Topic Q: How to display a select box (type: list) in a Joomla XML file to meet Joomla 5 standards?
Run Joomla in Swoole ?
By Anonymous Fri 29 Nov 2024 5:33 pm Board English Language
4
302
Thu 26 Dec 2024 10:19 am By mindphp View Topic Run Joomla in Swoole ?
Q - How to change default language for not logged-in users in phpBB3 ?
By Raja Pdl Tue 26 Nov 2024 9:35 am Board English Language
1
437
Tue 26 Nov 2024 10:01 am By Raja Pdl View Topic Q - How to change default language for not logged-in users in phpBB3 ?
Q: How to restrict the Joomla component name field to accept only characters according to Joomla's naming conventions ?
By Raja Pdl Tue 19 Nov 2024 12:05 pm Board English Language
2
332
Tue 19 Nov 2024 12:39 pm By Raja Pdl View Topic Q: How to restrict the Joomla component name field to accept only characters according to Joomla's naming conventions ?
Q - How to avoid horizontal scrolling and how to fix in the screen in Joomla 5 custom component with CSS ?
By Raja Pdl Thu 14 Nov 2024 3:57 pm Board English Language
1
360
Thu 14 Nov 2024 4:11 pm By Raja Pdl View Topic Q - How to avoid horizontal scrolling and how to fix in the screen in Joomla 5 custom component with CSS ?
Q - How to remove border bottom of dates in list view of in FullCalendar JS Library ?
By Raja Pdl Thu 14 Nov 2024 10:06 am Board English Language
5
348
Thu 14 Nov 2024 4:15 pm By Raja Pdl View Topic Q - How to remove border bottom of dates in list view of in FullCalendar JS Library ?
Q - How to create update sql file and how to connect that sql file when updating component in Joomla 5 ?
By Raja Pdl Wed 13 Nov 2024 3:52 pm Board English Language
11
735
Fri 15 Nov 2024 9:20 am By Raja Pdl View Topic Q - How to create update sql file and how to connect that sql file when updating component in Joomla 5 ?
Q - How to add only publish, unpublish options in filter type status of Joomla 5 component ?
By Raja Pdl Wed 13 Nov 2024 1:40 pm Board English Language
1
195
Wed 13 Nov 2024 1:45 pm By Raja Pdl View Topic Q - How to add only publish, unpublish options in filter type status of Joomla 5 component ?
Q - How to Implement a Calendar Input Field in Joomla 5 Custom Component and Submit Form Only When a Date is Selected ?
By Raja Pdl Wed 13 Nov 2024 12:38 pm Board English Language
9
476
Fri 15 Nov 2024 5:37 pm By tsukasaz View Topic Q - How to Implement a Calendar Input Field in Joomla 5 Custom Component and Submit Form Only When a Date is Selected ?
Q - What Length of VARCHAR Should I Use to Store an IPv6 Address?
By Raja Pdl Mon 11 Nov 2024 2:37 pm Board English Language
1
350
Mon 11 Nov 2024 2:50 pm By Raja Pdl View Topic Q - What Length of VARCHAR Should I Use to Store an IPv6 Address?
Q - How to display am, pm in week and day view of FullCalendar JS Library in locale "th" ?
By Raja Pdl Sat 09 Nov 2024 10:42 am Board English Language
3
253
Sun 10 Nov 2024 10:57 pm By Raja Pdl View Topic Q - How to display am, pm in week and day view of FullCalendar JS Library in locale "th" ?
Q - How to change date and time format in datetime-local input field ?
By Raja Pdl Fri 08 Nov 2024 4:35 pm Board English Language
2
577
Sat 09 Nov 2024 9:57 am By Raja Pdl View Topic Q - How to change date and time format in datetime-local input field ?
Q - How to add tabs buttons in Joomla 5 custom module and assign modules in that tabs ?
By Raja Pdl Thu 24 Oct 2024 5:57 pm Board English Language
1
230
Fri 25 Oct 2024 9:11 pm By Raja Pdl View Topic Q - How to add tabs buttons in Joomla 5 custom module and assign modules in that tabs ?