This is a simple, little PHP5 class I wrote to automate and simplify
the usually tedious process of validating user entries from HTML forms.
It is really very easy to use, but offers a good degree of control over
how your data is validated and how errors are displayed. The public member
functions are listed below, and you can view a simple example
here. It can prove to be a very
handy tool for you PHP web programmers. Let me know what you think or
if you improve on it.
[0 views]
[0 views]
__construct($method = null)
This is the only constructor for an SFV object. It has on optional
argument: $method. It may be NULL, "POST", or "GET" and, if specified,
will be the source for input data for the form variables.
add_var($label, $var_name, $var_type = "string",
$max_len = 0, $min_len = 1, $valid_ereg = null)
This allows you to add a single form variable and its constraints.
The arguments are:
$label - the label of the control for printing error messages
$var_name - the "name" of the control
$var_type - type of data permitted; may be one of "bool", "float",
"ufloat", "int", "uint", "string"
$max_len - maximum allowed length or 0 for no limit
$min_len - minimum required length or 0 for no minimum
$valid_ereg - POSIX pattern for matching
get_data($var_name)
This returns the user-entered data for a field specified by the
parameter: $var_name
set_data($var_name, $data)
This allows you to modify the data associated with a form
variable contained in an object.
get_checked($var_name)
This is handy for checkbox controls. It returns the string
"checkbox" for a boolean control that is not false specified
by param $var_name.
can_validate($submit_name)
This returns true if the form control name specified by param
$submit_name exists in either $_POST or $_GET (depending on
the method specified).
validate($submit_name = null)
If the optional parameter $submit_name is supplied all form
variables contained in the object will be validated, but if it
is NULL, any variables present will be validated. This will
return TRUE if all variables are valid, FALSE if not all are
valid, and NULL if $submit_name has not been submitted.
get_errors($var_name)
This returns an array of errors (or NULL if none) for the
form variable specified by its param: $var_name.
print_errors($var_name = null, $style = SFV_ERROR_STYLE,
$label_style = SFV_LABEL_STYLE, $table_style = SFV_TABLE_STYLE)
This will print a nice listing of all errors encountered
during validation. You can print for just a single variable
or all of them and specify the styles for the control labels,
error messages, and the table in which they are all printed.