MINOR Documentation for FormField->setTemplate() and FormField->setAttribute()

This commit is contained in:
Ingo Schommer 2011-12-22 14:56:38 +01:00
parent b4d7e51da3
commit 1eaf45a118

View File

@ -155,9 +155,7 @@ Readonly on a FormField
$myReadonlyField = $myField->performReadonlyTransformation(); $myReadonlyField = $myField->performReadonlyTransformation();
## Using a custom template ## Custom form templates
*Required Silverstripe 2.3 for some displayed functionality*
You can use a custom form template to render with, instead of *Form.ss* You can use a custom form template to render with, instead of *Form.ss*
@ -239,6 +237,27 @@ To find more methods, have a look at the `[api:Form]` class, as there is a lot o
templates, for example, you could use `<% control Fields %>` instead of specifying each field manually, as we've done templates, for example, you could use `<% control Fields %>` instead of specifying each field manually, as we've done
above. above.
### Custom form field templates
The easiest way to customize form fields is adding CSS classes and additional attributes.
:::php
$field = new TextField('MyText');
$field->addExtraClass('largeText');
$field->setAttribute('data-validation-regex', '[\d]*');
// Field() renders as:
// <input type="text" class="largeText" id="Form_Form_TextField" name="TextField" data-validation-regex="[\d]*">
Each form field is rendered into a form via the `[FieldHolder()](api:FormField->FieldHolder())` method,
which includes a container `<div>` as well as a `<label>` element (if applicable).
You can also render each field without these structural elements through the `[Field()](api:FormField->Field())` method.
In order to influence the form rendering, overloading these two methods is a good start.
In addition, most form fields are rendered through SilverStripe templates, e.g. `TextareaField` is rendered via `sapphire/templates/forms/TextareaField.ss`.
These templates can be overwritten globally by placing a template with the same name in your `mysite` directory,
or set on a form field instance via `[setTemplate()](api:FormField->setTemplate())` and `[setFieldHolderTemplate()](api:FormField->setFieldHolderTemplate())`.
### Securing forms against Cross-Site Request Forgery (CSRF) ### Securing forms against Cross-Site Request Forgery (CSRF)
SilverStripe tries to protect users against *Cross-Site Request Forgery (CSRF)* by adding a hidden *SecurityID* SilverStripe tries to protect users against *Cross-Site Request Forgery (CSRF)* by adding a hidden *SecurityID*