I think the class "org.apache.struts2.validators.DWRValidator" will not works 
so I try another way. I found a solution, maybe we can use it?

I found a good example using dwr here:
https://www.jitendrazaa.com/blog/java/jsp/step-by-step-dwr-application-simple-ajax-in-java/

What is to do:
- for using client validation we need the theme "xhtml" or "css_xhtml" (not 
"simple")
- and the form has to us validation, example "<s:form action="test" 
validate="true" theme="css_xhtml" >" 
- we need drw-1.1.1.jar and struts2-dwr-plugin-2.5.26.jar (that's standard)
- I create a new class "IdnToAscii" with the method "getIdnToAscii" (I propose 
to use the class "org.apache.struts2.validators.DWRValidator" for this)

import java.net.IDN;

public class IdnToAscii
{
  public IdnToAscii() {}

  public String getIdnToAscii(String psUrl)
  {
    if (psUrl == null)
    {
      psUrl = "";
    }
    return IDN.toASCII(psUrl);
  }
}

- create or change in the WEB-INF-folder the file dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC 
        "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" 
        "http://www.getahead.ltd.uk/dwr/dwr10.dtd";>

<dwr>
    <allow>
        <create creator="new" javascript="validator">
            <param name="class" 
value="org.apache.struts2.validators.DWRValidator"/>
        </create>
        <create creator="new" javascript="IdnToAscii">
            <param name="class" value="de.guenterpaul.ajax.dwr.IdnToAscii"/>
        </create>
        <convert converter="bean" 
match="com.opensymphony.xwork2.ValidationAwareSupport"/>
    </allow>

    <signatures>
        <![CDATA[
        import java.util.Map;
        import org.apache.struts2.validators.DWRValidator;

        DWRValidator.doPost(String, String, Map<String, String>);
        ]]>
    </signatures>
</dwr>

- I use the "css_xhtml" theme
- I change the file "resources/template/css_xhtml/form-validate.ftl"

<#if parameters.validate!false == true>
    <script type='text/javascript' 
src='${base}/dwr/interface/IdnToAscii.js'></script>
    <script type='text/javascript' src='${base}/dwr/engine.js'></script>

    <script type="text/javascript" 
src="${base}/struts/css_xhtml/validation.js"></script>
    <script type="text/javascript" src="${base}/struts/utils.js"></script>
    <#if parameters.onsubmit??>
        ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return 
validateForm_${parameters.id?replace('[^a-zA-Z0-9_]', '_', 'r')}();")}
    <#else>
        ${tag.addParameter('onsubmit', "return 
validateForm_${parameters.id?replace('[^a-zA-Z0-9_]', '_', 'r')}();")}
    </#if>
</#if>

- I copy the file "resources/template/xhtml/form-close-validate.ftl" int the 
"css_xhtml" folder and change the url-validator:

                <#elseif validator.validatorType = "url">
                if (continueValidation && fieldValue !== null && 
fieldValue.length > 0)
                {
                    DWREngine.setAsync(false);
                    IdnToAscii.getIdnToAscii(fieldValue, function(idnurl) {
                        fieldValue = idnurl;
                    });
                    alert("URL >" + fieldValue + "<");
                    if (continueValidation && fieldValue !== null && 
fieldValue.length > 0 && fieldValue.match(/${validator.urlRegex}/i) === null)
                    {
                        addError(field, error);
                        errors = true;
                        <#if validator.shortCircuit>continueValidation = 
false;</#if>
                    }
                }

That's all. It's important to use DWR in sync-mode. We only convert the 
field-value to IDN (using JAVA at the server) and after we check the changed 
value.

We can use the same way by email-validation. And xhtml-theme is the same way, I 
think (I can't use it in my project, so I don't test it here).

Hope it's useful.



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to