Dia
Dia

Reputation: 155

Where should I place the form handler?

The following piece of code has been taken from the octobercms documentation ..

{{ form_open({ request: 'onHandleForm' }) }}
    Please enter a string: <input type="text" name="value"/>
    <input type="submit" value="Submit me!"/>
{{ form_close() }}
<p>Last submitted value: {{ lastValue }}</p>

What I want to know is: Where should the function 'onHandleForm()' be placed?

Upvotes: 1

Views: 113

Answers (1)

trollkotze
trollkotze

Reputation: 1139

The following piece of code has been taken from the octobercms documentation ..

It can be helpful in such cases to provide a link to show where exactly in the documentation this is from, to have some context.

The documentation page in question actually gives a quite clear explanation:

The onHandleForm function can be defined in the page or layout PHP section in the following way:

function onHandleForm()
{
    $this['lastValue'] = post('value');
}

The linked documentation snippet about the PHP section then explains where PHP code is supposed to go.

From the given example there:

url = "/blog"
layout = "default"
==
<?
function onStart()
{
    $this['posts'] = ...;
}
?>
==
<h3>Blog archive</h3>
{% for post in posts %}
    <h4>{{ post.title }}</h4>
    {{ post.content }}
{% endfor %}

This is an example for a CMS page template. The PHP section is in between the two

==

lines - after the configuration section, and before the twig markup section. Quite the same for partial and layout templates.

Since the PHP section is optional, a template could also look like this:

url = "/blog"
layout = "default"
==
<h3>Blog archive</h3>
{% for post in posts %}
    <h4>{{ post.title }}</h4>
    {{ post.content }}
{% endfor %}

In this case, there is only one

==

line, separating the configuration section from the twig markup section. October will somehow automatically try to make sense of it by the given structure.

If you are not working with the files directly but through the October Web backend in the browser, you can switch between the twig markup section and the PHP code section through tabs above the code editor, with the lables Markup and Code. See the screnshots below (can't include any more actual links, due to not enough rep):

(If you'd like to have a dedicated stackechange community for OctoberCMS, plese support the octobercms.stackexchange proposal under area51.stackexchange.com/proposals/97119/octobercms. I'll remove this annoing message, once that goal is achieved - promise :) )

Upvotes: 1

Related Questions