Pergunta

I used ajax validation to validate the following

   <tr>
      <td width="20" style='color: red'>
         <img src="images/icon-star.png" width="16" height="16" />
      </td>
      <td id="lblCustomBillNo" style="width: 15%" class="searchCriteriaCellLbl">
         The custom Bill Number
      </td>
      <td width="5" class="searchCriteriaCellLbl">:</td>
      <td class="searchCriteriaCellVal">
          <s:textfield id="customBillNo" name="customBillNo" size="20" maxlength="24" style="width: 200px" />
      </td>
      <td class="errorFlag" style="color: red" valign="middle">
          <s:fielderror fieldName="customBillNo" />
      </td>
  </tr>
  <tr>
      <td width="20" style='color: red'>
        <img src="images/icon-star.png" width="16" height="16" />
      </td>
      <td id="lblBillNo" style="width: 15%" class="searchCriteriaCellLbl">
         <s:property value="%{getText('billNo')}" />
      </td>
      <td width="5" class="searchCriteriaCellLbl">:
      </td>
      <td class="searchCriteriaCellVal">
          <s:textfield label="billNo" id="billNo" name="billNo" size="20" maxlength="24" style="width: 200px" />
      </td>
      <td class="errorFlag" style="color: red" valign="middle">
          <s:fielderror fieldName="billNo" />
      </td>
  </tr>
  <tr>
      <td width="20" style='color: red'>
        <img src="images/icon-star.png" width="16" height="16" />
      </td>
     <td id="lblCarrierNo" style="width: 15%" class="searchCriteriaCellLbl">
        The carrier Number
     </td>
     <td width="5" class="searchCriteriaCellLbl">:
     </td>
     <td class="searchCriteriaCellVal">
        <s:textfield label="carrierNo" id="carrierNo" name="carrierNo" size="20" maxlength="24" style="width: 200px" />
     </td>
     <td class="errorFlag" style="color: red" valign="middle">
        <s:fielderror fieldName="carrierNo" />
     </td>
 </tr>

I use the following internationalization for errors in golbal i18n file

errors.required=${getText(fieldName)} requireddd

and this validation file

 <validators>      
     <field name="customBillNo">
        <field-validator type="requiredstring" short-circuit="true">
           <param name="trim">true</param>
           <message key="errors.required" />
        </field-validator>
     </field>
     <field name="billNo">
      <field-validator type="required" short-circuit="true">
        <message key="errors.required" />
       </field-validator>
     </field>
     <field name="carrierNo">
       <field-validator type="required" short-circuit="true">
          <message key="errors.required" />
       </field-validator>
     </field>
   </validators>

and i put this javascript to use ajax validation

      function validate(){

            //document.all.loading.style.display = 'block';

            var searchUrl = 'AddEnteringApproval_approval';

            var params = '';

            var elemArray = document.mainForm.elements;
            for (var i = 0; i < elemArray.length;i++)
            {
                var element = elemArray[i];

                var elementName= element.name;
                if(elementName=='formAction')
                    continue;
                params += '&' + elementName+'='+ encodeURIComponent(element.value);
            }

            params += '&struts.enableJSONValidation=true&struts.validateOnly=true';

            createXmlHttpObject(); // this is my function that prepare ajax

            sendRequestPost(http_request,searchUrl,false,params);

            postValidation();

    }

    function postValidation() {
       var form = $('#mainForm');

       var text = http_request.responseText;

       //clear previous validation errors, if any
       StrutsUtils.clearValidationErrors(form);
       alert(text)
       //get errors from response
       //var text = request.responseText;
       var errorsObject = StrutsUtils.getValidationErrors(text);

       //show errors, if any
       if(errorsObject.fieldErrors)
       {
         StrutsUtils.showValidationErrors(form, errorsObject);
       }
       else
       {
         //good to go, regular submit
         form.submit();
       }
    }


       /*  This is one of the functions that doesn't work using the simple theme, so I redefined it.
        This can be changed to clear the previous errors, as it does in the commented example
        cleaning the errors on divErrors.
        As I am just showing the messages with alerts I don't need to clear anything,
        but the method still need to be redefined, even if it is empty.
     */

   StrutsUtils.clearValidationErrors = function(form, errors) {
        //clear the div errors
        //$('#divErrors').empty();
    }


    /* This method is responsible to show the messages.
       The original versions works with the xhrml and css-xhtml theme but doesn't work with the simple theme
       so I override the method with another implementation that shows the messages using alerts.
       You can change the implementation to show the messages as you want,
       but as the previous method this has to be redefined.
     */

    StrutsUtils.showValidationErrors = function(form, errors) {

        if(errors.fieldErrors)
        {alert((errors.fieldErrors))
             for(var fieldName in errors.fieldErrors)
             { 
                 alert("errors.fieldErrors[fieldName]   " + errors.fieldErrors[fieldName]);

               for(var i = 0; i < errors.fieldErrors[fieldName].length; i++)
               {
                   alert('Field ->' + fieldName + '\nError -> ' + errors.fieldErrors[fieldName][i]);
               }
             }
        }
    };

but when i execute the code i get no organized JSON text i showed in alert message box, the field name is not like the one in the error message, the second field name is missing, the third field name is cut (i.e. carrierNo becomes rNo ).

can you help me. i want the field name in the JSON error match the error message text

I just figured out what is the problem, but i don't know why it happens and why. it always remove the first 6 characters. why this happens

enter image description here

Foi útil?

Solução

well I figured out the problem. it was a but in org.apache.struts2.interceptor.validation.JSONValidationInterceptor

it removes the 6 first characters because of this incomplete if statement

sb.append((validationAware instanceof ModelDriven) ? ((String)fieldError.getKey()).substring(6) : (String)fieldError.getKey());

this error is fuond in struts 2.1.8

it should be like this

sb.append(((validationAware instanceof ModelDriven)) && (fieldErrorKey.startsWith("model.")) ? fieldErrorKey.substring(6) : fieldErrorKey);

it was corrected in later struts releses. i corrected the problem. and i thought i have to share the information for people who faces the problem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top