form()

October CMS Documentation Docs

form()

Functions prefixed with form_ perform tasks that are useful when dealing with forms. The helper maps directly to the Form PHP class and its methods. For example:

___TWIG0___

is the PHP equivalent of the following:

<?= Form::close() ?>

Note: Methods in camelCase should be converted to snake_case.

# form_open()

Outputs a standard <form> opening tag along with the _session_key and _token hidden fields for CSRF protection. If you are using the AJAX Framework, it is recommended that you use form_ajax() instead.

___TWIG0___

Attributes can be passed in the first argument.

___TWIG0___

The above example would output as the following:

<form class="form-horizontal">

There are some special options that can also be used alongside the attributes.

___TWIG0___

The function support the following options:

OptionDescription
methodRequest method. Corresponds to the method FORM tag attribute. Eg: POST, GET, PUT, DELETE
requestA handler name to execute on the server when the form is posted. See the Handling forms article for details about the event handlers.
urlSpecifies URL to post the form to. Corresponds to the action FORM tag attribute.
filesDetermines whether the form will submit files. Accepted values: true and false.
modelA model object for the form model binding.

# form_ajax()

Outputs an AJAX enabled FORM opening tag. The first parameter of the form_ajax() function is the AJAX handler name. The handler can be defined in the layout or page PHP section code, it can also be defined in a component. You may find more information about AJAX in the AJAX Framework article.

___TWIG0___

Attributes can be passed in the second argument.

___TWIG0___

The above example would output as the following:

<form data-request="onSave" class="form-horizontal">

There are some special options that can also be used alongside the attributes.

___TWIG0___

___TWIG1___

Note: When attempting to reference a component's alias with __SELF__ as an argument to form_ajax() you must first build the string you wish to use outside of the call itself. Example:

___TWIG0___
___TWIG1___

The function support the following options:

OptionDescription
successJavaScript string to execute on successful result.
errorJavaScript string to execute on failed result.
confirmA confirmation message to display before sending the request.
redirectOn successful result, redirect to a URL.
updateAn array of partials to update on success in the following format: { 'partial': '#element' }.
dataExtra data to include with the request in the following format: { 'myvar': 'myvalue' }.

# form_close()

Outputs a standard FORM closing tag. This tag is generally available to provide consistency in usage.

___TWIG0___

The above example would output as the following:

</form>

# Passing attributes to the generated element

You can pass additional attributes to the Form::open() method by passing an array of attribute names and values to be rendered on the final generated <form> element.

<?= Form::open(array('id' => 'example', 'class' => 'something')) ?>
    // ..
<?= Form::close() ?>

The above example would output the following:

<form method="POST" action="" accept-charset="UTF-8" id="example" class="something">

</form>