apidoc
[ class tree: apidoc ] [ index: apidoc ] [ all elements ]

Class: DB_DataObject_FormBuilder

Source Location: /DB_DataObject_FormBuilder-0.9.0/FormBuilder.php

Class Overview




Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 141]


[ Top ]


Class Variables

$add_form_header =  true

[line 150]

Add a header to the form - if set to true, the form will have a header element as the first element in the form.
  • See: form_header_text
  • Access: public

Type:   mixed


[ Top ]

$form_header_text =  null

[line 159]

Text for the form header. If not set, the name of the database table this form represents will be used.
  • See: add_form_header
  • Access: public

Type:   mixed


[ Top ]

$rule_violation_message =  'The value you have entered is not valid.'

[line 168]

Text that is displayed as an error message if a validation rule is violated by the user's input.
  • See: rule_violation_message
  • Access: public

Type:   mixed


[ Top ]

$validateOnProcess =  false

[line 189]

If set to TRUE, the current DataObject's validate method is being called before the form data is processed. If errors occur, no insert/update operation will be made on the database. Use getValidationErrors() to retrieve the reasons for a failure.

Defaults to FALSE.

  • Access: public

Type:   mixed


[ Top ]

$_appendForm =  false

[line 214]

If false, FormBuilder will use the form object from $_form as a basis for the new form: It will just add elements to the existing form object, not generate a new one.

If true, FormBuilder will generate a new form object, create all elements as needed for the given DataObject, then strip the elements from the exiting form object in $_form and add it to the newly generated form object.

  • Access: protected

Type:   mixed


[ Top ]

$_dateFieldLanguage =  'en'

[line 223]

The language used in date fields. See documentation of HTML_Quickform's date element for more information.
  • See: HTML_QuickForm_date
  • Access: protected

Type:   mixed


[ Top ]

$_dateFromDatabaseCallback = array('DB_DataObject_FormBuilder','_date2array')

[line 232]

Callback method to convert a date from the format it is stored in the database to the format used by the QuickForm element that handles date values. Must have a format usable with call_user_func().
  • Access: protected

Type:   mixed


[ Top ]

$_dateToDatabaseCallback = array('DB_DataObject_FormBuilder','_array2date')

[line 241]

Callback method to convert a date from the format used by the QuickForm element that handles date values to the format the database can store it in.

Must have a format usable with call_user_func().

  • Access: protected

Type:   mixed


[ Top ]

$_elementTypeMap = array('shorttext' => 'text',
                                 'longtext'  => 'textarea',
                                 'date'      => 'date',
                                 'integer'   => 'text',
                                 'float'     => 'text')

[line 259]

Array to determine what QuickForm element types are being used for which

general field types. If you configure FormBuilder using arrays, the format is: array('nameOfFieldType' => 'QuickForm_Element_name', ...); If configured via .ini file, the format looks like this: elementTypeMap = shorttext:text,date:date,...

Allowed field types:

  • shorttext
  • longtext</
  • date
  • integer
  • float

  • Access: protected

Type:   mixed


[ Top ]

$_form =  false

[line 178]

If you want to use the generator on an existing form object, pass it to the factory method within the options array, element name: 'form' (who would have guessed?)
  • See: DB_DataObject_Formbuilder()
  • Access: protected

Type:   mixed


[ Top ]

$_queryType =  DB_DATAOBJECT_FORMBUILDER_QUERY_AUTODETECT

[line 203]

Used to determine which action to perform with the submitted data in processForm()
  • Access: protected

Type:   mixed


[ Top ]

$_validationErrors =  false

[line 196]

Contains the last validation errors, if validation checking is enabled.
  • Access: protected

Type:   mixed


[ Top ]



Method Detail

DB_DataObject_FormBuilder (Constructor)   [line 309]

DB_DataObject_FormBuilder DB_DataObject_FormBuilder( &$do, [array $options = false], object $do)

DB_DataObject_FormBuilder::DB_DataObject_FormBuilder()

The class constructor.

  • Access: public

Parameters:

object   $do   —  The DB_DataObject-derived object for which a form shall be built
array   $options   —  An optional associative array of options.
   &$do   — 

[ Top ]

create   [line 287]

object DB_DataObject_FormBuilder &create( &$do, [array $options = false], object $do)

DB_DataObject_FormBuilder::create()

Factory method. Although not really needed at the moment, it is the recommended method to make a new object instance. Benefits: Checks the passed parameters and returns a PEAR_Error object in case something is wrong. Also, it will make your code forward-compatible to future versions of this class, which might include other types or forms, resulting in this being a stripped-down base class that returns a specialized class for the desired purpose (i.e. for generating GTK form elements for use with PHP-GTK, WML forms for WAP...).

Options can be:

  • 'rule_violation_message' : See description of similarly-named class property
  • 'add_form_header' : See description of similarly-named class property
  • 'form_header_text' : See description of similarly-named class property

  • Return: or PEAR_Error object
  • Access: public

Parameters:

object   $do   —  The DB_DataObject-derived object for which a form shall be built
array   $options   —  An optional associative array of options.
   &$do   — 

[ Top ]

debug   [line 1231]

void debug( string $message)

DB_DataObject_FormBuilder::debug()

Outputs a debug message, if the debug setting in the DataObject.ini file is set to 1 or higher.

  • See: DB_DataObject::debugLevel()
  • Access: public

Parameters:

string   $message   —  The message to printed to the browser

[ Top ]

forceQueryType   [line 1205]

boolean forceQueryType( [integer $queryType = DB_DATAOBJECT_FORMBUILDER_QUERY_AUTODETECT])

DB_DataObject_FormBuilder::forceQueryType()

You can force the behaviour of the processForm() method by passing one of the following constants to this method:

  • DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEINSERT: The submitted data will always be INSERTed into the database
  • DB_DATAOBJECT_FORMBUILDER_QUERY_FORCEUPDATE: The submitted data will always be used to perform an UPDATE on the database
  • DB_DATAOBJECT_FORMBUILDER_QUERY_FORCENOACTION: The submitted data will overwrite the properties of the DataObject, but no action will be performed on the database.
  • DB_DATAOBJECT_FORMBUILDER_QUERY_AUTODETECT: The processForm() method will try to detect for itself if an INSERT or UPDATE query has to be performed. This will not work if no primary key field can be detected for the current DataObject. In this case, no action will be performed. This is the default behaviour.

  • Access: public

Parameters:

integer   $queryType   —  The type of the query to be performed. Please use the preset constants for setting this.

[ Top ]

getDataObjectSelectDisplayValue   [line 734]

string getDataObjectSelectDisplayValue( DB_DataObject &$do, [mixed $displayfield = false], [ $level = 1])

DB_DataObject_FormBuilder::getDataObjectSelectDisplayValue()

Returns a string which identitfies this dataobject. If multiple display fields are given, will display them all seperated by ", ". If a display field is a foreign key (link) the display value for the record it points to will be used. (Its display value will be surrounded by parenthesis as it may have multiple display fields of its own.)

Will use display field configurations from these locations, in this order:
1) $displayfield parameter
2) databaseName.formBuilder.ini file, section [tableName__display_fields]
3) the select_display_field member variable of the dataobject
4) global 'select_display_field' setting for DB_DataObject_FormBuilder

  • Return: select display value for this field
  • Access: public

Parameters:

DB_DataObject   &$do   —  the dataobject to get the display value for, must be populated
mixed   $displayfield   —  field to use to display, may be an array with field names or a single field
   $level   — 

[ Top ]

getFieldLabel   [line 705]

string getFieldLabel( $fieldName $fieldName)

DB_DataObject_FormBuilder::getFieldLabel()

Returns the label for the given field name. If no label is specified, the fieldname will be returned with ucfirst() applied.

  • Access: public

Parameters:

$fieldName   $fieldName   —  string The field name

[ Top ]

getForm   [line 913]

object &getForm( [string $action = false], [string $target = '_self'], [string $formName = false], [string $method = 'post'])

DB_DataObject_FormBuilder::getForm()

Returns a HTML form that was automagically created by _generateForm(). You need to use the get() method before calling this one in order to prefill the form with the retrieved data.

If you have a method named "preGenerateForm()" in your DataObject-derived class, it will be called before _generateForm(). This way, you can create your own elements there and add them to the "preDefElements" property, so they will not be auto-generated.

If you have your own "getForm()" method in your class, it will be called instead of _generateForm(). This enables you to have some classes that make their own forms completely from scratch, without any auto-generation. Use this for highly complex forms. Your getForm() method needs to return the complete HTML_QuickForm object by reference.

If you have a method named "postGenerateForm()" in your DataObject-derived class, it will be called after _generateForm(). This allows you to remove some elements that have been auto-generated from table fields but that you don't want in the form.

Many ways lead to rome.

  • Access: public

Parameters:

string   $action   —  The form action. Optional. If set to false (default), $_SERVER['PHP_SELF'] is used.
string   $target   —  The window target of the form. Optional. Defaults to '_self'.
string   $formName   —  The name of the form, will be used in "id" and "name" attributes. If set to false (default), the class name is used, prefixed with "frm"
string   $method   —  The submit method. Defaults to 'post'.

[ Top ]

getSelectOptions   [line 798]

array getSelectOptions( string $field, [ $displayfield = false], string $displayField)

DB_DataObject_FormBuilder::getSelectOptions()

Returns an array of options for use with the HTML_QuickForm "select" element. It will try to fetch all related objects (if any) for the given field name and build the array. For the display name of the option, it will try to use the settings in the database.formBuilder.ini file. If those are not found, the linked object's property "select_display_field". If that one is not present, it will try to use the global configuration setting "select_display_field". Can also be called with a second parameter containing the name of the display field - this will override all other settings. Same goes for "select_order_field", which determines the field name used for sorting the option elements. If neither a config setting nor a class property of that name is set, the display field name will be used.

  • Access: public

Parameters:

string   $field   —  The field to fetch the links from. You should make sure the field actually *has* links before calling this function (see: DB_DataObject::links())
string   $displayField   —  (Optional) The name of the field used for the display text of the options
   $displayfield   — 

[ Top ]

getValidationErrors   [line 1038]

mixed getValidationErrors( )

DB_DataObject_FormBuilder::getValidationErrors()

Returns errors from data validation. If errors have occured, this will be an array with the fields that have errors, otherwise a boolean.

  • See: DB_DataObject::validate()
  • Access: public

[ Top ]

processForm   [line 1084]

boolean processForm( array $values, string $queryType)

DB_DataObject_FormBuilder::processForm()

This will take the submitted form data and put it back into the object's properties. If the primary key is not set or NULL, it will be assumed that you wish to insert a new element into the database, so DataObject's insert() method is invoked. Otherwise, an update() will be performed. <b>Careful:</b> If you're using natural keys or cross-referencing tables where you don't have one dedicated primary key, this will always assume that you want to do an update! As there won't be a matching entry in the table, no action will be performed at all - the reason for this behaviour can be very hard to detect. Thus, if you have such a situation in one of your tables, simply override this method so that instead of the key check it will try to do a SELECT on the table using the current settings. If a match is found, do an update. If not, do an insert. This method is perfect for use with QuickForm's process method. Example:

  1.  if ($form->validate()) {
  2.      $form->freeze();
  3.      $form->process(array(&$formGenerator,'processForm')false);
  4.  }

If you wish to enforce a special type of query, use the forceQueryType() method.

Always remember to pass your objects by reference - otherwise, if the operation was an insert, the primary key won't get updated with the new database ID because processForm() was using a local copy of the object!

If a method named "preProcess()" exists in your derived class, it will be called before processForm() starts doing its magic. The data that has been submitted by the form will be passed to that method as a parameter. Same goes for a method named "postProcess()", with the only difference - you might have guessed this by now - that it's called after the insert/update operations have been done. Use this for filtering data, notifying users of changes etc.pp. ...

  • Return: TRUE if database operations were performed, FALSE if not
  • Access: public

Parameters:

array   $values   —  The values of the submitted form
string   $queryType   —  If the standard query behaviour ain't good enough for you, you can force a certain type of query

[ Top ]

useForm   [line 682]

boolean useForm( $form &$form, [$append $append = false])

DB_DataObject_FormBuilder::useForm()

Sometimes, it might come in handy not just to create a new QuickForm object, but to work with an existing one. Using FormBuilder together with HTML_QuickForm_Controller or HTML_QuickForm_Page is such an example ;-) If you do not call this method before the form is generated, a new QuickForm object will be created (default behaviour).

  • Return: Returns false if the passed object was not a HTML_QuickForm object or a QuickForm object was already created
  • Access: public

Parameters:

$form   &$form   —  object A HTML_QuickForm object (or extended from that)
$append   $append   —  boolean If TRUE, the form will be appended to the one generated by FormBuilder. If false, FormBuilder will just add its own elements to this form.

[ Top ]

validateData   [line 1022]

mixed validateData( )

DB_DataObject_FormBuilder::validateData()

Makes a call to the current DataObject's validate() method and returns the result.

  • See: DB_DataObject::validate()
  • Access: public

[ Top ]

_array2date   [line 998]

mixed _array2date( $dateInput, [boolean $timestamp = false], array $date)

DB_DataObject_FormBuilder::_array2date()

Takes a date array as used by the QuickForm date element and turns it back into a string representation suitable for use with a database date field (format 'YYYY-MM-DD'). If second parameter is true, it will return a unix timestamp instead.

Beware: For the date conversion to work, you must at least use the letters "d", "m" and "Y" in your format string (see "date_element_format" option). If you want to enter a time as well, you will have to use "H", "i" and "s" as well. Other letters will not work! Exception: You can also use "M" instead of "m" if you want plain text month names.

  • Access: protected

Parameters:

array   $date   —  An array representation of a date, as user in HTML_QuickForm's date element
boolean   $timestamp   —  Optional. If true, return a timestamp instead of a string. Defaults to false.
   $dateInput   — 

[ Top ]

_date2array   [line 947]

array _date2array( mixed $date)

DB_DataObject_FormBuilder::_date2array()

Takes a string representing a date or a unix timestamp and turns it into an array suitable for use with the QuickForm data element. When using a string, make sure the format can be handled by the PEAR::Date constructor!

Beware: For the date conversion to work, you must at least use the letters "d", "m" and "Y" in your format string (see "date_element_format" option). If you want to enter a time as well, you will have to use "H", "i" and "s" as well. Other letters will not work! Exception: You can also use "M" instead of "m" if you want plain text month names.

  • Access: protected

Parameters:

mixed   $date   —  A unix timestamp or the string representation of a date, compatible to strtotime()

[ Top ]

_generateForm   [line 404]

object &_generateForm( [string $action = false], [string $target = '_self'], [string $formName = false], [string $method = 'post'])

DB_DataObject_FormBuilder::_generateForm()

Builds a simple HTML form for the current DataObject. Internal function, called by the public getForm() method. You can override this in child classes if needed, but it's also possible to leave this as it is and just override the getForm() method to simply fine-tune the auto-generated form object (i.e. add/remove elements, alter options, add/remove rules etc.). If a key with the same name as the current field is found in the preDefElements property, the QuickForm element object contained in that array will be used instead of auto-generating a new one. This allows for complete step-by-step customizing of your forms.

Note for date fields: HTML_QuickForm allows passing of an options array to the HTML_QuickForm_date element. You can define your own options array for date elements in your DataObject-derived classes by defining a method "dateOptions($fieldName)". FormBuilder will call that method whenever it encounters a date field and expects to get back a valid options array.


Parameters:

string   $action   —  The form action. Optional. If set to false (default), PHP_SELF is used.
string   $target   —  The window target of the form. Optional. Defaults to '_self'.
string   $formName   —  The name of the form, will be used in "id" and "name" attributes. If set to false (default), the class name is used
string   $method   —  The submit method. Defaults to 'post'.

[ Top ]

_getQFType   [line 1319]

string _getQFType( string $fieldType)

DB_DataObject_FormBuilder::_getQFType()

Returns the QuickForm element type associated with the given field type, as defined in the _elementTypeMap property. If an unknown field type is given, the returned type name will default to 'text'.

  • Return: The QuickForm element type name
  • Access: protected

Parameters:

string   $fieldType   —  The internal field type

[ Top ]

_reorderElements   [line 646]

mixed _reorderElements( )

DB_DataObject_FormBuilder::_reorderElements()

Changes the order in which elements are being processed, so that you can use QuickForm's default renderer or dynamic templates without being dependent on the field order in the database.

Make a class property named "preDefOrder" in your DataObject-derived classes which contains an array with the correct element order to use this feature.

  • Return: Array in correct order or FALSE if reordering was not possible
  • Author: Fabien Franzen <atelierfabien@home.nl>
  • Access: protected

[ Top ]


Documentation generated on Mon, 11 Mar 2019 10:16:40 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.