pclib  3.0.0
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, $templatePath='')
 Use default template for displaying database table content.
 
 getValue ($id)
 Return form value.
 
 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=[])
 Upload form files. More...
 
 getFile ($id)
 Return content of uploaded file. 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...
 
 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...
 
 setAttr ($id, $value, $value2=null)
 Set attribute globally for template elements. More...
 
 getBlock ($block)
 Return values of block $block. More...
 
 deleteSession ()
 Remove object from session.
 
 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_Action ($id, $sub, $value)
 Call controller's method and include result into template. More...
 
 print_Block ($block)
 Print template block. More...
 
 addTag ($line)
 Add new element. More...
 
 addType ($name, $fn)
 Add new type of element handled by callback function $fn. More...
 
 setFields ($fields)
 Show only this $fields in template (using tag {grid.fields} or {form.fields}). More...
 
 htmlTag ($name, $attr=[], $content=null)
 Return html tag $name with attributes. 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...
 
 toArray ()
 Convert object to array. More...
 

Data Fields

 $submitted
 When form has been submitted, it contains name of pressed button.
 
 $invalid = []
 Array of error messages filled by validate() function. More...
 
 $submitDisabledInputs = true
 Should submit disabled input fields?
 
 $uploadBlackList = array('*.php','*.php?','*.phtml','*.exe','.htaccess')
 Files matching patterns cannot be uploaded by Form class.
 
 $useButtonTag = true
 Generate buttons with html button tag or as input type=button.
 
- Data Fields inherited from Tpl
 $name
 Name of the template.
 
 $elements = array()
 Array of elements loaded from <?elements ? > section.
 
 $values = array()
 Array of template values.
 
 $db
 var Db
 
 $router
 var Router
 
 $escapeHtmlFunction
 Function for escaping html in template values.
 
- Data Fields inherited from BaseObject
 $serviceLocator
 var function() Return service object when requested with service().
 

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.
 
 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...
 
 getRowNo ()
 Return row number of the current block.
 
 createFromTable ($tableName, $templatePath)
 Use default template for displaying database table content.
 
 compute ($id)
 Return computed value of element $id.
 
 getPopup ($id, $attr, $url)
 Generate javascript code for popup window. More...
 
 getUrl ($elem)
 Return url for the element (button, link) with completed parameters.
 
 formatDate ($dtstr, $fmt='')
 DATABASE DATE => HUMAN DATE (in date() format) More...
 
 formatStr ($s, $fmt)
 Format string $s according format $fmt.
 
 getItems ($id)
 Load lookup table for elements such as bind, select, check or radio. More...
 
- Protected Member Functions inherited from BaseObject
 service ($service, $default=null)
 Try acquire $service and load it into property $this->$service. More...
 

Protected Attributes

 $validator
 var FormValidator Form validator.
 
 $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.
 
 $config = null
 Link to array of configuration parameters.
 
 $document
 Document array - It contains parsed template.
 
 $parser
 var TplParser
 
 $fields = []
 List of id's to be printed in {grid.fields} tag.
 
 $types = []
 Additional element type handlers.
 

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()

_out (   $block = null)
protected

Display form.

Parameters
string$blockIf set, only block $block will be shown.

Reimplemented from Tpl.

◆ content()

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()

dbSync (   $tab)

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

Parameters
$tabTable name

◆ delete()

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()

◆ getFile()

getFile (   $id)

Return content of uploaded file.

Parameters
string$idInput id
Returns
string $file|false

◆ getHttpData()

getHttpData ( )
protected

Return sanitized _POST (_GET) data.

Returns
array $data

◆ getTag()

getTag (   $id,
  $ignoreHtmlAttr = false 
)
protected

Prepare form tag with common attributes.

Returns
array $tag

◆ insert()

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()

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.

Reimplemented from Tpl.

◆ mailTo()

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()

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()

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()

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()

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()

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

Reimplemented from Tpl.

◆ print_Element()

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

Reimplemented from Tpl.

◆ print_Errors()

print_Errors ( )

Print form error messages.

Called for {errors} tag.

See also
validate()

◆ print_Input()

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()

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()

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()

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()

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()

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()

saveSession ( )

Save form values to session.

Do not call directly.

See also
loadsession()

Reimplemented from Tpl.

◆ update()

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()

upload (   $tableName,
  $id,
  $old = [] 
)

Upload form files.

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

◆ validate()

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

$invalid = []

Array of error messages filled by validate() function.

Error messages can be shown using {errors} or {fieldname.err} tag in template.


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