Class Form
Base class for all forms. The form class is an extensible base for all forms on a sapphire application. It can be used either by extending it, and creating processor methods on the subclass, or by creating instances of form whose actions are handled by the parent controller.
In either case, if you want to get a form to do anything, it must be inextricably tied to a controller. The constructor is passed a controller and a method on that controller. This method should return the form object, and it shouldn't require any arguments. Parameters, if necessary, can be passed using the URL or get variables. These restrictions are in place so that we can recreate the form object upon form submission, without the use of a session, which would be too resource-intensive.
You will need to create at least one method for processing the submission
(through FormAction). This method will be passed two parameters: the raw
request data, and the form object. Usually you want to save data into a DataObject by using Form::saveInto(). If you want to process the submitted
data in any way, please use Form::getData() rather than the raw request
data.
Validation
Each form needs some form ofValidator to trigger the FormField->validate() methods for each field. You can't disable validator
for security reasons, because crucial behaviour like extension checks for file
uploads depend on it. The default validator is an instance of RequiredFields. If you want to enforce serverside-validation to be ignored for
a specific FormField, you need to subclass it.
URL Handling
The form class extendsRequestHandler, which means it can be accessed
directly through a URL. This can be handy for refreshing a form by ajax, or even
just displaying a single form field. You can find out the base URL for your form
by looking at the <form action="..."> value. For example, the edit form in
the CMS would be located at "admin/EditForm". This URL will render the form
without its surrounding template when called through GET instead of POST.
By appending to this URL, you can render invidual form elements through the FormField->FieldHolder() method. For example, the "URLSegment" field in a standard CMS form would be accessible through "admin/EditForm/field/URLSegment/FieldHolder".
- Object
-
ViewableData
implements
IteratorAggregate
-
RequestHandler
-
Form
Direct known subclasses
ChangePasswordForm,
CMSActionOptionsForm,
Order_CancelForm,
PageCommentInterface_Form,
RecipientImportField_UploadForm,
SearchForm,
ShowPoll,
Unsubscribe_MailingListForm,
ComplexTableField_Popup,
DataObjectManager_Popup,
DocumentSearchForm,
GroupImportForm,
LoginForm,
MediawebForm,
Member_ProfileForm,
MemberImportForm
Indirect known subclasses
AdvancedSearchForm,
FileDataObjectManager_Popup,
ImageDataObjectManager_Popup,
MediawebPage_Popup,
MemberLoginForm,
MemberTableField_Popup,
PhotoAlbumManager_Popup,
ScaffoldingComplexTableField_Popup
Methods summary
public
|
#
__construct(
Create a new form, with the given fields an action buttons. |
public
|
|
public
|
#
httpSubmission( mixed $request )
Handle a form submission. GET and POST requests behave identically. Populates
the form with |
public
|
#
handleField(
Handle a field request. Uses Form->dataFieldByName() to find a matching field, and falls back to FieldSet->fieldByName() to look for tabs instead. This means that if you have a tab and a formfield with the same name, this method gives priority to the formfield. |
public
|
|
public
|
#
setRedirectToFormOnValidationError( boolean $bool )
Set whether the user should be redirected back down to the form on the page upon validation errors in the form or if they just need to redirect back to the page |
public
boolean
|
#
getRedirectToFormOnValidationError( )
Get whether the user should be redirected back down to the form on the page upon validation errors |
public
|
#
addErrorMessage( mixed $fieldName, mixed $message, mixed $messageType )
Add an error message to a field on this form. It will be saved into the session and used the next time this form is displayed. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
#
HiddenFields( )
Return all <input type="hidden"> fields in a form - including fields nested in CompositeFields. Useful when doing custom field layouts. |
public
|
|
public
|
#
dataFieldByName( mixed $name )
Get a named field from this form's fields. It will traverse into composite fields for you, to find the field you want. It will only return a data field. |
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
string
|
|
public
|
#
setTarget(
Set the target of this form to any value - useful for opening the form contents in a new window or refreshing another frame |
public
|
|
public
|
#
setTemplate( string $template )
Set the SS template that this form should use to render with. The default is "Form". |
public
string
|
#
getTemplate( )
Return the template to render this form with. If the template isn't set, then default to the form class name e.g "Form". |
public
string
|
#
FormEncType( )
Returns the encoding type of the form. This will be either
"multipart/form-data"" if there are any |
public
string
|
#
FormHttpMethod( )
Returns the real HTTP method for the form: GET, POST, PUT, DELETE or HEAD. As
most browsers only support GET and POST in form submissions, all other HTTP
methods are added as a hidden field "_method" that gets evaluated in |
public
string
|
#
FormMethod( )
Returns the form method to be used in the <form> tag. See |
public
|
|
public
string
|
#
FormAction( )
Return the form's action attribute. This is build by adding an executeForm get variable to the parent controller's Link() value |
public
|
|
public
|
|
public
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
string
|
|
protected
|
|
public
|
|
public
|
#
sessionMessage(
Set a message to the session, for display next time this form is shown. |
public static
|
|
public
|
|
public
|
|
public
|
#
getRecord( )
Returns the DataObject that has given this form its data through |
public
string
|
|
public
|
#
validate( )
Processing that occurs before a form is executed. This includes form
validation, if it fails, we redirect back to the form with appropriate error
messages. Triggered through |
public
|
#
loadDataFrom( array|
Load data from the given DataObject or array. It will call
$object->MyField to get the value of MyField. If you passed an array, it will
call $object[MyField]. Doesn't save into dataless FormFields ( |
public
|
#
saveInto(
Save the contents of this form into the given data object. It will make use of setCastedField() to do this. |
public
array
|
|
public
|
#
resetField( string $fieldName, mixed $fieldValue = null )
Resets a specific field to its passed default value. Does NOT clear out all submitted data in the form. |
public
|
#
callfieldmethod( fieldName $data )
Call the given method on the given field. This is used by Ajax-savvy form fields. By putting '&action=callfieldmethod' to the end of the form action, they can access server-side data. |
public
|
|
public
|
#
forAjaxTemplate( )
Return a rendered version of this form, suitable for ajax post-back. It triggers slightly different behaviour, such as disabling the rewriting of # links |
public
|
#
formHtmlContent( )
Returns an HTML rendition of this form, without the <form> tag itself. Attaches 3 extra hidden files, _form_action, _form_name, _form_method, and _form_enctype. These are the attributes of the form. These fields can be used to send the form to Ajax. |
public
|
#
renderWithoutActionButton( mixed $template )
Render this form using the given template, and return the result as a string You can pass either an SSViewer or a template name |
public
|
#
setButtonClicked( funcName $funcName )
Sets the button that was clicked. This should only be called by the Controller. |
public
|
|
public
|
#
defaultAction( )
Return the default button that should be clicked when another one isn't available |
public
|
#
disableDefaultAction( )
Disable the default button. Ordinarily, when a form is processed and no action_XXX button is available, then the first button in the actions list will be pressed. However, if this is "delete", for example, this isn't such a good idea. |
public
|
#
disableSecurityToken( )
Disable the requirement of a SecurityID in the Form. This security protects against CSRF attacks, but you should disable this if you don't want to tie a form to a session - eg a search form. |
public static
|
|
public
boolean
|
#
securityTokenEnabled( )
Returns true if security is enabled - that is if the SecurityID should be included and checked on this form. |
public static
string
|
#
single_field_required( )
Returns the name of a field, if that's the only field that the current controller is interested in. It checks for a call to the callfieldmethod action. This is useful for optimising your forms |
public static
|
#
current_action( )
Return the current form action being called, if available. This is useful for optimising your forms |
public static
|
#
set_current_action( mixed $action )
Set the current form action. Should only be called by Controller. |
public
String
|
|
public
|
|
public
|
|
public
|
|
public
|
|
public
|
Methods inherited from RequestHandler
allowedActions(),
checkAccessAction(),
getRequest(),
handleRequest(),
hasAction(),
httpError()
Methods inherited from ViewableData
ATT_val(),
BaseHref(),
CSSClasses(),
ColumnBreak(),
ColumnCalc(),
ColumnNumber(),
ColumnPad(),
ColumnPos(),
CurrentMember(),
CurrentPage(),
Debug(),
Even(),
EvenOdd(),
First(),
FirstLast(),
HasPerm(),
IsAjax(),
JS_val(),
Last(),
Me(),
Middle(),
MiddleString(),
Modulus(),
MultipleOf(),
Odd(),
Pos(),
RAW_val(),
SQL_val(),
ThemeDir(),
ThemeName(),
Top(),
TotalItems(),
XML_val(),
__get(),
__isset(),
__set(),
buildCastingCache(),
cachedCall(),
castingClass(),
castingHelper(),
castingHelperPair(),
castingObjectCreator(),
castingObjectCreatorPair(),
customise(),
defineMethods(),
escapeTypeForField(),
getField(),
getIterator(),
getSecurityID(),
getXMLValues(),
hasField(),
hasValue(),
i18nLocale(),
iteratorProperties(),
obj(),
renderWith(),
setCustomisedObj(),
setField()
Methods inherited from Object
__call(),
__toString(),
__wakeup(),
addMethodsFrom(),
addStaticVars(),
addWrapperMethod(),
add_extension(),
add_static_var(),
allMethodNames(),
cacheToFile(),
cacheToFileWithArgs(),
clearCache(),
combined_static(),
create(),
createMethod(),
create_from_string(),
exists(),
extInstance(),
extend(),
getCustomClass(),
getExtensionInstance(),
getExtensionInstances(),
get_extensions(),
get_static(),
hasExtension(),
hasMethod(),
has_extension(),
invokeWithExtensions(),
is_a(),
loadCache(),
parentClass(),
parse_class_spec(),
remove_extension(),
sanitiseCachename(),
saveCache(),
set_stat(),
set_static(),
set_uninherited(),
stat(),
strong_create(),
uninherited(),
uninherited_static(),
useCustomClass()
Magic methods summary
Properties summary
public
boolean
|
$IncludeFormTag |
#
$includeFormTag Accessed by Form.ss; modified by |
protected
mixed
|
$fields | |
protected
mixed
|
$actions | |
protected
mixed
|
$controller | |
protected
mixed
|
$name | |
protected
mixed
|
$validator | |
protected
string
|
$formMethod | |
protected static
mixed
|
$current_action | |
protected
Dataobject
|
$record |
#
$record Populated by |
protected
boolean
|
$hasDefaultAction |
#
Keeps track of whether this form has a default action or not. Set to false by $this->disableDefaultAction(); |
protected
string
|
$target |
#
Target attribute of form-tag. Useful to open a new window upon form submission. |
protected
string
|
$legend |
#
Legend value, to be inserted into the <legend> element before the <fieldset> in Form.ss template. |
protected
string
|
$template |
#
The SS template to render this form HTML into. Default is "Form", but this can be changed to another template for customisation. |
protected
mixed
|
$buttonClickedFunc | |
protected
mixed
|
$message | |
protected
mixed
|
$messageType | |
protected
boolean
|
$redirectToFormOnValidationError |
#
Should we redirect the user back down to the the form on validation errors rather then just the page |
protected
boolean
|
$security | |
public
boolean
|
$jsValidationIncluded |
#
HACK This is a temporary hack to allow multiple calls to includeJavascriptValidation on the validator (if one is present). |
protected
array
|
$extraClasses |
#
array Extra CSS-classes for the formfield-container |
public static
array
|
$url_handlers |
#
The default URL handling rules. This specifies that the next component of the URL corresponds to a method to be called on this RequestHandlingData object. |
Properties inherited from RequestHandler
$allowed_actions,
$brokenOnConstruct,
$request
Properties inherited from ViewableData
$casting,
$customisedObject,
$default_cast,
$failover,
$iteratorPos,
$iteratorTotalItems