Source for file form.class.php
Documentation is available at form.class.php
// *** Tangra (Application Framework and Tools for PHP)
* Needed for Tangra_Form_Submit_Method::POST and Tangra_Form_Submit_Method::GET
require_once(TANGRA_MAIN_DIR. 'core/tangra_form_submit_method.class.php');
require_once(TANGRA_MAIN_DIR. 'form/fields/form_field.class.php');
* Needed for automatic change of form encoding to Form::ENCODING_MULTIPART
require_once(TANGRA_MAIN_DIR. 'form/fields/file/form_field_file.class.php');
require_once(TANGRA_MAIN_DIR. 'web_site/web_event_simple.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_form_field_already_exists.class.php');
require_once(TANGRA_MAIN_DIR. 'exceptions/te_form_field_not_exists.class.php');
* Class Form is used to handle most of the aspects when working with HTML forms
* Enter description here...
* Method of sending the form Tangra_Form_Submit_Method::POST/Tangra_Form_Submit_Method::GET
* Encoding Form::ENCODING_APPLICATION / Form::ENCODING_MULTIPART
* Action - page for action attribute
* Status of the submission of the form
* @param string $name Name of the form
* @param string $action Action page
* @param integer $method Tangra_Form_Submit_Method::POST or Tangra_Form_Submit_Method::GET
* @param integer $enctype Form::ENCODING_APPLICATION or Form::ENCODING_MULTIPART
function __construct($name, $action, $method = Tangra_Form_Submit_Method::POST, $enctype = Form::ENCODING_APPLICATION) {
* Gets method of form submission.
* Returned value will Tangra_Form_Submit_Method::POST or Tangra_Form_Submit_Method::GET
* Gets action property of the form
* Please note that if the field is Form_Field_File type, encoding of the form will be automatically changed to Form::ENCODING_MULTIPART (that way form will be submited correctly)
* @param Form_Field $field
* @throws TE_Form_Field_Already_Exists
public function add_field(Form_Field $field) {
$this->fields[$field->get_name()] = $field;
throw new TE_Form_Field_Already_Exists($field->get_name());
* Sets value of form fiels
* @param string $field_name Name of the field
* @param mixed $value Value of the field
* @throws TE_Form_Field_Not_Exists
$this->fields[$field_name]->set_value($value);
throw new TE_Form_Field_Not_Exists($field_name);
* @param string $field_name Name of the field
* @throws TE_Form_Field_Not_Exists
$ret = $this->fields[$field_name]->get_value();
throw new TE_Form_Field_Not_Exists($field_name);
* HTML value is value of the field as sent by HTTP POST/GET.
* It is not intended to be called directly by the user except in vary rare and specific circumstances.
* @param unknown_type $field_name
* @throws TE_Form_Field_Not_Exists
$ret = $this->fields[$field_name]->get_html_value();
throw new TE_Form_Field_Not_Exists($field_name);
* Returns array containing all fields.
* Returns field specified by $field_name
* @param string $field_name name of the field
* @throws TE_Form_Field_Not_Exists
$ret = &$this->fields[$field_name];
throw new TE_Form_Field_Not_Exists($field_name);
* Translates HTML values of the fields to "application" values.
foreach ($this->fields as $key => $value) {
$this->fields[$key]->translate_value_html2app();
* Performs value check on all fields
* Cycles all fields and call theirs basic_check method
foreach ($this->fields as $key => $val) {
$status |= $this->fields[$key]->basic_check();
* Returns status of the form
* Status of the form is the state of the last submission. If all fields are OK then status is true, otherwise - false
* Returns name of the submit button
* Name of the submit button contains form name and "_submit" suffix
* Returns name of the submit button with "_x" suffix
* When submit button is image, browsers return submit name plus _x and _y suffixes (position of the click in the image)
* Returns name of the submit button with "_y" suffix
* When submit button is image, browsers return submit name plus _x and _y suffixes (position of the click in the image)
* Transfers values from $_POST or $_GET into html_value ofd each field
* @param Context $context
$submit_array = $context->get_var('POST');
$submit_array = $context->get_var('GET');
$submit_array = array_merge($submit_array, array('_FILES' => $_FILES));
foreach ($this->fields as $field) {
$field->capture_submit($this->get_name(), $submit_array);
* Cycles all fields and calls their accept_submit method
foreach ($this->fields as &$field) {
* Checks if form is receiving submit, i.e. submit_name is present in $_POST or $_GET
* @param Context $context
$var_func = 'exists_in_post';
$var_func = 'exists_in_get';
//first listening for normal submit button capture
//second: for image submit button capture
* Each form field may have potential errors. Most of them are built-in,
* for example - 'mandatory_missing" which means that field is mandatory, but POST/GET
* does not contain value for it.
* Each field may have custom errors that are not set during basic_check. In this
* case, if error condition is true, application have to call set_field_error to tell the
* field that it is in error.
* @param string $field_name Name of the field
* @param string $potential_error_name Name of the error
$this->fields[$field_name]->set_error($potential_error_name);
throw new TE_Form_Field_Not_Exists($field_name);
* Returns field erros (if any)
* @param string $field_name Name of the field
$ret = $this->fields[$field_name]->get_errors();
throw new TE_Form_Field_Not_Exists($field_name);
* Returns event of form submission
* @return Web_Event_Simple
* Returns events of form submission both for <input type="submit" and <input type="image"
* @return arrray Simple array of Web_Event_Simple elements
* Creates submit event with capture specified by $submit_name
* @param string $submit_name
* @return Web_Event_Simple
* Sets the name of the form
* Name of the form must be alphanumeric (underscore is allowed), starting with letter, 50 characters max.
* Please note that $name will be converted to lowercase.
* @throws Tangra_Exception
if (ereg("^[a-z]{1}[a-z0-9_]{1,50}$", $name)) {
throw new Tangra_Exception('Invalid form name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.');
* Sets the method of submission
* Must be either Tangra_Form_Submit_Method::POST or Tangra_Form_Submit_Method::GET
* @throws Tangra_Exception
throw new Tangra_Exception('Invalid form method.');
* Must be either Form::ENCODING_APPLICATION or Form::ENCODING_MULTIPART
* @param integer $enctype
* @throws Tangra_Exception
case Form::ENCODING_APPLICATION:
case Form::ENCODING_MULTIPART:
throw new Tangra_Exception('Invalid encoding type. Must be either Form::ENCODING_APPLICATION or Form::ENCODING_MULTIPART');
|