silverorange Code


Swat.SwatForm
/Swat/SwatForm.php at line 25

Class SwatForm

SwatObject
└─SwatUIObject
└─SwatWidget
└─SwatContainer
└─SwatDisplayableContainer
└─SwatForm

public class SwatForm
extends SwatDisplayableContainer

A form widget which can contain other widgets

SwatForms are very useful for processing widgets. For most widgets, if they are not inside a SwatForm they will not be able to be processed properly.

With Swat's default style, SwatForm widgets have no visible margins, padding or borders.

Copyright:
2004-2007 silverorange
License:
http://www.gnu.org/copyleft/lesser.html LGPL License 2.1

Field Summary
final mixed

AUTHENTICATION_TOKEN_FIELD

final mixed

HIDDEN_FIELD

final mixed

METHOD_GET

final mixed

METHOD_POST

final mixed

PROCESS_FIELD

final mixed

SERIALIZED_PREFIX

string

$action

The action attribute of the HTML form tag.

boolean

$autofocus

Whether or not to automatically focus the a default SwatControl when this form loads.

SwatButton

$button

A reference to the button that was clicked to submit the form, or null if the button is not set.

SwatControl

$default_focused_control

A reference to the default control to focus when the form loads.

static string

$default_salt

The default value to use for signature salt.

string

$encoding_type

Encoding type of the form.

protected array

$hidden_fields

Hidden form fields.

protected string

$salt

The value to use when salting serialized data signatures.

Constructor Summary

SwatForm(string id)

Creates a new form.

Method Summary
void

addHiddenField(string name, mixed value)

Adds a hidden form field.

void

addWithField(SwatWidget widget, string title)

Adds a widget within a new SwatFormField.

static void

clearAuthenticationToken()

Clears the token value used to prevent cross-site request forgeries.

void

clearHiddenFields()

Clears all hidden fields.

void

display()

Displays this form.

protected void

displayHiddenFields()

Displays hidden form fields.

protected array

getCSSClassNames()

Gets the array of CSS classes that are applied to this form.

array

getFormData()

Returns the super-global array with this form's data.

protected SwatHtmlTag

getFormTag()

Gets the XHTML form tag used to display this form.

mixed

getHiddenField(string name)

Gets the value of a hidden form field.

protected string

getInlineJavaScript()

Gets inline JavaScript required for this form.

string

getMethod()

Gets the HTTP method this form uses to send data.

string

getSalt()

Gets the salt value to use when salting signature data.

boolean

isAuthenticated()

Whether or not this form is authenticated.

boolean

isSubmitted()

Whether or not this form was submitted on the previous page request.

protected void

notifyOfAdd(SwatWidget widget)

Notifies this widget that a widget was added.

true

process()

Processes this form.

protected void

processHiddenFields()

Checks submitted form data for hidden fields.

protected string

serializeHiddenField(mixed value)

Serializes a hidden field value into a string safe for including in form data.

static void

setAuthenticationToken(string token)

Sets the token value used to prevent cross-site request forgeries.

void

setMethod(string method)

Sets the HTTP method this form uses to send data.

void

setSalt(string salt)

Sets the salt value to use when salting signature data.

protected mixed

unserializeHiddenField(string value)

Unserializes a hidden field value that was serialized using SwatForm::serializeHiddenField().

Methods inherited from Swat.SwatDisplayableContainer
display, getCSSClassNames
Methods inherited from Swat.SwatContainer
add, addChild, copy, display, displayChildren, getChild, getChildren, getDescendantStates, getDescendants, getFirst, getFirstDescendant, getFocusableHtmlId, getHtmlHeadEntrySet, getMessages, hasMessage, init, notifyOfAdd, packEnd, packStart, printWidgetTree, process, remove, replace, sendAddNotifySignal, setDescendantStates
Methods inherited from Swat.SwatWidget
addCompositeWidget, addMessage, confirmCompositeWidgets, copy, createCompositeWidgets, display, displayHtmlHeadEntries, getCSSClassNames, getCompositeWidget, getCompositeWidgets, getFocusableHtmlId, getHtmlHeadEntrySet, getMessages, hasMessage, init, isDisplayed, isInitialized, isProcessed, isSensitive, printWidgetTree, process, replaceWithContainer
Methods inherited from Swat.SwatUIObject
addComment, addJavaScript, addStyleSheet, addTangoAttribution, copy, getCSSClassNames, getCSSClassString, getFirstAncestor, getHtmlHeadEntrySet, getInlineJavaScript, getUniqueId, isVisible

Field Detail

/Swat/SwatForm.php at line 34

AUTHENTICATION_TOKEN_FIELD

public final mixed AUTHENTICATION_TOKEN_FIELD = '_swat_form_authentication_token'

/Swat/SwatForm.php at line 33

HIDDEN_FIELD

public final mixed HIDDEN_FIELD = '_swat_form_hidden_fields'

/Swat/SwatForm.php at line 30

METHOD_GET

public final mixed METHOD_GET = 'get'

/Swat/SwatForm.php at line 29

METHOD_POST

public final mixed METHOD_POST = 'post'

/Swat/SwatForm.php at line 32

PROCESS_FIELD

public final mixed PROCESS_FIELD = '_swat_form_process'

/Swat/SwatForm.php at line 35

SERIALIZED_PREFIX

public final mixed SERIALIZED_PREFIX = '_swat_form_serialized_'

/Swat/SwatForm.php at line 45

action

public string $action = '#'

The action attribute of the HTML form tag


/Swat/SwatForm.php at line 66

autofocus

public boolean $autofocus = false

Whether or not to automatically focus the a default SwatControl when this form loads

Autofocusing is good for applications or pages that are keyboard driven -- such as data entry forms -- as it immediatly places the focus on the form.


/Swat/SwatForm.php at line 87

button

public SwatButton $button = null

A reference to the button that was clicked to submit the form, or null if the button is not set.

You usually do not want to explicitly set this in your code because other parts of Swat set this proprety automatically.


/Swat/SwatForm.php at line 76

default_focused_control

public SwatControl $default_focused_control = null

A reference to the default control to focus when the form loads

If this is not set then it defaults to the first SwatControl in the form.


/Swat/SwatForm.php at line 98

default_salt

public static string $default_salt = null

The default value to use for signature salt

If this value is not null, all newly instantiated forms will call the SwatForm::setSalt() method with this value as the $salt parameter.


/Swat/SwatForm.php at line 54

encoding_type

public string $encoding_type = null

Encoding type of the form

Used for multipart forms for file uploads.


/Swat/SwatForm.php at line 115

hidden_fields

protected array $hidden_fields = array()

Hidden form fields

An array of the form: name => value where all the values are passed as hidden fields in this form.

See Also:
SwatForm::addHiddenField()
SwatForm::getHiddenField()

/Swat/SwatForm.php at line 122

salt

protected string $salt = null

The value to use when salting serialized data signatures


Constructor Detail

/Swat/SwatForm.php at line 160

SwatForm

public SwatForm(string id)

Creates a new form

Parameters:
id - a non-visible unique id for this widget.
See Also:
SwatWidget::__construct()

Method Detail

/Swat/SwatForm.php at line 290

addHiddenField

public void addHiddenField(string name, mixed value)

Adds a hidden form field

Adds a form field to this form that is not shown to the user. Hidden form fields are outputted as type="hidden" input tags. Values are serialized before being output so the value can be either a primitive type or an object. Unserialization happens automatically when SwatForm::getHiddenField() is used to retrieve the value. For non-array and non-object types, the value is also stored as an unserialized value that can be retrieved without using SwatForm::getHiddenField().

Parameters:
name - the name of the field.
value - the value of the field, either a string or an array.
See Also:
SwatForm::getHiddenField()
Throws:
if an attempt is made to add a value of type 'resource'.

/Swat/SwatForm.php at line 366

addWithField

public void addWithField(SwatWidget widget, string title)

Adds a widget within a new SwatFormField

This is a convenience method that does the following: - creates a new SwatFormField, - adds the widget as a child of the form field, - and then adds the SwatFormField to this form.

Parameters:
widget - a reference to a widget to add.
title - the visible title of the form field.

/Swat/SwatForm.php at line 531

clearAuthenticationToken

public static void clearAuthenticationToken()

Clears the token value used to prevent cross-site request forgeries

After this method is called, no cross-site request forgery detection can be performed, and all forms will be considered authenticated. This is acceptable if a user's session is ending and the threat of cross-site request forgeries is gone.


/Swat/SwatForm.php at line 347

clearHiddenFields

public void clearHiddenFields()

Clears all hidden fields


/Swat/SwatForm.php at line 220

display

public void display()

Displays this form

Outputs the HTML form tag and calls the display() method on each child widget of this form. Then, after all the child widgets are displayed, displays all hidden fields.

This method also adds a hidden field called 'process' that is given the unique identifier of this form as a value.


/Swat/SwatForm.php at line 617

displayHiddenFields

protected void displayHiddenFields()

Displays hidden form fields

Displays hiden form fields as XHTML elements. This method automatically handles array type values so they will be returned correctly as arrays.

This methods also generates an array of hidden field names and passes them as hidden fields.

If an authentication token is set on this form to prevent cross-site request forgeries, the token is displayed in a hidden field.


/Swat/SwatForm.php at line 694

getCSSClassNames

protected array getCSSClassNames()

Gets the array of CSS classes that are applied to this form

Returns:
the array of CSS classes that are applied to this form.
See Also:
SwatUIObject::getCSSClassString()

/Swat/SwatForm.php at line 387

getFormData

public array getFormData()

Returns the super-global array with this form's data

Returns a reference to the super-global array containing this form's data. The array is chosen based on this form's method.

Returns:
a reference to the super-global array containing this form's data.

/Swat/SwatForm.php at line 673

getFormTag

protected SwatHtmlTag getFormTag()

Gets the XHTML form tag used to display this form

Returns:
the XHTML form tag used to display this form.

/Swat/SwatForm.php at line 318

getHiddenField

public mixed getHiddenField(string name)

Gets the value of a hidden form field

Parameters:
name - the name of the field whose value to get.
Returns:
the value of the field. The type of the field is preserved from the call to {@link SwatForm::addHiddenField()}. If the field does not exist, null is returned.
Throws:
if the serialized form data does not match the signature data.
See Also:
SwatForm::addHiddenField()

/Swat/SwatForm.php at line 711

getInlineJavaScript

protected string getInlineJavaScript()

Gets inline JavaScript required for this form

Right now, this JavaScript focuses the first SwatControl in the form.

Returns:
inline JavaScript required for this form.

/Swat/SwatForm.php at line 202

getMethod

public string getMethod()

Gets the HTTP method this form uses to send data

Returns:
a method constant.

/Swat/SwatForm.php at line 493

getSalt

public string getSalt()

Gets the salt value to use when salting signature data

SwatInputControl widgets may want ot use this value for salting their own data. This can be done using:

<?php
$salt 
$this->getForm()->getSalt();
?>

Returns:
the value to use when salting signature data.

/Swat/SwatForm.php at line 441

isAuthenticated

public boolean isAuthenticated()

Whether or not this form is authenticated

This can be used to catch cross-site request forgeries if the SwatForm::setAuthenticationToken() method was previously called.

If form authentication is used, processing should only be performed on authenticated forms. An unauthenticated form may be a malicious request.

Returns:
true if this form is authenticated or if this form does not use authentication. False if this form is not authenticated.

/Swat/SwatForm.php at line 416

isSubmitted

public boolean isSubmitted()

Whether or not this form was submitted on the previous page request

This method may becalled before or after the SwatForm::process() method. and is thus sometimes more useful than SwatForm::isProcessed() which only returns a meaningful value after SwatForm::process() is called.

Returns:
true if this form was submitted on the previous page request and false if it was not.

/Swat/SwatForm.php at line 583

notifyOfAdd

protected void notifyOfAdd(SwatWidget widget)

Notifies this widget that a widget was added

If any of the widgets in the added subtree are file entry widgets then set this form's encoding accordingly.

Parameters:
widget - the widget that has been added.
See Also:
SwatContainer::notifyOfAdd()

/Swat/SwatForm.php at line 254

process

public true process()

Processes this form

If this form has been submitted then calls the process() method on each child widget. Then processes hidden form fields.

This form is only marked as processed if it was submitted by the user.

Returns:
if this form was actually submitted, false otherwise.
See Also:
SwatContainer::process()

/Swat/SwatForm.php at line 549

processHiddenFields

protected void processHiddenFields()

Checks submitted form data for hidden fields

Checks submitted form data for hidden fields. If hidden fields are found, properly re-adds them to this form.

Throws:
if the serialized form data does not match the signature data.

/Swat/SwatForm.php at line 754

serializeHiddenField

protected string serializeHiddenField(mixed value)

Serializes a hidden field value into a string safe for including in form data

Parameters:
value - the hidden field value to serialize.
Returns:
the hidden field value serialized for safely including in form data.

/Swat/SwatForm.php at line 515

setAuthenticationToken

public static void setAuthenticationToken(string token)

Sets the token value used to prevent cross-site request forgeries

After the authentication token is set, when any form is processed, the the submitted form data must contain this token.

For the safest results, this token should be taken from an active session. For usability reasons, the same token should be used for the same user over multiple requests. The token should be unique to a user's session and should be difficult to guess.

Parameters:
token - the value used to prevent cross-site request forgeries.

/Swat/SwatForm.php at line 184

setMethod

public void setMethod(string method)

Sets the HTTP method this form uses to send data

Parameters:
method - a method constant. Must be one of SwatForm::METHOD_* otherwise an error is thrown.
Throws:
SwatException

/Swat/SwatForm.php at line 473

setSalt

public void setSalt(string salt)

Sets the salt value to use when salting signature data

Parameters:
salt - the value to use when salting signature data.

/Swat/SwatForm.php at line 783

unserializeHiddenField

protected mixed unserializeHiddenField(string value)

Unserializes a hidden field value that was serialized using SwatForm::serializeHiddenField()

Parameters:
value - the hidden field value to unserialize.
Returns:
the unserialized value.
Throws:
if the serialized form data does not match the signature data.

silverorange Code