Paste: changed validation-failed?

Author: middayc
Mode: factor
Date: Mon, 29 Dec 2008 00:51:03
Plain Text |
! I was thinking how to make it to alow custom behaviour 
! scenarious besides default one, comments welcome

SYMBOL: on-validation-fail                       ! line added

: validation-failed ( -- * )
    on-validation-fail get dup [ call ] [ drop   ! line added
        post-request? revalidate-url and [
            begin-conversation
            nested-forms-key param " " split harvest nested-forms cset
            form get form cset
            <continue-conversation>
        ] [ <400> ] if*
    ] if exit-with ;                             ! ] if added

! = EXAMPLE USAGE =
! instead of doing this for normal behaviour in your webapp:

... create-validation-rules validate-params ...

! you do this to get JSON object (hashtable) with error 
! messages per form field back

... create-validation-rules validate-with-json ...

! and before that you define 

: ?get-validation-error-message ( val -- msg? )
    dup class \ validation-error eq? [ message>> ] [ drop f ] if ;

: get-validation-errors ( form -- errors )
    values>> [ ?get-validation-error-message ] assoc-map ;

: validate-with-json ( validators -- )
    [ [ form get get-validation-errors >json <content-json> ] on-validation-fail set 
        validate-params ] with-scope ;

! as I said any comments welcome

New Annotation

Summary:
Author:
Mode:
Body: