footh wrote:
I know I probably could find the answer to this
question by going through docs and samples, but I've
been through that so much in the past couple weeks
that I was hoping I could play my "get out of jail
free" card.

I have a form that registers a user.  Field validation
can be done easily on the client side.  But, in the
case that I must check whether the user tries to
register a name that already exists, I need to hit my
user database and then return to the form if an error
occurs.

Basically, I bind my form to a bean, call form.load,
call form.show to show the form, call form.save to
load the request data into the bean, then check if the
user already exists.

1. You do not have to call form.save( bean ) and do validation after. What I do is to plug additional javascript validator and validate the data BEFORE I do form.save:

<fd:form     xmlns:fd="http://apache.org/cocoon/forms/1.0#definition";
                        xmlns:i18n="http://apache.org/cocoon/i18n/2.1";>
        <fd:validation>
                <fd:javascript>
                        return loginValidator( widget );
                </fd:javascript>
        </fd:validation>
        <fd:widgets>
                <fd:messages id="messages">
                        <fd:label>messages</fd:label>
                </fd:messages>
                <fd:field id="username" required="true">
                        <fd:datatype base="string"/>
                        <fd:label>Username</fd:label>
                </fd:field>
                <fd:field id="password" required="true">
                        <fd:datatype base="string"/>
                        <fd:label>Password</fd:label>
                </fd:field>
                <fd:booleanfield id="remember" required="false">
                        <fd:label>rememberme</fd:label>
                </fd:booleanfield>
                <fd:submit id="finish" command="finish" validate="true">
                        <fd:label>Log in</fd:label>
                </fd:submit>
        </fd:widgets>
</fd:form>

you can have your business logic called directly in <fd:javascript> but I consider it good practice to call a flowscript function here. As you see you can access "widget" variable which in this case is the whole form.

function loginValidator( form ) {
        var username = form.getChild( "username" ).getValue();
        var pass = form.getChild( "password" ).getValue();
        
        var user = validateLoginData( username, pass );
        if ( user == null ) {
                form.getChild( "messages" ).addMessage( "Bad credentials!" );
                return false;
        }
        
        cocoon.session.setAttribute( "user", user );
        
        if ( form.getChild( "rememberme" ).getValue() == java.lang.Boolean.TRUE 
) {
                storeLoginCookies( user.username, user.password );
        }
        return true;
}

Your javascript validator should return false if validation failed. Any additional messages are registered at "messages" widget. Please mind that widget is not "persistent" - you do not have to clear the messages, they will be shown only once.


If the user does already exist, then I obviously need
to start over again and display that error (and
display the fields they already entered).

Might anyone be able to point me in the right
direction of how to accomplish this with CForms?  Do I
simply call form.show again?  How do I insert this
error into the validation errors framework?

If you want more fine grained error display you can always do:

form.getChild( "username" ).setValidationError(
    new ValidationError( "Username already taken." ) );

or even better:
form.getChild( "username" ).setValidationError(
    new ValidationError( "username.taken", true ) );
for i18n-enabled form.

HTH
--
Leszek Gawron                                      [EMAIL PROTECTED]
IT Manager                                         MobileBox sp. z o.o.
+48 (61) 855 06 67                              http://www.mobilebox.pl
mobile: +48 (501) 720 812                       fax: +48 (61) 853 29 65

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to