m33ts4k0z
m33ts4k0z

Reputation: 411

Joomla 3: JForm class. How to submit a form created by it?

Looking around the net I have found ways to create a form in Joomla 3.x and that works pretty well.

Here is my php code:

$form   =& JForm::getInstance('myform','form.xml');
$fieldSets = $form->getFieldsets();
foreach ($fieldSets as $name => $fieldSet) :
?>          
<?php
foreach ($form->getFieldset($name) as $field):
?>
    <p>
    <?php if (!$field->hidden) : ?>
    <span class="formlabel"><?php echo $field->label; ?></span> 
    <?php endif; ?>
    <span class="control"><?php echo $field->input; ?></span>
    </p>
<?php
endforeach;
?>  
<div class="clr"></div>
<?php
endforeach;

And here is my form.xml:

<?xml version="1.0" encoding="utf-8"?>
<form class="form-validate">
<fieldset name="information">        
     <field id="name"
        name="name"
        type="text"
        label="Namn"
        description=""
        class="inputbox"
        size="30"
        default=""
        required="true"
    />

    <field id="aftername"
        name="aftername"
        type="text"
        label="Efternamn"
        description=""
        class="inputbox"
        size="30"
        default=""
        required="true"
    />

    <field id="email"
        name="email"
        type="text"
        label="E-Post Adress"
        description=""
        class="inputbox validate-email"
        size="30"
        default=""
        required="true"
    />          

     <field
        name="captcha"
        type="captcha"
        label="Type the numbers"
        description="COM_CONTACT_CAPTCHA_DESC"
        validate="captcha"
    />

</fieldset> 

</form>

Actually all these are working fine. I see the fields and the captcha and they render correctly. The problem is that trying to add a submit button following the Joomla documentation, doesnt do anything when I click it. My component is just a php file without views folder and templates or anything. Is that a wrong approach to create the component or will a simple form work like this anyway?

Thanks in advance

Upvotes: 4

Views: 6164

Answers (2)

Eduardo Brasil
Eduardo Brasil

Reputation: 21

I had the same problem and researching the topic I found your question! The answer did not help me much, so I made some efforts to address the issue and got the following result ...

    $fieldsets = $this->form->getFieldsets(); 
    foreach ($fieldsets as $fieldset) {
        echo '<h1>' . $fieldset->name . '</h1>';
        $fields = $this->form->getFieldset($fieldset->name);
        foreach ($fields as $field) {
            echo $field->label;
            echo $field->input;
        }
    }

Attention Line: "$this->form->getFieldset($fieldset->name)"

I see that in your code missed the point attribute "name".

Upvotes: 2

Emerson Rocha
Emerson Rocha

Reputation: 558

JForm is complicated to explain in a stand alone way. Your question involves not only him, but the structure of an entire component.

JForm is used to render forms, and allow reuse of fields, even complex. To see a complete sample, you better follow or an example of com_helloword with Joomla version 1.6 and above or look the source code of Joomla native components.

Upvotes: -1

Related Questions