[
class tree: tangra_lib
] [
index: tangra_lib
] [
all elements
]
tangra_lib
Packages:
tangra_lib
Source for file form_field.class.php
Documentation is available at
form_field.class.php
<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//
/**
* Contains class Form_Field_Date
*
*
@package
tangra_lib
*
@subpackage
form
*/
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_already_exists.class.php'
)
;
/**
*
*/
require_once
(
TANGRA_MAIN_DIR
.
'exceptions/te_key_not_exists.class.php'
)
;
/**
* Represents abstract concept of HTML form field
*
*
@package
tangra_lib
*
@subpackage
form
*/
abstract
class
Form_Field
extends
Tangra_Class
{
/**
* Name of the field
*
*
@var
string
*
@internal
*/
private
$name
;
/**
* Is field required?
*
*
@var
boolean
*
@internal
*/
private
$required
;
/**
* Value of the field
*
*
@var
mixed
*
@internal
*/
private
$value
;
/**
* HTML value of the field
*
*
@var
string
*
@internal
*/
private
$html_value
;
/**
* Default value
*
*
@var
mixed
*
@internal
*/
private
$default_value
;
/**
* Type of the field. Contains class name of the field object
*
*
@var
string
*
@internal
*/
private
$type
;
/**
* Array with potential errors
*
*
@var
array
*
@internal
*/
private
$potential_errors
=
array
(
)
;
/**
* Array with current errors
*
*
@var
array
*
@internal
*/
private
$current_errors
=
array
(
)
;
/**
* Constructor
*
*
@param
string
$name
Name of the field
*
@param
mixed
$value
Default value.
*
@param
boolean
$required
Is field required. Default is false
*/
function
__construct
(
$name
,
$value
,
$required
=
false
)
{
$this
->
set_name
(
$name
)
;
$this
->
set_required
(
$required
)
;
$this
->
set_value
(
$value
)
;
$this
->
set_default_value
(
$value
)
;
$this
->
type
=
get_class
(
$this
)
;
$this
->
add_potential_error
(
'mandatory_missing'
)
;
}
/**
* Sets field's name
*
*
@param
string
$name
*/
protected
final
function
set_name
(
$name
)
{
if
(
ereg
(
"^[a-z]{1}[a-z0-9_]{1,50}$"
,
$name
))
{
$this
->
name
=
strtolower
(
$name
)
;
}
else
{
throw
new
Tangra_Exception
(
'Invalid field name. Must be alphanumeric (underscore allowed), starting with letter, 50 characters max.'
)
;
}
}
/**
* Returns field name
*
*
@return
string
*/
public
final
function
get_name
(
)
{
return
$this
->
name
;
}
/**
* Sets field's value
*
*
@param
mixed
$value
*/
public
function
set_value
(
$value
)
{
$this
->
value
=
$value
;
}
/**
* Returns field's value
*
*
@return
mixed
*/
public
function
get_value
(
)
{
return
$this
->
value
;
}
/**
* Sets will the field be required
*
*
@param
boolean
$required
*/
public
function
set_required
(
$required
)
{
$this
->
required
=
$required
?
true
:
false
;
}
/**
* Returns if the field is required
*
*
@return
unknown
*/
public
function
get_required
(
)
{
return
$this
->
required
;
}
/**
* Returns type of the field (class name)
*
*
@return
string
*/
public
function
get_type
(
)
{
return
$this
->
type
;
}
/**
* Sets default value
*
*
@param
mixed
$default_value
*/
public
function
set_default_value
(
$default_value
)
{
$this
->
default_value
=
$default_value
;
}
/**
* Returns default value
*
*
@return
mixed
*/
public
function
get_default_value
(
)
{
return
$this
->
default_value
;
}
/**
* Set field's value to default value
*
*/
public
function
set_to_default
(
)
{
$this
->
value
=
$this
->
set_value
(
$this
->
default_value
)
;
}
/**
* Performs basic check for validity
*
*
@return
boolean
*
@internal
*/
public
function
basic_check
(
)
{
$has_errors
=
false
;
if
(
$this
->
required
&&
$this
->
get_html_value
(
)
==
NULL
)
{
$has_errors
=
true
;
$this
->
set_error
(
'mandatory_missing'
)
;
}
else
{
$this
->
clear_errors
(
)
;
}
return
$has_errors
;
}
/**
* Captures submit
*
*
@param
string
$form_name
*
@param
array
$submit_array
*
@internal
*/
public
function
capture_submit
(
$form_name
,
&
$submit_array
)
{
if
(
array_key_exists
(
$form_name
.
'_'
.
$this
->
get_name
(
)
,
$submit_array
))
{
$this
->
set_html_value
(
$submit_array
[
$form_name
.
'_'
.
$this
->
get_name
(
)
]
)
;
}
else
{
$this
->
set_html_value
(
NULL
)
;
}
}
/**
* Accepts submit data
*
@internal
*/
public
function
accept_submit
(
)
{
if
(
!
$this
->
get_errors
(
))
{
$this
->
translate_value_html2app
(
)
;
}
}
/**
* Adds potential error for the field
*
*
@param
string
$error_name
Name of the error
*/
public
function
add_potential_error
(
$error_name
)
{
if
(
ereg
(
"^[a-z]{1}[a-z0-9_]{1,50}$"
,
$error_name
))
{
if
(
!
array_key_exists
(
$error_name
,
$this
->
potential_errors
))
{
$this
->
potential_errors
[
$error_name
]
=
$error_name
;
}
else
{
throw
new
TE_Key_Already_Exists
(
$error_name
)
;
}
}
else
{
throw
new
Tangra_Exception
(
'Invalid error name. Must be alphanumeric, starting with letter. Underscore is allowed.'
)
;
}
}
/**
* Returns array with field properties
*
*
@return
array
*
@internal
*/
public
function
get_properties_array
(
)
{
$ret
[
'name'
]
=
$this
->
get_name
(
)
;
$ret
[
'required'
]
=
$this
->
get_required
(
)
;
$ret
[
'type'
]
=
strtolower
(
$this
->
get_type
(
))
;
$ret
[
'value'
]
=
$this
->
is_field_in_error
(
)
?
$this
->
get_html_value
(
)
:
$this
->
get_value
(
)
;
$ret
[
'p_errors'
]
=
array
(
)
;
foreach
(
$this
->
potential_errors
as
$err
)
{
array_push
(
$ret
[
'p_errors'
]
,
$err
)
;
}
foreach
(
$this
->
current_errors
as
$err_name
)
{
$ret
[
'current_errors'
]
[
$err_name
]
=
$this
->
potential_errors
[
$err_name
]
;
}
return
$ret
;
}
/**
* Nonsense function. Candidate for removal
*
*
@param
string
$error_name
*
@return
string
*
@internal
*/
public
function
get_potential_error
(
$error_name
)
{
$ret
=
false
;
if
(
array_key_exists
(
$error_name
,
$this
->
potential_errors
))
{
$ret
=
$this
->
potential_errors
[
$error_name
]
;
}
else
{
throw
new
TE_Key_Not_Exists
(
$error_name
)
;
}
return
$ret
;
}
/**
* Sets field current error
*
*
@param
string
$potential_error_name
Name of the error
*/
public
function
set_error
(
$potential_error_name
)
{
array_push
(
$this
->
current_errors
,
$this
->
get_potential_error
(
$potential_error_name
))
;
}
/**
* Return field's current errors
*
*
@return
unknown
*/
public
function
get_errors
(
)
{
return
$this
->
current_errors
;
}
/**
* Clear current errors
*
*/
public
function
clear_errors
(
)
{
$this
->
current_errors
=
array
(
)
;
}
/**
* Transfers data from html_value to value
*
@internal
*/
protected
function
translate_value_html2app
(
)
{
$this
->
value
=
$this
->
get_html_value
(
)
;
}
/**
* Sets html_value
*
*
@param
mixed
$html_value
*/
protected
function
set_html_value
(
$html_value
)
{
$this
->
html_value
=
$html_value
;
}
/**
* Returns html_value
*
*
@return
mixed
*/
public
function
get_html_value
(
)
{
return
$this
->
html_value
;
}
/**
* Checks if field is in error
*
*
@return
boolean
*
@internal
*/
protected
function
is_field_in_error
(
)
{
$field_errors
=
$this
->
get_errors
(
)
;
return
$field_errors
?
true
:
false
;
}
}