silverstripe-framework/docs/en/02_Developer_Guides/03_Forms/How_Tos/02_Lightweight_Form.md
Aaron Carlino 6888901468
NEW: Update docs to be compliant with Gatsby site (#9314)
* First cut

* Temporarily disable composer.json for netlify build

* POC

* New recursive directory query, various refinements

* Fix flexbox

* new styled components plugin

* Apply frontmatter delimiters

* Mobile styles, animation

* Search

* Redesign, clean up

* Nuke the cache, try again

* fix file casing

* Remove production env file

* ID headers

* Move app to new repo

* Add frontmatter universally

* Hide children changelogs

* Add how to title

* New callout tags

* Revert inline code block change

* Replace note callouts

* Fix icons

* Repalce images

* Fix icon

* Fix image links

* Use proper SQL icon
2019-11-18 17:58:33 +13:00

1.5 KiB

title summary iconBrand
How to Create Lightweight Form Create a simple search form with Silverstripe CMS wpforms

How to Create Lightweight Form

Out of the box, SilverStripe provides a robust and reusable set of HTML markup for FormField, however this can sometimes produce markup which is unnecessarily bloated.

For example, a basic search form. We want to use the Form API to handle the form but we may want to provide a totally custom template to meet our needs. To do this, we'll provide the class with a unique template through setTemplate.

app/code/Page.php

public function SearchForm() 
{
    $fields = new FieldList(
        TextField::create('q')
    );

    $actions = new FieldList(
        FormAction::create('doSearch', 'Search')
    );

    $form = new Form($this, 'SearchForm', $fields, $actions);
    $form->setTemplate('SearchForm');

    return $form;
}

app/templates/Includes/SearchForm.ss

<form $FormAttributes>
    <fieldset>
        $Fields.dataFieldByName(q)
    </fieldset>
    
    <div class="Actions">
        <% loop $Actions %>$Field<% end_loop %>
    </div>
</form>

SearchForm.ss will be executed within the scope of the Form object so has access to any of the methods and properties on Form such as $Fields and $Actions.

[notice] To understand more about Scope or the syntax for custom templates, read the Templates guide. [/notice]