Ferreteria/v2/usage/forms/creating: Difference between revisions
(extracted from parent page) |
m (15 revisions imported: moving this project here) |
||
(14 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
How to create a form for editing record-based data: | How to create a form for editing record-based data: | ||
from {{vbzcart/code|dropins/orders/order.php#L1507}} (function PageForm()): | from {{vbzcart/code|dropins/orders/order.php#L1507}} (function PageForm()): | ||
<php> | <php> | ||
$oForm = new fcForm_DB( | $oForm = new fcForm_DB($this); | ||
$oField = new | $oField = new fcFormField_<type>($oForm,'<field name>'); | ||
$oCtrl = new fcFormControl_HTML( | $oCtrl = new fcFormControl_HTML[_<type>]($oField,array(<HTML attributes>)); | ||
</php> | </php> | ||
The first line creates a Form object, the next line creates a Field, and the third line assigns a Control to the Field. Adding additional fields and controls follows the format of the last 2 lines. | The first line creates a Form object, the next line creates a Field, and the third line assigns a Control to the Field. Adding additional fields and controls follows the format of the last 2 lines. | ||
Recent changes: | |||
=== | * There is now also a Storage object (in addition to the Control object) for each Field (aka Native) object, but you don't need to create it (see next note). | ||
* Creating the Control object for each Field is now also optional; each Field (aka "native object") now has default Control and Storage classes which it instantiates when needed. You can access them via $oField->ControlObject() and $oField->StorageObject() respectively. | |||
==Fields and Controls== | |||
{| width=100% | |||
|- | |||
| valign=top | | |||
Field types ({{ferreteria/code|forms/field.php}}): | |||
* '''fcFormField_Text''' | |||
* '''fcFormField_Num''' | |||
* '''fcFormField_Time''' | |||
** ->Format({{arg|date format}}) | |||
| valign=top | | |||
Control types ({{ferreteria/code|forms/ctrl.php}}): | |||
* '''fcFormControl_HTML''' - generic single-line text-editing field | |||
* '''fcFormControl_HTML_TextArea''' - multiline text | |||
** attributes: rows, cols | |||
* '''fcFormControl_HTML_Hidden''' - hidden values | |||
* '''fcFormControl_HTML_DropDown''' - drop-down chooser | |||
** ->Records({{arg|dataset}}); | |||
* '''fcFormControl_HTML_CheckBox''' - checkbox | |||
* '''fcFormControl_HTML_Timestamp''' - time/date | |||
** ->Format({{arg|string}}); | |||
|} | |||
===Automatic Values=== | |||
In some cases, you want to set field values from code. This can include either forcing a value (user cannot edit it), or suggesting a default value which the user can edit or ignore. | |||
* To force a value (these can be done in either order): | |||
** Set the control to non-editable: <code>[control]->Editable(FALSE);</code> | |||
** Set the field value: <code>[field]->SetValue({{arg|value}});</code> | |||
** If you only want to set the value for new or non-new records (e.g. to set a WhenCreated or WhenEdited timestamp), you can put those lines inside a <code>if ($this->IsNew())</code> or <code>if (!$this->IsNew())</code> clause. | |||
* To suggest a default value that can be edited (this has not been tested): | |||
** <code>[field]->SetDefault({{arg|value}});</code> | |||
===Examples=== | |||
'''General''': | |||
<php>// set a default value for new records | |||
$oField->SetDefaultNative($this->TitleID());</php> | |||
'''Timestamp''': | |||
<php> | <php> | ||
$oField = new fcFormField_Time($ | $oField = new fcFormField_Time($oForm,'WhenEnt'); | ||
$oCtrl = new | $oCtrl = new fcFormControl_HTML_Timestamp($oField,array('size'=>10)); | ||
$ | $oCtrl->Format('m/Y'); | ||
</php> | </php> | ||
This sets the format of the Control for the "WhenEnt" field based on the parameter string used by the PHP [http://php.net/manual/en/function.date.php date() function] – in this case, just the month (2 digits) and year (4 digits) are displayed. | This sets the format of the Control for the "WhenEnt" field based on the parameter string used by the PHP [http://php.net/manual/en/function.date.php date() function] – in this case, just the month (2 digits) and year (4 digits) are displayed. | ||
'''Dropdown selector''': | |||
<php> | <php> | ||
$oField = new fcFormField_Num($oForm,'ID_Place'); | $oField = new fcFormField_Num($oForm,'ID_Place'); | ||
$oCtrl = new fcFormControl_HTML_DropDown( | $oCtrl = new fcFormControl_HTML_DropDown($oField,array()); | ||
$oCtrl->Records($this->PlaceTable()->GetData_forDropDown()); | $oCtrl->Records($this->PlaceTable()->GetData_forDropDown()); | ||
</php> | </php> | ||
=== | |||
See {{ | '''TextArea''': | ||
<php>$oCtrl = new fcFormControl_HTML_TextArea($oField,array('rows'=>3,'cols'=>60));</php> | |||
==Other Elements== | |||
See "{{l/same|displaying}}" for the care and feeding of form templates. |
Latest revision as of 16:46, 22 May 2022
How to create a form for editing record-based data:
from Template:Vbzcart/code (function PageForm()): <php> $oForm = new fcForm_DB($this); $oField = new fcFormField_<type>($oForm,'<field name>'); $oCtrl = new fcFormControl_HTML[_<type>]($oField,array(<HTML attributes>)); </php> The first line creates a Form object, the next line creates a Field, and the third line assigns a Control to the Field. Adding additional fields and controls follows the format of the last 2 lines.
Recent changes:
- There is now also a Storage object (in addition to the Control object) for each Field (aka Native) object, but you don't need to create it (see next note).
- Creating the Control object for each Field is now also optional; each Field (aka "native object") now has default Control and Storage classes which it instantiates when needed. You can access them via $oField->ControlObject() and $oField->StorageObject() respectively.
Fields and Controls
Field types (Template:Ferreteria/code):
|
Control types (Template:Ferreteria/code):
|
Automatic Values
In some cases, you want to set field values from code. This can include either forcing a value (user cannot edit it), or suggesting a default value which the user can edit or ignore.
- To force a value (these can be done in either order):
- Set the control to non-editable:
[control]->Editable(FALSE);
- Set the field value:
[field]->SetValue(<value>);
- If you only want to set the value for new or non-new records (e.g. to set a WhenCreated or WhenEdited timestamp), you can put those lines inside a
if ($this->IsNew())
orif (!$this->IsNew())
clause.
- Set the control to non-editable:
- To suggest a default value that can be edited (this has not been tested):
[field]->SetDefault(<value>);
Examples
General: <php>// set a default value for new records $oField->SetDefaultNative($this->TitleID());</php> Timestamp: <php> $oField = new fcFormField_Time($oForm,'WhenEnt'); $oCtrl = new fcFormControl_HTML_Timestamp($oField,array('size'=>10)); $oCtrl->Format('m/Y'); </php> This sets the format of the Control for the "WhenEnt" field based on the parameter string used by the PHP date() function – in this case, just the month (2 digits) and year (4 digits) are displayed.
Dropdown selector: <php> $oField = new fcFormField_Num($oForm,'ID_Place'); $oCtrl = new fcFormControl_HTML_DropDown($oField,array()); $oCtrl->Records($this->PlaceTable()->GetData_forDropDown()); </php>
TextArea: <php>$oCtrl = new fcFormControl_HTML_TextArea($oField,array('rows'=>3,'cols'=>60));</php>
Other Elements
See "displaying" for the care and feeding of form templates.