In Joomla from 2.5 to be done in Class JForm to add to the convenience. Flexibility to create a form And running through the field with field preparation of standard form. I run several at Standard form field types by building takes the form of XML.
creating forms in the models folder and create a file. hello.xml model is built in as root and use the tag form to consist in the field to take the field before the main attribute like name Class Label Type Required.
com_helloworld/models/forms/hello.xml
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset name="hello_form">
<field
name="name"
type="text"
class="inputbox"
label="Name"
required="true"
/>
<field
name="detail"
type="textarea"
class="inputbox"
label="Detail"
required="true"
rows="5"
/>
</fieldset>
</form>
- Create hello.php file in the models folder, open and code.
com_helloworld/models/hello.php
<?php
defined('_JEXEC') or die('Restricted access');
class HelloworldModelHello extends JModelForm
{
function getForm($data = array(), $loadData = true)
{
$options = array('control' => 'jform', 'load_data' => $loadData);
$form = $this->loadForm('com_helloworld.hello', 'hello', $options);
if (empty($form)) {
return false;
}
return $form;
}
}
?>
Modify files in the folder view.html.php views/hello.
com_helloworld/views/hello/view.html.php
<?php
defined('_JEXEC') or die('Restricted access');
class HelloworldViewHello extends JViewLegacy
{
function display($tpl = null)
{
$this->form = $this->get('Form');
$this->addToolbar();
parent::display($tpl);
}
function addToolbar()
{
JToolbarHelper::save();
JToolbarHelper::cancel();
}
}
-Edit the default.php file in the tmpl.
com_helloworld/views/hello/tmpl/default.php
<?php
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.keepalive');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<script>
Joomla.submitbutton = function(task) {
if (task == 'cancel' || document.formvalidator.isValid(document.id('adminForm'))) {
Joomla.submitform(task, document.getElementById('adminForm'));
} else {
alert('Form Invaild');
}
}
</script>
<form action="<?php echo JRoute::_('index.php?option=com_helloworld&c=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 JHtml::_('form.token'); ?>
</fieldset>
</form>
?>
Summary File:
com_helloworld/helloworld.php
com_helloworld/controllers/hello.php
com_helloworld/models/hello.php
com_helloworld/models/forms/hello.xml
com_helloworld/views/hello/view.html.php
com_helloworld/views/hello/tmpl/default.php
Display:
Chapter 10 - Saving data into database Component Development