Jasper

Handling forms with tri-state return values

In the event that the result of a form submission leads to different pages being displayed dependant upon the success or otherwise of the form's action, a boolean return value is not sufficient to cover all possible cases, the form not having been used at all providing a third possible outcome.

The easiest way to deal with these situations is to return a string or integer instead of the boolean value from the preprocess(...) method of the class implementing the form in question. The example used here is that of the feedback form which forms part of the Jasper distribution, after it has been amended as laid out in the tutorial on preprocessing forms, a link to which is provided in the related links section at the foot of this page.

The pseudo-code listing below shows the changes necessary to the FeedbackForm class and in particular its preprocess(...) method. A string return value is used here but this is not mandatory. An integer value will do just as well. So, the listing:

class FeedbackForm
{
    string SUCCESS = "success";
    string FAILURE = "failure";
    string PASSIVE = "passive";
    
    function preprocess ( props )
    {
        if ( ArrayUtils.keyExistsAndValueEquals( props, "FORM.command", "feedback_submit" ) )
        {
            if ( Feedback.validate( props ) )
            {
                Feedback.send( props );

                return ( FeedbackForm.SUCCESS );
            }
            else
            {
                return ( FeedbackForm.FAILURE );
            }
        }
        else
        {
            return ( FeedbackForm.PASSIVE );
        }
    }

    ....
}

Note that in all cases the preprocess(...) method returns a value and in particular, when the 'command' form variable has not been set to the relevant value or not set at all, the preprocess(...) method signifies that it has taken no action. The easiest way to handle these return values at the preprocess stage is to create a private method in the PreProcess class and invoke it directly from the constructor:

class PreProcess
{
    function PreProcess ( array props )
    {
        PreProcess.FeedbackForm( props );

        ....
    }
    
    function FeedbackForm ( array props )
    {
        string feedbackform_preprocess = FeedbackForm.preprocess( props );
        
    	if ( StringUtils.equals( feedbackform_preprocess, FeedbackForm.SUCCESS ) )
    	{
    	    props{ "FORM.page" } = "feedback_successful";
    	}

    	if ( StringUtils.equals( feedbackform_preprocess, FeedbackForm.FAILURE ) )
    	{
    	    props{ "FORM.page" } = "feedback_failed";
    	}
    }

    ....
}

Note that the passive return value is not used here, its utility being implicit. Since the purpose of preprocessing in this instance is to change the overall page displayed in response to the feedback form, no action need be taken if the form has not been used.

Related links

Last updated: Sunday, 23rd October 2011, 07:17 PM

User comments:

There are none.

Leave a comment:

Display name Email address Content
Enable HTML content
PreviewSubmit