Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  [Idea] A more generic validation function

dmitriid
Posted: Tue Apr 22, 2008 11:05 am Reply with quote
User Joined: 17 Aug 2006 Posts: 213
Let's say that you have these fields in your form:- login
- password
- repeat_password
- repeat_password_once_more
View user's profile Send private message
dmitriid
Posted: Tue Apr 22, 2008 11:43 am Reply with quote
User Joined: 17 Aug 2006 Posts: 213
Forgot to actually attach the file *blush*


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---



Post recived from mailinglist
View user's profile Send private message
dmitriid
Posted: Tue Apr 22, 2008 1:50 pm Reply with quote
User Joined: 17 Aug 2006 Posts: 213
Well, here's the more complete version of the code. Ugly as sin, but oh well

If you have a form with three fields
- login
- password
- pasword_repeat


you may try to validate the form as follows:


F = fun(A, Field1, Field2) -> [Field1, {error}];
validate(A, [
View user's profile Send private message
dmitriid
Posted: Tue Apr 22, 2008 1:53 pm Reply with quote
User Joined: 17 Aug 2006 Posts: 213
I should *really* lear to attach files fist and then hit the "Send"
button

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---



Post recived from mailinglist
View user's profile Send private message
dmitriid
Posted: Tue Apr 22, 2008 6:19 pm Reply with quote
User Joined: 17 Aug 2006 Posts: 213
Sorry or spaming the list Smile

Here's yet another version:


Create a form that contains 4 fields:
- login
- password
- password_repeat
- email


and the following validation function:


process_form(A) ->
View user's profile Send private message
dmitriid
Posted: Wed Apr 23, 2008 3:59 pm Reply with quote
User Joined: 17 Aug 2006 Posts: 213
Yup, it's me again Smile Minor improvements to the function

Same stuff. Create a form that contains 4 fields:
- login
- password
- password_repeat
- email

and the following validation function:
process_signup(A) ->
F = fun(A, Field) ->
{ok, Val} = yaws_api:postvar(A, Field),
L = string:len(Val),
if
L < 4 orelse L > 16 ->
{Field, {length}};
true ->
{}
end
end,
EmailCheck = fun(Args, Field2) ->
{ok, Email} = yaws_api:postvar(Args, Field2),
Match = regexp:match(Email, "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-
Za-z]+"),
Match /= nomatch
end,

%% the magic is here Smile
buktu_form:validate(A, [
{login, [F, {'=', "User"}]},
{email, EmailCheck},
{password, [{'=', password_repeat}, F, {'=', "Password"}]}
]).


Couple of notes:
- the function that you pass in can return:
-- a tuple in the form {FieldName, Error}
-- true if validation succeeded, false if validation failed
-- arbitrary Value which will be converted to {FieldName, Value}

- if any of the fields in the rule don't exist or are empty, for each
such rule the function will return
-- {invalid_field} if you match the field against a value
-- {invalid_fields, [field1, field2]} if you match the field against
another field

- You can pass several rules to a fild, just organize them in a list.
If you only need one rule, you don't need a list. If you only need to
check for a field's existence, pass in a tuple that contains only the
field's name:
- {field_name} %% check if the field exists
- {field_name, {'=', field2_name}} %% check if the field is equal to
another field
- {field_name, {'=', Value}} %% check if the field is equal to a value
- {field_name, F} %% pass in a callback
You can pass '=', '/=', '<', '=<', '>', '>=' as rules for simple matches


The entire return value of the function is a proplist in the form
[{FieldName, Errors}] where Errorrs = Error | [Error]



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---



Post recived from mailinglist
View user's profile Send private message
Guest
Posted: Mon May 05, 2008 3:15 am Reply with quote
Guest
On Tue, Apr 22, 2008 at 4:04 AM, Dmitrii Dimandt <dmitriid@gmail.com> wrote:
> Let's say that you have these fields in your form:
> - login
> - password
> - repeat_password
> - repeat_password_once_more (see the example below)
>
> You need to see if login exists, if passwords exist and that they match.
> Validation functions provided by erlyweb can't check if two fields match or
> not.

Sorry for the belated reply. I recently got back from vacation and
haven't been on top of my emails.

I think it's just as easy to do this test outside of the validation
function. This is what I've been doing:

{[Username, Password, Password1...], Errs} =
erlyweb_forms:validate(A, ["username", "password", "password1"],
fun(Field, Val) -> ... end),

Errs1 = if Password =/= Password1 ->
[password_match_error | Errs];
true ->
Errs
end,

...

Yariv



>
> I've started working on a more generic function that would allow passing
> several rules to validate a field, several functions etc.
>
> I'm not too good at programming *blush* but here's what the function does so
> far:
>
> validate(A, [{password, {'=', password_repeat}}])
> %% [] if everything is ok
> %% [[password, {not_equal, password_repeat}]]
>
> % --------------------------------
> % --------------------------------
>
>
> validate(A, [{password, [{'=', password_repeat},
> {'=', password_repeat_once_more}]
> }])
> %% [] if everything is ok
> %% [[password, [{not_equal, password_repeat},
> {not_equal, password_repeat_once_more}]]]
> % --------------------------------
> % --------------------------------
>
>
> validate(A, [{password, [{'=', password_repeat},
> {'=', password_repeat_once_more}]
> }, {login}])
>
> %% [] if everything is ok
> %% [[password, [{not_equal, password_repeat},
> {not_equal, password_repeat_once_more}]],
> [login, absent]
> ]
>
>
> What I want t achieve in the end is allowing passing such constructs as
> rules:
>
> - Functions
> validate{A, [{FieldName, module:function/2}])
> %% where function accepts A and FieldName
> - Function and value to check against
> validate(A, [{FieldName, {module:function/3, Value}}])
> %% where function accepts A, FieldName and Value
> - A slew of operators such as =, =/=, <, <=, >, >= to check against other
> fields or values
> validate(A, [{FieldName, {'=', FieldName2}}])
> validate(A, [{FieldName, {'=', "some value"}}])
>
> I'm attaching whatever I have so far to this message. It's a horrible,
> horrible mess Smile So far you can only validate against to rules:
> validate(A, [{FieldName}]) %% if a field exists
> validate(A, [{FieldName, {'=', FieldName2}}]) %% if a field equals some
> other field
>
> Of course, you re not limited to the number of fields or the number of rules
> that are being passed in:
> validate(A, [
> {FieldName1},
> {FieldName2, {'=', FieldName3}},
> {FieldName4, [
> {'=', FieldName3},
> {'=', FieldName4},
> {'=', FieldName5}
> ]},
> ]
> )
>
>
> I'll be glad to hear your input on this
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post received from mailinglist
dmitriid
Posted: Mon May 05, 2008 9:13 am Reply with quote
User Joined: 17 Aug 2006 Posts: 213
I just wanted a more declarative sort of approach. Smile I don't know if
I'll use it myself though Smile



On May 5, 2008, at 6:14 AM, Yariv Sadan wrote:

>
> On Tue, Apr 22, 2008 at 4:04 AM, Dmitrii Dimandt
> <dmitriid@gmail.com> wrote:
>> Let's say that you have these fields in your form:
>> - login
>> - password
>> - repeat_password
>> - repeat_password_once_more (see the example below)
>>
>> You need to see if login exists, if passwords exist and that they
>> match.
>> Validation functions provided by erlyweb can't check if two fields
>> match or
>> not.
>
> Sorry for the belated reply. I recently got back from vacation and
> haven't been on top of my emails.
>
> I think it's just as easy to do this test outside of the validation
> function. This is what I've been doing:
>
> {[Username, Password, Password1...], Errs} =
> erlyweb_forms:validate(A, ["username", "password", "password1"],
> fun(Field, Val) -> ... end),
>
> Errs1 = if Password =/= Password1 ->
> [password_match_error | Errs];
> true ->
> Errs
> end,
>
> ...
>
> Yariv
>
>
>
>>
>> I've started working on a more generic function that would allow
>> passing
>> several rules to validate a field, several functions etc.
>>
>> I'm not too good at programming *blush* but here's what the
>> function does so
>> far:
>>
>> validate(A, [{password, {'=', password_repeat}}])
>> %% [] if everything is ok
>> %% [[password, {not_equal, password_repeat}]]
>>
>> % --------------------------------
>> % --------------------------------
>>
>>
>> validate(A, [{password, [{'=', password_repeat},
>> {'=', password_repeat_once_more}]
>> }])
>> %% [] if everything is ok
>> %% [[password, [{not_equal, password_repeat},
>> {not_equal, password_repeat_once_more}]]]
>> % --------------------------------
>> % --------------------------------
>>
>>
>> validate(A, [{password, [{'=', password_repeat},
>> {'=', password_repeat_once_more}]
>> }, {login}])
>>
>> %% [] if everything is ok
>> %% [[password, [{not_equal, password_repeat},
>> {not_equal, password_repeat_once_more}]],
>> [login, absent]
>> ]
>>
>>
>> What I want t achieve in the end is allowing passing such
>> constructs as
>> rules:
>>
>> - Functions
>> validate{A, [{FieldName, module:function/2}])
>> %% where function accepts A and FieldName
>> - Function and value to check against
>> validate(A, [{FieldName, {module:function/3, Value}}])
>> %% where function accepts A, FieldName and Value
>> - A slew of operators such as =, =/=, <, <=, >, >= to check against
>> other
>> fields or values
>> validate(A, [{FieldName, {'=', FieldName2}}])
>> validate(A, [{FieldName, {'=', "some value"}}])
>>
>> I'm attaching whatever I have so far to this message. It's a
>> horrible,
>> horrible mess Smile So far you can only validate against to rules:
>> validate(A, [{FieldName}]) %% if a field exists
>> validate(A, [{FieldName, {'=', FieldName2}}]) %% if a field equals
>> some
>> other field
>>
>> Of course, you re not limited to the number of fields or the number
>> of rules
>> that are being passed in:
>> validate(A, [
>> {FieldName1},
>> {FieldName2, {'=', FieldName3}},
>> {FieldName4, [
>> {'=', FieldName3},
>> {'=', FieldName4},
>> {'=', FieldName5}
>> ]},
>> ]
>> )
>>
>>
>> I'll be glad to hear your input on this
>>
>>
>>>
>>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post received from mailinglist
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum