Version: current

# Class FormResponse

This class collects all output that needs to be returned after an Form-Request to the client. It automatically determines if it needs to send back javascript after an Ajax-Request or just redirect to another page (on a normal request).

FormResponse is also responsible for keeping the client- and serverside in sync after an HTTP-Request by collecting javascript-commands (which mostly trigger subsequent update-calls by Ajax.) Use the output as a return-value for Ajax-based saving methods. Be sure to check if the call is acutally "ajaxy" by checking Director::is_ajax(). It is the developers responsibility to include this into his custom form-methods. Use the Request-Parameter 'htmlonly' to enforce a pure HTML-response from the client-side.

Example: A {@TableField} is in an incorrect state after being saved, as it still has rows marked as "new" which are already saved (and have an ID) in the database. By using AjaxSynchroniser we make sure that every instance is refreshed by Ajax and reflects the correct state.

Caution:

- FormResponse assumes that prototype.js is included on the client-side. (We can't put it into Requirements because it has to be included BEFORE an AjaxSynchroniser is called).
- Please DON'T escape literal parameters which are passed to FormResponse, they are escaped automatically.
- Some functions assume a {LeftAndMain}-based environment (e.g. load_form())

Package: forms\core
Located at sapphire/core/control/FormResponse.php

## Methods summary

`public static string` | # `respond( )` Get all content as a javascript-compatible string (only if there is an Ajax-Request present). Falls back to {non_ajax_content}, {redirect_url} or Director::redirectBack() (in this order). Get all content as a javascript-compatible string (only if there is an Ajax-Request present). Falls back to {non_ajax_content}, {redirect_url} or Director::redirectBack() (in this order). #### Returns `string` string
`public static` | # `load_form( mixed $content, mixed $id = 'Form_EditForm' )` Caution: Works only for forms which inherit methods from LeftAndMain.js Caution: Works only for forms which inherit methods from LeftAndMain.js
`public static` | # `add( mixed $scriptContent, mixed $uniquenessID = null )` Add custom scripts. Caution: Not escaped for backwards-compatibility. Add custom scripts. Caution: Not escaped for backwards-compatibility. #### Parameters $scriptContent $scriptContent string $uniquenessID
`public static` | # `clear( )`
`public static` | # `get_page( mixed $id, mixed $form = 'Form_EditForm', mixed $uniquenessID = null )` #### Parameters $id $id int $form $uniquenessID
`public static` | # `status_message( mixed $message = "", mixed $status = null )` Sets the status-message (overlay-notification in the CMS). You can call this method multiple times, it will default to the "worst" statusmessage. Sets the status-message (overlay-notification in the CMS). You can call this method multiple times, it will default to the "worst" statusmessage. #### Parameters $message $message string $status $status string
`public static` | # `error( mixed $message = "" )` Alias for status_message($messsage, 'bad') Alias for status_message($messsage, 'bad') #### Parameters $message $message string
`public static` | # `update_status( mixed $status, mixed $form = "Form_EditForm" )` Update the status (upper right corner) of the given Form Update the status (upper right corner) of the given Form #### Parameters $status $status string $form $form string
`public static` | # `set_node_title( mixed $id, mixed $title = "" )` Set the title of a single page in the pagetree Set the title of a single page in the pagetree #### Parameters $id $id int $title $title string
`public static` | # `set_non_ajax_content( mixed $content )` Fallback-method to supply normal HTML-response when not being called by ajax. Fallback-method to supply normal HTML-response when not being called by ajax. #### Parameters $content $content string HTML-content
`public static` | # `set_redirect_url( mixed $url )` #### Parameters $url $url string
`public static string` | # `get_redirect_url( )` #### Returns `string` string
`public static` | # `update_dom_id( mixed $domID, mixed $domContent, mixed $reapplyBehaviour = true, mixed $replaceMethod = 'replace', mixed $uniquenessID = null )` Replace a given DOM-element with the given content. It automatically prefills {$non_ajax_content} with the passed content (as a fallback). Replace a given DOM-element with the given content. It automatically prefills {$non_ajax_content} with the passed content (as a fallback). #### Parameters $domID $domID string The DOM-ID of an HTML-element that should be replaced $domContent $domContent string The new HTML-content $reapplyBehaviour $reapplyBehaviour boolean Applies behaviour to the given domID after refreshing it $replaceMethod $replaceMethod string Method for replacing - either 'replace' (=outerHTML) or 'update' (=innerHTML) (Caution: "outerHTML" might cause problems on the client-side, e.g. on table-tags) $uniquenessID
`protected static string` | # `get_javascript( )` #### Returns `string` Compiled string of javascript-function-calls (needs to be evaluated on the client-side!)

### Magic methods summary

## Properties summary

`protected static array` | $rules | # array array
`protected static array` | $behaviour_apply_rules | # array Separated from $rules because we need to apply all behaviour at the very end of the evaluated script to make sure we include all possible Behaviour.register()-calls. array Separated from $rules because we need to apply all behaviour at the very end of the evaluated script to make sure we include all possible Behaviour.register()-calls.
`protected static mixed` | $non_ajax_content | # string string
`protected static array` | $status_messages | # Status-messages are accumulated, and the "worst" is chosen Status-messages are accumulated, and the "worst" is chosen
`protected static mixed` | $redirect_url | # string string
`protected static array` | $status_include_order | # string string

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -