pclib  2.3.2
Lightweight PHP framework
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Form Class Reference

Create and manage forms. More...

Inheritance diagram for Form:
Tpl BaseObject

Public Member Functions

 validate ()
 Do form validation - return true if form pass validation rules. More...
 
 loadSession ()
 Load form values from session. More...
 
 saveSession ()
 Save form values to session. More...
 
 isEditable ($id, $testAttr=true)
 Is element $id editable?
 
 create ($tableName)
 Use default template for displaying database table content.
 
 getValue ($id)
 Return form value. More...
 
 print_Element ($id, $sub, $value)
 This function is called for each template tag when it is printed. More...
 
 print_Errors ()
 Print form error messages. More...
 
 print_Class ($id, $sub, $value)
 Print all form elements at once. More...
 
 print_Label ($id)
 Print label for form field. More...
 
 print_Input ($id, $sub, $value)
 Print HTML input field. More...
 
 print_Text ($id, $sub, $value)
 Print HTML TEXTAREA field. More...
 
 print_Button ($id, $sub, $value)
 Print form button. More...
 
 print_Checkbox_Radio_Group ($id, $sub, $value)
 Print group of radiobuttons or checkboxes. More...
 
 print_Checkbox ($id, $sub, $value, $i=null)
 Print INPUT TYPE=CHECKBOX field. More...
 
 print_Radio ($id, $sub, $value, $i=null)
 Print INPUT TYPE=RADIO field. More...
 
 print_ListInput ($id, $sub, $value)
 Print HTML5 input with datalist. More...
 
 print_Select ($id, $sub, $value)
 Print HTML SELECT field. More...
 
 preparedValues ($skipEmpty=false)
 Prepare form values for storing into database. More...
 
 upload ($tableName, $id, $old=array())
 Upload form files. More...
 
 insert ($tab)
 Insert form values into db-table $tab and save uploaded files into specified directory. More...
 
 update ($tab, $cond)
 Update form record in database table $tab, using where-condition $cond. More...
 
 delete ($tab, $cond)
 Delete form record and remove attached files (if any). More...
 
 content ()
 Return array of label => value(s) pairs for all elements of the form. More...
 
 mailTo ($to, $subj, $intro='', $hdr='')
 Send form by mail. More...
 
 ajaxSync ($elemList=null)
 Synchronize form elements on the client with server, without reloading page. More...
 
 dbSync ($tab)
 Set maxlength and size of the form fields, according underlying database table. More...
 
- Public Member Functions inherited from Tpl
 __construct ($path= '', $sessName= '')
 Load and parse template file. More...
 
 init ()
 Initialization - must be called after load()
 
 load ($path)
 Load template file. More...
 
 loadString ($s)
 Load string $s as template. More...
 
 out ($block=null)
 Display template populated with content. More...
 
 html ($block=null)
 Return html output of the template populated with content. More...
 
 enable ()
 Enable (show) or disable (hide) tag or block $name. More...
 
 disable ()
 Disable (hide) tag or block $name. More...
 
 set ($keystr, $value)
 Set attributes for template elements. More...
 
 getValue ($id)
 Return value of element $id. More...
 
 loadSession ()
 Load object from session.
 
 saveSession ()
 Save object to session.
 
 deleteSession ()
 Remove object from session.
 
 create ($tableName)
 Use default template for displaying database table content.
 
 compute ($id)
 Return computed value of element $id. More...
 
 print_Element ($id, $sub, $value)
 This function is called for each template tag when it is printed. More...
 
 print_Number ($id, $sub, $value)
 Print numeric $value. More...
 
 print_String ($id, $sub, $s)
 Print string $value. More...
 
 print_Bind ($id, $sub, $value)
 Bind $value to LABEL coming from datasource and print LABEL. More...
 
 print_Link ($id, $sub, $value)
 Create html link. More...
 
 print_Env ($id, $sub, $value)
 Print value from url (from _GET array). More...
 
 print_Class ($id, $sub, $value)
 Print all fields into template. More...
 
 print_Action ($id, $sub, $value)
 Call controller's method and include result into template. More...
 
 print_Block ($block)
 Print template block. More...
 
 getPopup ($id, $attr, $url)
 Generate javascript code for popup window. More...
 
 getItems ($id)
 Load lookup table for elements such as bind, select, check or radio. More...
 
- Public Member Functions inherited from BaseObject
 loadDefaults ($className=null)
 Load default parameters of class $className into object instance.
 
 setProperties (array $defaults)
 Set public properties of object from the array. More...
 
 addEvent ($name, $callback)
 Add event handler to $object->$name property. More...
 
 toArray ()
 Convert object to array. More...
 

Data Fields

 $submitted
 When form has been submitted, it contains name of pressed button.
 
 $onValidate
 Occurs after validation. More...
 
 $onUpload
 Occurs before uploading of files. More...
 
 $onSave
 Occurs before inserting or updating database. More...
 
 $onDelete
 Occurs before deleting from database. More...
 
 $invalid = array()
 Array of error messages filled by validate() function. More...
 
 $uploadBlackList = array('*.php','*.php?','*.phtml','*.exe','.htaccess')
 Files matching patterns cannot be uploaded by Form class. More...
 
 $useButtonTag = false
 Generate buttons with html button tag or as input type=button. More...
 
- Data Fields inherited from Tpl
 $onInit
 Occurs on template initialization. More...
 
 $onLoad
 Occurs after template is loaded and parsed. More...
 
 $onBeforeOut
 Occurs before output. More...
 
 $onAfterOut
 Occurs after output. More...
 
 $name
 Name of the template. More...
 
 $elements = array()
 Array of elements loaded from <?elements ? > section.
 
 $values = array()
 Array of template values. More...
 
 $db
 var Db
 
 $translator
 var Translator
 
 $router
 var Router
 
 $useXhtml = false
 Generate XHTML code. More...
 
 $escapeHtmlFunction
 Function for escaping html in template values. More...
 
- Data Fields inherited from BaseObject
 $onNewInstance
 Occurs when new instance of the class is created. More...
 
 $onMissingMethod
 Occurs when called method does not exist. More...
 
 $serviceLocator
 var function() Return service object when requested with service(). More...
 

Protected Member Functions

 _out ($block=null)
 Display form. More...
 
 getHttpData ()
 Return sanitized _POST (_GET) data. More...
 
 getTag ($id, $ignoreHtmlAttr=false)
 Prepare form tag with common attributes. More...
 
 checkboxToArray ($value)
 Convert checkbox $value (int|string) to array.
 
 trPrintElement ($elem)
 Helper for print_class()
 
 divPrintElement ($elem)
 Helper for print_class()
 
 fileName ($id)
 Create file name based on element attributes.
 
 toSqlDate ($dtstr, $fmtstr= '')
 HUMAN DATE => DATABASE DATE.
 
 toNumber ($value)
 Bypass non-numerical characters.
 
 toBitField (array $bitArray)
 Convert array of bits to integer. More...
 
 escape ($s)
 escape value for using in form input
 
 head ()
 return <form> header
 
 foot ()
 return </form> footer html
 
- Protected Member Functions inherited from Tpl
 getAttr ($id, $attr)
 Return attribute of element $id. More...
 
 getVariable ($id)
 Get template variable tvar... More...
 
 getRowNo ()
 Return row number of the current block. More...
 
 createFromTable ($tableName, $templatePath)
 Use default template for displaying database table content.
 
 trPrintElement ($elem)
 Print element in table layout. More...
 
 getUrl ($elem)
 Return url for the element (button, link) with completed parameters. More...
 
 formatDate ($dtstr, $fmt= '')
 DATABASE DATE => HUMAN DATE (in strftime() format) More...
 
 formatStr ($s, $fmt)
 Format string $s according format $fmt.
 
- Protected Member Functions inherited from BaseObject
 fireEvent ($name, array $args=array())
 Run all event handlers in $object->$name property. More...
 
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Protected Attributes

 $validator
 var FormValidator Form validator. More...
 
 $className = 'form'
 Name of the 'class' element.
 
- Protected Attributes inherited from Tpl
 $sessName
 Name of the session variable where template values are stored. More...
 
 $app
 var App Link to application object. More...
 
 $config = null
 Link to array of configuration parameters. More...
 
 $document
 Document array - It contains parsed template. More...
 
 $parser
 var TplParser
 

Additional Inherited Members

- Static Public Member Functions inherited from BaseObject
static defaults ()
 Set or retireve default parameters of the object. More...
 

Detailed Description

Create and manage forms.

Features:

See Template tags implemented by class Form for description of implemented tags.
In addition, you can use common template elements (see Template tags implemented by class Tpl)

Member Function Documentation

_out (   $block = null)
protected

Display form.

Parameters
string$blockIf set, only block $block will be shown.
ajaxSync (   $elemList = null)

Synchronize form elements on the client with server, without reloading page.

Parameters
string$elemListColon separated list of elements to synchronize
Returns
string json-data
content ( )

Return array of label => value(s) pairs for all elements of the form.

Helper for showing form content to the end user. Used in mailTo().

Returns
array $values
dbSync (   $tab)

Set maxlength and size of the form fields, according underlying database table.

Parameters
$tabTable name
delete (   $tab,
  $cond 
)

Delete form record and remove attached files (if any).

Parameters
string$tabdatabase table name
string$condwhere-condition of the query
See also
insert()
getHttpData ( )
protected

Return sanitized _POST (_GET) data.

Returns
array $data
getTag (   $id,
  $ignoreHtmlAttr = false 
)
protected

Prepare form tag with common attributes.

Returns
array $tag
getValue (   $id)

Return form value.

insert (   $tab)

Insert form values into db-table $tab and save uploaded files into specified directory.

Columns of the table must have same names like keys in $values array. Convert numbers, dates and checkboxes to database format automatically.

Parameters
string$tabdatabase table name
Returns
int $inserted_id
loadSession ( )

Load form values from session.

Called when $sessname in constructor is set. Do not call directly. Useful, if you need keep form values after page-reload.

mailTo (   $to,
  $subj,
  $intro = '',
  $hdr = '' 
)

Send form by mail.

Parameters
string$to- destination mail address
string$subj- mail subject
string$intro- text of the mail before list of form values
string$hdr- headers
preparedValues (   $skipEmpty = false)

Prepare form values for storing into database.

Convert date, number, remove unwanted fields etc.

Parameters
bool$skipEmptyRemove empty fields?
Returns
array $values
print_Button (   $id,
  $sub,
  $value 
)

Print form button.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Checkbox (   $id,
  $sub,
  $value,
  $i = null 
)

Print INPUT TYPE=CHECKBOX field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Checkbox_Radio_Group (   $id,
  $sub,
  $value 
)

Print group of radiobuttons or checkboxes.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Class (   $id,
  $sub,
  $value 
)

Print all form elements at once.

Uses simple table layout for presentation. Implement {form.fields} placeholder.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Element (   $id,
  $sub,
  $value 
)

This function is called for each template tag when it is printed.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Errors ( )

Print form error messages.

Called for {errors} tag.

See also
validate()
print_Input (   $id,
  $sub,
  $value 
)

Print HTML input field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Label (   $id)

Print label for form field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_ListInput (   $id,
  $sub,
  $value 
)

Print HTML5 input with datalist.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Radio (   $id,
  $sub,
  $value,
  $i = null 
)

Print INPUT TYPE=RADIO field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Select (   $id,
  $sub,
  $value 
)

Print HTML SELECT field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
print_Text (   $id,
  $sub,
  $value 
)

Print HTML TEXTAREA field.

Template element handler
Get element definition from Tpl->elements and write element value from Tpl->values into template.
Note
This function comes in form print_*() where "*" is usually TAGNAME which can be used in <?elements ?> section of template.
Parameters
string$idname of template tag {ID}
string$submodificator (second part) of template tag name {ID.sub}
mixed$valuevalue of element going from Tpl->values array
saveSession ( )

Save form values to session.

Do not call directly.

See also
loadsession()
toBitField ( array  $bitArray)
protected

Convert array of bits to integer.

update (   $tab,
  $cond 
)

Update form record in database table $tab, using where-condition $cond.

Parameters
string$tabdatabase table name
string$condwhere-condition of query
See also
insert()
upload (   $tableName,
  $id,
  $old = array() 
)

Upload form files.

Parameters
array$oldList of previous versions of files - will be deleted
validate ( )

Do form validation - return true if form pass validation rules.

Scan $elements and check validation rules, which are set in elements section of form template. If user does not fill form properly validate() return false and template tags {errors} and {TAGNAME.err} are set with error messages. You can use predefined validation rules with attributes required,email,date,number,file (see Template tags implemented by class Form) or even define your own rule (see PClib callback functions)

Returns
bool

Field Documentation

$invalid = array()

Array of error messages filled by validate() function.

You can set it on your own too. Error messages can be shown using {errors} or {fieldname.err} tag in template.

$onDelete

Occurs before deleting from database.

$onSave

Occurs before inserting or updating database.

$onUpload

Occurs before uploading of files.

$onValidate

Occurs after validation.

$uploadBlackList = array('*.php','*.php?','*.phtml','*.exe','.htaccess')

Files matching patterns cannot be uploaded by Form class.

$useButtonTag = false

Generate buttons with html button tag or as input type=button.

$validator
protected

var FormValidator Form validator.


The documentation for this class was generated from the following file: