Tuesday, July 20, 2010

Validation framework for adobe director

Validation can be a real pain in the ass. After all no developer likes doing labor tasks. With Java, there are many excellent validation frameworks. To know about my take on validation with java see this article. Adobe director however doesn't have anything like it. So i decided to go ahead and make a simple framework in lingo.

Lets start with usage point of view. We need the name of text field, type of validator (regex, length etc..) and the error message when validation fails. Lets represent this as a list L = {"text field name", "Validator name", "error msg"}. To keep it simple, I only consider validation on text fields (I could have extended it, but it'd make the code look ugly, I wanted to keep it simple. Also, 99% of validation is on text fields)

Its obvious that a field can have multiple validators. Also, it'd be nice to validate everything in one call. So we can have input as List {L1, L2, ...Ln), where Li aforementioned list format.

But validators can have parameters. For example length validator can have 'length' parameter. So L should be List("text field name", List ("validator name", param1, param2, ...), "error message"). From the usage point of view we could have something like:
oValidator = new(script "ValidationUtil")
isValid = oValidator.validate([ \
["txtField1", ["NonEmptyValidator"],"Please enter textField1"], \
["txtSSN", ["LengthValidator", 4],"Please enter 4 digit ssn"], \
["txtSSN", ["NumberValidator"],"Please enter a numeric value"]
)
Predefined validators (functions) can be called by their string name using call(...) method.
Since we have all the necessary things are worked out, heres a complete listing of ValidationUtils class. You are free to use and modify this class as per your requirements. Just let me know if your are using it. At least i'll know it was useful to someone.
-----------------------------------------------------
-- This class manages all the validation stuff --
-- @author : Raghavendra Kotikalapudi --
-- @email : ragha.unique2000@gmail.com --
-----------------------------------------------------

--Currently works only for text fields..
--Checks for validation on the given validator..
on isValidOnValidator me, memberName, lstValidatorAndParams

val = sprite(memberName).text
--Extract validator name..
validator = lstValidatorAndParams[1]
otherParams = lstValidatorAndParams
--Remove validator name...it now has params only.
otherParams.deleteAt(1)
--Achieves dynamic function calling..
return call(symbol(validator), me, val, otherParams)

end

--This is the main function to be called.
on validate me, lstMembersAndValidators

ret = true
repeat with lst in lstMembersAndValidators

if isValidOnValidator(me, lst[1], lst[2]) = false then
alert(lst[3])
ret = false
exit repeat
end if

end repeat

return ret

end


--Validates non emptiness..
on NonEmptyValidator me, str, lstOptionalParams

if voidP(str) then
return false
else if length(str) = 0 then
return false
else
return true
end if

end

--Validates of length of str in in the range (low, hi) inclusive
--lstOptParams has low, high pair
on LengthRangeValidator me, str, lstOptionalParams

len = length(str)
if len >= lstOptionalParams[1] and len <= lstOptionalParams[2] then return true else return false end if end --Validates for non existence of special symbols.. --i.e, str can contain a-z, A-Z or 0-9 on NoSpecialSymbolsValidator me, str, lstOptionalParams foundCount = PRegEx_Search([str], "~+|`+|!+|@+|#+|\$+|%+|\^+|&+|\*+|\(+|\)+|\{+|\}+|\[+|\]+|\++|\\+|\|+|:+|;+|/+|\<+|\>+|\?+|,+")

--If special char is found, validate to false
if foundCount > 0 then
return false
else
return true
end if

end

--Validates if the given str is a valid name or not, i.e., it should not contain
--special symbols or 0-9
on NameValidator me, str, lstOptionalParams

val = NoSpecialSymbolsValidator(me, str, lstOptionalParams)
if val = false then
return false
else
foundCount = PRegEx_Search([str], "[0-9]+")
--If number if found..
if foundCount > 0 then
return false
else
return true
end if
end if

end
New Validators can be added as an when needed. PRegEX_search(...) comes from PRegEx Xtra. Fortunately, it is free to download. This framework handles error notifications by showing alert messages whenever validation fails. Neat isn't it.

If you have any suggestions or improvements, please let me know through comments. Hope you find this useful!

No comments:

Post a Comment