Search code examples
jqueryattributesjquery-validatestrutsstruts-1

Using Struts html tag, render attribute not present in TLD


I'm trying to validate Struts 1.3 - made forms with jQuery Validator plugin. Even though Struts 1.x has reached it's end-of-life, I'm not allowed to switch to another MVC framework.

jQuery validator uses attributes on <input> tag to determine which validation rules to apply. For example:

<input type="text" minlenght="30" required />

will check if the textbox was filled in and it's contents length is at least of 30.

I'm trying to achieve the same with <html:text>, which is supposed to render as an <input type="text"> element. But minlength and required are not defined in struts-html.tld as attributes, causing the JSP compiler to throw the following (translated from Spanish) exception:

org.apache.jasper.JasperException: /WEB-INF/path-to-my-jsp Attribute xxx invalid for tag xxx according to TLD

According to this question it is possible, despite the fact that Struts is used to generate the form. But I couldn't make it work.

Is there any way to force the JSP compiler to ignore those undefined attributes and make Struts render them "as-is"?

Note: I've chosen jQuery Validator due to it's ease of use, but I'm open to suggestions on other methods to perform the same task. However, I still want to know if it's possible to make the JSP compiler to ignore an invalid / undefined attribute.

P.S. Please excuse my english. I've tried my best.


Solution

  • Quote OP:

    "jQuery validator uses attributes on tag to determine which validation rules to apply."

    <input type="text" minlenght="30" required />
    

    You do not have to declare rules using HTML5 attributes.

    You could instead declare your rules from within the .validate() method:

    $(document).ready(function() {
    
        $('#myform').validate({
            rules: {
                fieldname: {  // <- field name attribute
                    required: true,
                    minlength: 30
                }
            }
        });
    
    });
    

    Corresponding HTML:

    <input type="text" name="fieldname" />
    

    NOTE: the jQuery Validate plugin mandates that the input field contains a unique name attribute no matter how the rules are applied. The name attribute is how the plugin keeps track of the inputs. If you fail to assign a unique name attribute, the plugin will behave unpredictably or not at all.


    I just noticed that you've misspelled the minlength rule as minlenght. Perhaps this is the root cause of your compilation error?