RE: struts validator

"Louis Moore" <[email protected]> Mon, 7 Jun 2004 14:27:38 -0700
Newsgroups gmane.comp.java.keel.user
Message-ID <C5E6D04B7C014946B7CCBFC2AF30DB62C096B4@corpmail01.corp.walmart.com>
The generated xml is below, it looks correct to me:

<testeng.getcase
   id="testeng.getcase"
   activation="request"
   logger="testeng">
     <parameter name="caseid"
       required="true"
          type="java.lang.String"
          pattern="CASE[0-9]{8}"
      />

      <validation-error
          model="testeng.prompt-getcase"
          params="true"
      />

     <attribute name="forward"
         value="testeng.getcase"/>     

</testeng.getcase>

Here's the validate function from DefaultModelValidator.java with my
changes (the boolean valid was added and used to determine if the
redirect should happen, the return of the redirect command was moved
outside the param loop, and I also corrected a typo in the
ConfigurationException thrown near the end):

    public Command validate(ModelRequest req, Model newModel) throws
ConfigurationException, ModelException {

        Configuration conf = newModel.getConfiguration();
        /* If there's no config, we can't possibly have any parameter
config */
        if (conf == null) {
            log.debug("No configuration - nothing to validate");
            return null;
        }

        if (log.isDebugEnabled()) {
            log.debug("Validating model '" + req.getModel() + "'");
        }

        if (!convertersRegistered) {
            registerConverters();
        }
        
        String name = null;
        Object value = null;
        String defaultValue = null;
        String pattern = null;
        String type = null;
        boolean required = false;
        boolean valid = true;
        
        Configuration[] paramConfigs = conf.getChildren("parameter");
        for (int i = 0; i < paramConfigs.length; i++) {

            Configuration oneParamConfig = paramConfigs[i];
            name = oneParamConfig.getAttribute("name");

            if (log.isDebugEnabled()) {
                log.debug("Validating field '" + name + "'");
            }

            required = oneParamConfig.getAttributeAsBoolean("required",
false);
            value = req.getParameter(name);
            if (required && (value == null)) {
                valid = false;
                req.addError(name, "'" + name + "' is required");
            }
            defaultValue = oneParamConfig.getAttribute("default", null);
            type = oneParamConfig.getAttribute("type", null);

            if ((value == null) && (defaultValue != null)) {
                req.setParameter(name, defaultValue);
            }

            if ((value != null) && (type != null)) {
                /* verify the value can be converted to the specified
type */
                if (!type.equals("java.lang.String")) {
                    Class clazz = null;
                    try {
                        clazz = Class.forName(type);
                    } catch (ClassNotFoundException ce) {
                        throw new ConfigurationException("No class found
for '" + type + "', specified as type for parameter '" + name + "'");
                    }

                    if (type.equalsIgnoreCase("java.util.Date")) {
                        try {
                            new SuperString(value.toString()).toDate();
                        } catch (Exception e) {
                            valid = false;
                            log.error("Value '" + value + " cannot be
converted to a date", e);
                            req.addError(name, "Value '" + value + "'
cannot be converted to a date");
                        }
                    } else {
                        Converter c = ConvertUtils.lookup(clazz);
                        if (c == null) {
                            log.error("No converter found for class '" +
type + "'");
                            throw new IllegalArgumentException("No
converter found for class '" + type + "'");
                        }
                        try {
                            c.convert(clazz, value);
                        } catch (ConversionException ce) {
                            valid = false;
                            req.addError(name, "Value '" + value + "'
cannot be converted to a '" + type + "'");
                        }
                    }
                }
            }
            pattern = oneParamConfig.getAttribute("pattern", null);
            if ((value != null) && (pattern != null)) {
                if (!value.toString().matches(pattern)) {
                    valid = false;
                    req.addError(name, "Value '" + value + "' does not
match validation pattern '" + pattern + "'");
                }
            }
        }

        if (conf.getChild("validation-error", false) != null && !valid)
{
            Configuration validationErrorConfig =
conf.getChild("validation-error");
            Command redirect =
req.createResponse().createCommand(validationErrorConfig.getAttribute("m
odel"));
            /* Catch dumb mistake... :-) */
            if
(validationErrorConfig.getAttribute("model").equals(req.getModel())) { 
                throw new ConfigurationException("Cannot specify model
'" + validationErrorConfig.getAttribute("model")
                        + "' to have itself as the validation-error
model!"); 
            }
            if (validationErrorConfig.getAttributeAsBoolean("params",
true)) {
                String oneParamName = null;
                for (Iterator i =
req.getParameters().keySet().iterator(); i.hasNext();) {
                    oneParamName = (String) i.next();
                    redirect.setParameter(oneParamName,
req.getParameter(oneParamName));
                }
            }
            return redirect;
        }
        return null;
    }

Please let me know when the message fix is in as well. Thanks!

-Lou

-----Original Message-----
From: Michael Nash JGlobal.com [mailto:[email protected]] 
Sent: Monday, June 07, 2004 1:37 PM
To: user-6VIttnCrOeJXNEnpj1eHPNi2O/[email protected]
Cc: Louis Moore
Subject: Re: [Keel User] struts validator

Lou:

Ah, sounds like you found a couple of interesting bugs there - can you
see if the XML being generated from the xdoclet below looks right? I
think I've found the problem where the errors we'ren't being displayed,
I'll be checking in a patch momentarily.

And yes, a patch on the bugs below would definitely be appreciated! 

Thanks!

Mike

On Mon, 7 Jun 2004 12:45:03 -0700
"Louis Moore" <[email protected]> wrote:

> Thanks -- so I got started working with the DefaultModelValidator,
> here's the metadata for a model I have using it:
> 
> @model.model
>   name="testeng.getcase"
>   id="testeng.getcase"
>   logger="testeng"
> @model.parameter
>   name="caseid"
>   required="true"
>   type="java.lang.String"
>   pattern="CASE[0-9]{8}"
> @model.validation-error
>   model="testeng.prompt-getcase"
>   params="true"
> 
> And I got it to work but I found a couple of bugs along the way.
(These
> are in both 2.1-dev and CVS HEAD):
> 
> 1) The redirect to the validation-error model code is always executed.
> (meaning even if your input is valid you get redirected) 
> 2) But if validation-error params=false, the redirect command is not
> returned. (meaning if you have params=false the redirect never happens
> at all)
> 
> I was able to fix both of these in my code and it's working fine. I'd
be
> happy to submit a patch if you want. The only problem I'm still having
> is the errors aren't being displayed after redirect when the pattern
> wasn't matched (using the keel:errors tag). I'd appreciate any ideas
on
> that.
> 
> Regards,
> Lou
>  
> 
> -----Original Message-----
> From: Michael Nash JGlobal.com [mailto:[email protected]] 
> Sent: Saturday, June 05, 2004 11:05 AM
> To: user-6VIttnCrOeJXNEnpj1eHPNi2O/[email protected]
> Cc: Louis Moore
> Subject: Re: [Keel User] struts validator
> 
> Lou:
> 
> Just wanted to let you know there's now a start on documentation for
the
> Validation service. Have a look at the Keel manual (HEAD version), in
> the "Default Model Service" chapter. It needs more, but I think
there's
> enough to get the general idea!
> 
> Mike
> 
> On Thu, 3 Jun 2004 10:51:44 -0700
> "Louis Moore" <[email protected]> wrote:
> 
> > Hi,
> > 
> > Is there any documentation or examples of how to use the struts
> > validator framework within keel? Since Keel has only one action
> mapping
> > and form-bean for the model action it seems like maybe the validator
> > framework isn't a viable option unless I abandon the use of models
and
> > write my own struts actions. Is this the case or is there a way to
> > configure the validator to work with keel models?
> > 
> > If the struts validator plug-in isn't supported well within keel I'm
> > considering creating a keel validation service with the
> turbine-fulcrum
> > project's intake service. 
> > 
> > Thanks,
> > Lou
> > _______________________________________________
> > User mailing list
> > [email protected]
> > http://lists.keelframework.org/listinfo.cgi/user-keelframework.org
> 
> 
> -- 
> Michael Nash
> 
> JGlobal Ltd 
> Next-Generation Web Application Development and Open Source Support
> http://www.jglobal.com
> 
> Bahamas Commerce and Trade
> Offshore eCommerce Hosting and Business Services
> http://www.bahamascommerce.com
> 
> 
> _______________________________________________
> User mailing list
> [email protected]
> http://lists.keelframework.org/listinfo.cgi/user-keelframework.org


-- 
Michael Nash

JGlobal Ltd 
Next-Generation Web Application Development and Open Source Support
http://www.jglobal.com

Bahamas Commerce and Trade
Offshore eCommerce Hosting and Business Services
http://www.bahamascommerce.com