Implement serverside and client side validatation

This commit is contained in:
YNH Webdev
2013-09-06 18:06:43 +02:00
parent 9f40c6355c
commit bbd48d24e4
19 changed files with 492 additions and 84 deletions

View File

@@ -18,7 +18,7 @@ feature {NONE}
make_decimal_validator (e: STRING)
do
make_regexp_validator ("[0-9]+(\\.[0-9]*)?|\\.[0-9]+", e)
make_regexp_validator ("^[0-9]+(\.[0-9]*)?$|^\.[0-9]+$", e)
end
end

View File

@@ -14,7 +14,7 @@ inherit
create
make_email_validator
feature{NONE}
feature {NONE}
make_email_validator (e: STRING)
do

View File

@@ -10,6 +10,9 @@ class
inherit
WSF_VALIDATOR [STRING]
redefine
state
end
create
make_regexp_validator
@@ -18,21 +21,32 @@ feature {NONE}
make_regexp_validator (r, e: STRING)
do
make(e)
make (e)
regexp_string := r
create regexp
regexp.compile (r)
end
feature -- Implementation
validate (input: STRING): BOOLEAN
is_valid (input: STRING): BOOLEAN
do
--Only compile when used
if not regexp.is_compiled then
regexp.compile (regexp_string)
end
Result := regexp.matches (input)
end
feature
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json ("WSF_REGEXP_VALIDATOR"), create {JSON_STRING}.make_json ("name"))
Result.put (create {JSON_STRING}.make_json (regexp_string), create {JSON_STRING}.make_json ("expression"))
Result.put (create {JSON_STRING}.make_json (error), create {JSON_STRING}.make_json ("error"))
end
regexp_string: STRING
regexp: REGULAR_EXPRESSION

View File

@@ -0,0 +1,20 @@
note
description: "Summary description for {WSF_VALIDATABLE}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
WSF_VALIDATABLE
feature
validate
deferred
end
is_valid: BOOLEAN
deferred
end
end

View File

@@ -16,7 +16,14 @@ feature {NONE}
feature
validate (input: G): BOOLEAN
state: JSON_OBJECT
do
create Result.make
Result.put (create {JSON_STRING}.make_json (generator), create {JSON_STRING}.make_json ("name"))
Result.put (create {JSON_STRING}.make_json (error), create {JSON_STRING}.make_json ("error"))
end
is_valid (input: G): BOOLEAN
deferred
end

View File

@@ -19,6 +19,8 @@ feature {NONE}
make_button (n: STRING; t: STRING)
do
make (n, "button")
add_class ("btn")
add_class ("btn-default")
text := t
end

View File

@@ -68,7 +68,7 @@ feature -- Implementation
if checked then
attributes := attributes + " checked"
end
Result := render_tag_with_tagname ("div",render_tag_with_tagname ("label", render_tag ("", attributes) + " " + label, "",""), "","checkbox")
Result := render_tag_with_tagname ("div", render_tag_with_tagname ("label", render_tag ("", attributes) + " " + label, "", ""), "", "checkbox")
end
value: BOOLEAN

View File

@@ -88,14 +88,14 @@ feature
loop
css_classes_string := css_classes_string + " " + c.item
end
Result:=render_tag_with_tagname(tag_name,body,attributes,css_classes_string)
Result := render_tag_with_tagname (tag_name, body, attributes, css_classes_string)
end
render_tag_with_tagname (tag, body, attributes,css_classes_string: STRING): STRING
render_tag_with_tagname (tag, body, attributes, css_classes_string: STRING): STRING
local
l_attributes: STRING
do
l_attributes:=attributes
l_attributes := attributes
if not css_classes_string.is_empty then
l_attributes := " class=%"" + css_classes_string + "%""
end

View File

@@ -11,6 +11,8 @@ inherit
WSF_MULTI_CONTROL [WSF_CONTROL]
WSF_VALIDATABLE
create
make_form_control
@@ -24,22 +26,23 @@ feature {NONE}
feature -- Validation
validate: BOOLEAN
validate
do
Result := True
is_valid := True
across
controls as c
until
Result = False
is_valid = False
loop
-- TODO: Change generic parameter of elm from ANY to <WILDCARD> if something like that is available in Eiffel.
-- Otherwise, check separately for STRING, LIST...
if attached {WSF_FORM_ELEMENT_CONTROL[ANY]} c.item as elem then
if attached {WSF_VALIDATABLE} c.item as elem then
elem.validate
if not elem.is_valid then
Result := False
is_valid := False
end
end
end
end
is_valid: BOOLEAN
end

View File

@@ -11,9 +11,13 @@ inherit
WSF_CONTROL
redefine
read_state_changes,load_state,read_state
read_state_changes,
load_state,
read_state
end
WSF_VALIDATABLE
create
make_form_element, make_form_element_with_validators
@@ -31,9 +35,8 @@ feature {NONE}
do
make (c.control_name + "_container", "div")
add_class ("form-group")
if attached {WSF_TEXT_CONTROL}c or attached {WSF_TEXTAREA_CONTROL}c then
if attached {WSF_INPUT_CONTROL} c or attached {WSF_TEXTAREA_CONTROL} c then
c.add_class ("form-control")
end
value_control := c
validators := v
@@ -41,21 +44,6 @@ feature {NONE}
error := ""
end
feature
is_valid (): BOOLEAN
do
Result := True
across
validators as c
until
not Result
loop
if not c.item.validate (value_control.value) then
Result := False
end
end
end
feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
load_state (new_states: JSON_OBJECT)
@@ -67,27 +55,37 @@ feature {WSF_PAGE_CONTROL, WSF_CONTROL} -- STATE MANAGEMENT
set_state (new_state: JSON_OBJECT)
do
value_control.set_state(new_state)
value_control.set_state (new_state)
end
read_state (states: JSON_OBJECT)
-- Read states in subcontrols
do
Precursor (states)
value_control.read_state(states)
value_control.read_state (states)
end
read_state_changes (states: JSON_OBJECT)
-- Read states_changes in subcontrols
do
Precursor (states)
value_control.read_state_changes(states)
value_control.read_state_changes (states)
end
state: JSON_OBJECT
--Read state
local
validator_description: JSON_ARRAY
do
create Result.make
create validator_description.make_array
across
validators as v
loop
validator_description.add (v.item.state)
end
Result.put (create {JSON_STRING}.make_json (value_control.control_name), create {JSON_STRING}.make_json ("value_control"))
Result.put (validator_description, create {JSON_STRING}.make_json ("validators"))
end
feature --EVENT HANDLING
@@ -96,12 +94,15 @@ feature --EVENT HANDLING
-- Pass callback to subcontrols
do
if equal (cname, control_name) then
if event.is_equal ("validate") then
validate
end
else
value_control.handle_callback (cname, event)
end
end
feature --Implementation
feature --Implementation
render: STRING
local
@@ -117,6 +118,42 @@ feature --Implementation
Result := render_tag (body, "")
end
feature -- Validation
add_validator (v: WSF_VALIDATOR [G])
do
validators.extend (v)
end
set_error (e: STRING)
do
error := e
state_changes.replace (create {JSON_STRING}.make_json (e), create {JSON_STRING}.make_json ("error"))
end
validate
local
current_value: G
do
current_value := value_control.value
is_valid := True
across
validators as c
until
not is_valid
loop
if not c.item.is_valid (current_value) then
is_valid := False
set_error (c.item.error)
end
end
if is_valid then
set_error ("")
end
end
is_valid: BOOLEAN
feature
value_control: WSF_VALUE_CONTROL [G]

View File

@@ -5,20 +5,21 @@ note
revision: "$Revision$"
class
WSF_TEXT_CONTROL
WSF_INPUT_CONTROL
inherit
WSF_VALUE_CONTROL [STRING]
create
make_text
make_input
feature {NONE}
make_text (n: STRING; v: STRING)
make_input (n: STRING; v: STRING)
do
make (n, "input")
type := "text"
text := v
end
@@ -61,7 +62,7 @@ feature -- Implementation
render: STRING
do
Result := render_tag ("", "type=%"text%" value=%"" + text + "%"")
Result := render_tag ("", "type=%"" + type + "%" value=%"" + text + "%"")
end
set_text (t: STRING)
@@ -81,6 +82,8 @@ feature
text: STRING
type: STRING
change_event: detachable PROCEDURE [ANY, TUPLE []]
end

View File

@@ -5,7 +5,7 @@ note
revision: "$Revision$"
class
WSF_MULTI_CONTROL[G -> WSF_CONTROL]
WSF_MULTI_CONTROL [G -> WSF_CONTROL]
inherit

View File

@@ -78,7 +78,6 @@ feature
control.read_state (states)
data := "<html><head>"
data.append ("<link href=%"//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css%" rel=%"stylesheet%">")
data.append ("</head><body>")
data.append (control.render)
data.append ("<script type=%"text/javascript%">window.states=")

View File

@@ -0,0 +1,25 @@
note
description: "Summary description for {WSF_PASSWORD_CONTROL}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
WSF_PASSWORD_CONTROL
inherit
WSF_INPUT_CONTROL
create
make_password
feature {NONE}
make_password (n: STRING; v: STRING)
do
make_input (n, v)
type := "password"
end
end

View File

@@ -9,7 +9,7 @@ class
inherit
WSF_TEXT_CONTROL
WSF_INPUT_CONTROL
redefine
render
end
@@ -21,7 +21,7 @@ feature {NONE}
make_textarea (n, t: STRING)
do
make_text (n, t)
make_input (n, t)
tag_name := "textarea"
end