Automatic generation produced by ISE Eiffel
indexing
description: "class that provides basic form validation functions"
author: "Peizhu Li, <lip@student.ethz.ch>"
date: "10.12.2007"
revision: "$0.6$"
class
FORM_VALIDATOR
create
make
feature -- Attributes
context: REQUEST_DISPATCHER
-- actual HTTP context
last_value: STRING_8
-- value from last checked field
feature -- creation
make (a_context: REQUEST_DISPATCHER)
-- initialization
do
context := a_context
end
feature -- CGI_FORMS
text_field_value (field_name: STRING_8): STRING_8
-- get a text field value as string /wrapper of CGI_FORM text_field_value
do
last_value := context.text_field_value (field_name)
last_value.left_adjust
last_value.right_adjust
Result := last_value
end
button_value (field_name, overriding_value: STRING_8): BOOLEAN
-- wrapper of CGI_FORM button_value
do
Result := context.button_value (field_name, overriding_value)
last_value := Result.out
end
menu_values (field_name: STRING_8): LINKED_LIST [STRING_8]
-- wrapper of CGI_FORM menu_values
do
Result := context.menu_values (field_name)
last_value := Result.out
end
fields: ARRAY [STRING_8]
-- wrapper of CGI_FORM fields
do
Result := context.fields
end
value_count (field_name: STRING_8): INTEGER_32
-- wrapper of CGI_FORM value_count
do
Result := context.value_count (field_name)
end
value_list (field_name: STRING_8): LINKED_LIST [STRING_8]
-- wrapper of CGI_FORM value_list
do
Result := context.value_list (field_name)
end
field_defined (field_name: STRING_8): BOOLEAN
-- wrapper of CGI_FORM field_defined
do
Result := context.field_defined (field_name)
end
feature -- Access
get_field_string (a_field_name: STRING_8): STRING_8
-- retrieve the string value of a text field with the specified name.
require
field_name_valid: a_field_name /= Void and then not a_field_name.is_empty
do
if field_defined (a_field_name) then
Result := text_field_value (a_field_name)
Result.left_adjust
Result.right_adjust
else
create Result.make_empty
end
last_value := Result
end
get_field_integer (a_field_name: STRING_8): INTEGER_32
-- retrieve the integer value of a given field
require
field_name_valid: a_field_name /= Void and then not a_field_name.is_empty
field_defined: field_defined (a_field_name) and then is_field_value_not_empty (a_field_name)
do
Result := get_field_string (a_field_name).to_integer
last_value := Result.out
end
get_field_double (a_field_name: STRING_8): REAL_64
-- retrieve the double value of a given field
require
field_name_valid: a_field_name /= Void and then not a_field_name.is_empty
field_defined: field_defined (a_field_name) and then is_field_value_not_empty (a_field_name)
do
Result := get_field_string (a_field_name).to_double
last_value := Result.out
end
feature -- validation
is_string_not_empty (value: STRING_8): BOOLEAN
--is field value valid?
do
if value /= Void then
value.left_adjust
value.right_adjust
if (not value.is_empty) then
Result := True
end
end
end
is_field_value_not_empty (field: STRING_8): BOOLEAN
--is field value valid?
do
if field_defined (field) and then text_field_value (field) /= Void then
last_value := text_field_value (field)
last_value.left_adjust
last_value.right_adjust
if (not last_value.is_empty) then
Result := True
end
else
last_value := ""
end
end
is_email_valid (email: STRING_8): BOOLEAN
--is email valid? (non-void, non-empty after eliminating leading and trailing spaces, with only one '@')
do
if email /= Void then
email.left_adjust
email.right_adjust
if (not email.is_empty and then email.occurrences ('@') = 1) then
Result := True
end
end
end
is_date_valid (day, month, year: STRING_8): BOOLEAN
-- is date valid?
local
date_checker: DATE_VALIDITY_CHECKER
do
if is_string_not_empty (day) and is_string_not_empty (month) and is_string_not_empty (year) then
if day.is_integer and month.is_integer and year.is_integer then
create date_checker
if date_checker.is_correct_date (year.to_integer, month.to_integer, day.to_integer) then
Result := True
end
end
end
end
is_non_required_date_valid (day, month, year: STRING_8): BOOLEAN
-- is date which is not required valid? It accepts 'n/a'
local
date_checker: DATE_VALIDITY_CHECKER
do
if is_string_not_empty (day) and is_string_not_empty (month) and is_string_not_empty (year) then
if day.is_equal ("n/a") or month.is_equal ("n/a") or year.is_equal ("n/a") then
Result := True
elseif day.is_integer and month.is_integer and year.is_integer then
create date_checker
if date_checker.is_correct_date (year.to_integer, month.to_integer, day.to_integer) then
Result := True
end
end
end
end
is_must_field_filled (a_field_name: STRING_8): BOOLEAN
-- check a field value for non emptyness
require
field_name_valud: a_field_name /= Void and then not a_field_name.is_empty
do
create last_value.make_from_string (get_field_string (a_field_name))
if not last_value.is_empty then
Result := True
end
end
is_field_in_range (field_name: STRING_8; min, max: REAL_64): BOOLEAN
-- whether a integer/float field in specified min/max range
require
field_name_valud: field_name /= Void and then not field_name.is_empty
field_exists: is_must_field_filled (field_name)
local
value: REAL_64
do
value := get_field_double (field_name)
if value >= min and then value <= max then
Result := True
end
end
are_fields_equal (name1, name2: STRING_8): BOOLEAN
-- whether two fields have same values
require
field_name_valud: name1 /= Void and then not name1.is_empty and then name2 /= Void and then not name2.is_empty
fields_exist: is_must_field_filled (name1) and then is_must_field_filled (name2)
local
value1, value2: STRING_8
do
create value1.make_from_string (get_field_string (name1))
create value2.make_from_string (get_field_string (name2))
if value1.is_equal (value2) then
Result := True
end
end
is_must_fields_valid (names: ARRAY [STRING_8]): ARRAY [STRING_8]
-- check a given must field list to see whether they are filled with values. return an array contains fields names that failed validation.
local
i, j: INTEGER_32
do
create Result.make (1, 100)
j := 1
from
i := names.lower
until
i > names.upper
loop
if names.item (i) /= Void then
if not is_must_field_filled (names.item (i)) then
Result.put (names.item (i), j)
j := j + 1
end
end
i := i + 1
end
end
invariant
invariant_clause: True
end -- class FORM_VALIDATOR
-- Generated by ISE Eiffel --
For more details: www.eiffel.com