[Opensource] ActionForm in Controllers

larry hamel expresso at codeguild.com
Fri Mar 14 16:01:07 PST 2003


this could be off topic--I'm not sure if you're talking about parameters, but

            request.validateDBObject(object, errorCollection);
 
will populate an object from parameters.

I supplement this with the following, which allows specification of required fields. it's kind of clunky... what's better?

lemme know if you want this in Controller.java

larry
-------------------------

    /**
     * populates DBObject with any parameters which match field names
     * will forward any validation errors to failureState
     * @return true if NO errors exist
     */
    public boolean isValidAndPopulated(DBObject object,
                                       String[] requiredFields,
                                       String failureState,
                                       ControllerRequest request,
                                       ControllerResponse response)
                                throws ControllerException {
        ErrorCollection errorCollection = new ErrorCollection();

        try {
            request.validateDBObject(object, errorCollection);
        } catch (ValidationException e) {
            errorCollection.addError(e.getMessage());
        }

        // test for presence of required fields
        try {
            for (int i = 0;
                 requiredFields != null && i < requiredFields.length;
                 i++) {
                String value = object.getField(requiredFields[i]);

                if (value == null || value.length() == 0) {

                    // create error message
                    String friendlyFieldName = null;
                    try {
                        friendlyFieldName = object.getDescription(requiredFields[i]);
                    } catch ( Throwable e ) {
                        /**
                         * @todo ignore internationalization string missing errors for now; add later
                         */
                    }

                    if (friendlyFieldName == null || friendlyFieldName.length() == 0) {
                        friendlyFieldName = requiredFields[i];
                    }

                    errorCollection.addError("The " + friendlyFieldName + " field is required. Please complete this field.");
                }
            }
        } catch (DBException e) {
            throw new ControllerException(e);
        }

        if (errorCollection.size() > 0) {
            response.saveErrors(errorCollection);
            getLogger().debug("There were errors. Redirecting to " + failureState);

            try {
                response.setFormCache();
                transition(failureState, request, response);
            } catch (NonHandleableException e) {
                throw new ControllerException(e);
            }
        }

        return errorCollection.size() <= 0;
    }


At 10:39 AM 3/14/2003, Michael Rimov wrote:
>At 10:46 AM 3/14/2003 -0300, you wrote:
>>> If so, is there a way to automatically setup DBObject's fields with the values of the ActionForm?
>>Hmm, don't know about this. Anyone??
>
>Expresso doesn't have anything directly like that yet.  The closest is for rendering DBObjects to Inputs and Blocks.
>
>Theoretically, you could do it with BeanUtils.setProperty() calls, setting the values for each corresponding field in the DBObject.
>
>HTH,
>
>                                                -Mike 
>
>_______________________________________________
>Opensource mailing list
>Opensource at jcorporate.com
>http://mail.jcorporate.com/mailman/listinfo/opensource
>Archives: http://mail.jcorporate.com/pipermail/opensource/




More information about the Opensource mailing list