mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-09-08 10:41:25 +02:00
fdbd899766
- Remaining Developer Guides and Upgrading - SilverStripe in a namespace or api has not been change - To keep PRs easier no formatting was changed Update merge conflics with two files Update Silverstripe Ltd, Silverstripe Cloud and Silverstripe CMS Silverstripe CMS Ltd > Silverstripe Ltd Silverstripe CMS Platform > Silverstripe Cloud Silverstripe CMS Framework > Silverstripe CMS Resolve merge conflict Remove Framework from Silverstripe CMS Framework - 3 files Change SilverStripe CMS to Silverstripe CMS
60 lines
1.5 KiB
Markdown
60 lines
1.5 KiB
Markdown
---
|
|
title: How to Create Lightweight Form
|
|
summary: Create a simple search form with Silverstripe CMS
|
|
iconBrand: wpforms
|
|
---
|
|
|
|
# How to Create Lightweight Form
|
|
|
|
Out of the box, Silverstripe CMS provides a robust and reusable set of HTML markup for [FormField](api:SilverStripe\Forms\FormField), however this can
|
|
sometimes produce markup which is unnecessarily bloated.
|
|
|
|
For example, a basic search form. We want to use the [Form](api:SilverStripe\Forms\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**
|
|
|
|
|
|
```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**
|
|
|
|
|
|
```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](api:SilverStripe\Forms\Form) such as `$Fields` and `$Actions`.
|
|
|
|
[notice]
|
|
To understand more about Scope or the syntax for custom templates, read the [Templates](../../templates) guide.
|
|
[/notice]
|
|
|
|
|