Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem when a template is changed #19

Closed
lionel-m opened this issue Jan 3, 2013 · 7 comments
Closed

Problem when a template is changed #19

lionel-m opened this issue Jan 3, 2013 · 7 comments

Comments

@lionel-m
Copy link

lionel-m commented Jan 3, 2013

Hi,

I have a problem when I change a template. To explain this, my module display a search form and a link for creating a new job :

First function :

        // Search form
        $arrFields = array
        (
            'contract_type' => array
            (
                ...
            ),
            'categories' => array
            (
                ...
            )
        );

        // First param is the form id
        $objForm = new HasteForm('search_jobs', $arrFields);

        // The submit button
        $objForm->submit = $GLOBALS['TL_LANG']['MSC']['form_search'];

        // Get the form as string
        $objForm->addFormToTemplate($this->Template);

        // Validate() also checks whether the form has been submitted
        if ($objForm->validate())
        {
            // Fetch all form data
            $arrData = $objForm->fetchAll();
        }

       ...

If I use this link I change the template with a new form.

Second function :

$arrFields = array
        (
            'title' => array
            (
                ...
            ),
            'description' => array
            (
               ...
            )
            ...
        );

        // Change the template
        $this->Template = new FrontendTemplate('jb_jobForm');

        // Add "tr" and "td" tags on each field of the form
        foreach ($arrFields as $key => $value) {
            $arrFields[$key]['eval']['tableless'] = FALSE;
        }

        // First param is the form id
        $objForm = new HasteForm('job_offer', $arrFields);

        // The submit button
        $objForm->submit = $GLOBALS['TL_LANG']['MSC']['form_submit'];

        // Get the form as string
        $objForm->addFormToTemplate($this->Template);

        // Validate() also checks whether the form has been submitted
        if ($objForm->validate())
        {
            // Fetch all form data
            $arrData = $objForm->fetchAll();

            ...

The template is changed. That's OK. But not the form. The first form is inserted into the second form.

Normally I should have "title", "description", ... but I have "contract_type", "categories", ... and the id of the form is not changed.

Do you have an idea of the problem ?

Thx

@qzminski
Copy link
Member

qzminski commented Jan 3, 2013

No idea what could be wrong. Does echo $objForm->generateForm() also outpus the wrong form?

@lionel-m
Copy link
Author

lionel-m commented Jan 3, 2013

I inserted this function in the code and it returns me the correct form !

// Get the form as string
        $objForm->addFormToTemplate($this->Template);

        echo $objForm->generateForm() ;

And $this->Template = jb_jobForm ( FrontendTemplate Object ( [strTemplate:protected] => jb_jobForm)

But I have the first form in the frontend... The first overwrites the second.

I use this code on each template

<div class="form">
        <form action="<?php echo $this->action; ?>" method="post">
                <div class="formbody">
                    <input type="hidden" name="FORM_SUBMIT" value="<?php echo $this->formId; ?>" />
                    <input type="hidden" name="REQUEST_TOKEN" value="{{request_token}}" />

                    <table summary="Form fields">
                        <?php foreach ($this->fields as $objWidget): ?>

                            <?php echo $objWidget->parse(); ?>

                        <?php endforeach; ?>
                    </table>

                    <div class="submit_container">
                        <input type="submit" class="submit" value="<?php echo $this->submit; ?>" />
                    </div>
                </div>
        </form>
    </div>

@lionel-m
Copy link
Author

lionel-m commented Jan 3, 2013

In the second form I use a table. We can see with Firebug that has not worked well and the formId is the first...

firebug

@qzminski
Copy link
Member

qzminski commented Jan 3, 2013

I think there is some mistake in your code. Maybe try to do the following:

$objTemplate = new FrontendTemplate('jb_jobForm');

// Initialize form, etc.

$objForm->addFormToTemplate($objTemplate);
$this->Template = $objTemplate;

@lionel-m
Copy link
Author

lionel-m commented Jan 3, 2013

No result...

I use three forms with Haste and if I remove the first, the other forms work perfectly. The method for changing the template is the same...

Thank you for the help.

@qzminski
Copy link
Member

qzminski commented Jan 3, 2013

I suggest to make independent templates for each form. This way you will avoid strange errors like above.

$objTemplateSearch
$objFormSearch
// ...
$objTemplateJob
$objFormJob

// And in the main template, depending on the situation:
$this->Template->form = $objTemplateSearch->parse();

@lionel-m
Copy link
Author

lionel-m commented Jan 4, 2013

Hi,

I found a solution by replacing in the main template :

$objForm->addFormToTemplate($this->Template);

With

$this->Template->form = $objForm->generateForm();

It works...

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants