Validação

Olá galera.

Alguém poderia me ajudar com a validação de campos nos formulários usando struts? Eu já coloquei no validation.xml

<field property="nome" depends="required" ><arg0 key="prompt.nome"/></field>

No arquivo validator-rules.xml já tem a tag validator:

<validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionErrors,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required">

e no arquivo properties tem a linha:

errors.required={0} é um campo obrigatório.

no arquivo jsp tem:

<td align="right" ><bean:message key="prompt.pensionista"/></td>
<td align="left" colspan="3"><html:text property="nome" name="pensiInserirBusiness" size="30" maxlength="30" styleClass="texto"/></td>

Mas eu ainda faço a validação dentro da classe Action.java. Como eu faço para fazer a validação usando apenas xml e os recursos do framework struts?
Eu sei q tem jeito mas até agora não consegui fazer…

Por favor pessoal, se alguém souber como eu faço isso poderia me dar uma luz? Algum tutorial? Alguma coisa???

Muito obrigado!

cara vc vai fazer o seguinte:

vou te mandar um exemplo com validação no servidor

ja incluiu no config ?

<plug-in className=“org.apache.struts.validator.ValidatorPlugIn”>
<set-property property=“pathnames” value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

feito isso

validation.xml (coloca seu formulario,msg etc)

 &lt;formset&gt;
  &lt;form name="searchForm"&gt;
     &lt;field property="isbn" depends="required"&gt;
       &lt;arg0 key="message.notfound"/&gt;
     &lt;/field&gt;
  &lt;/form&gt;

</formset>

no validation-rules:

   &lt;validator name="required"
        classname="org.apache.struts.validator.FieldChecks"
           method="validateRequired"
     methodParams="java.lang.Object,
                   org.apache.commons.validator.ValidatorAction,
                   org.apache.commons.validator.Field,
                   org.apache.struts.action.ActionMessages,
                   javax.servlet.http.HttpServletRequest"
              msg="errors.required"&gt;
  &lt;/validator&gt;   

use tambem formulario dinamico na definicao:

ex: <form-bean name=“searchForm” type=“org.apache.struts.validator.DynaValidatorForm”>
<form-property name=“isbn” type=“java.lang.String”/>
</form-bean>

do tipo dynavalidatorform pra nao precisar de beans para ler os formularios

e finalmente no seu action:

cata o formulario dinamico :

DynaActionForm searchForm = (DynaActionForm) form;

e faz a validação :

ActionErrors e = searchForm.validate(mapping,request);

ai voce seta a variavel “e” pra mostrar o erro.

nao esqueça de setar o validate no action do struts como “false”

ActionErrors não é deprecated ?!?!

Estou tentando fazer a validação no cliente e está dando esse erro:

09&#58;14&#58;13,615 INFO  &#91;STDOUT&#93; 2006/06/28 09&#58;14&#58;13&#58;613 BRT &#91;ERROR&#93; org.apache.struts.validator.DynaValidatorForm - org.apache.struts.validator.FieldChecks.validateRequired&#40;java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest&#41; &lt;org.apache.commons.validator.ValidatorException&#58; org.apache.struts.validator.FieldChecks.validateRequired&#40;java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest&#41;&gt;org.apache.commons.validator.ValidatorException&#58; org.apache.struts.validator.FieldChecks.validateRequired&#40;java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest&#41;
        at org.apache.commons.validator.ValidatorAction.loadValidationMethod&#40;ValidatorAction.java&#58;627&#41;
        at org.apache.commons.validator.ValidatorAction.executeValidationMethod&#40;ValidatorAction.java&#58;557&#41;
        at org.apache.commons.validator.Field.validateForRule&#40;Field.java&#58;827&#41;
        at org.apache.commons.validator.Field.validate&#40;Field.java&#58;906&#41;
        at org.apache.commons.validator.Form.validate&#40;Form.java&#58;174&#41;
        at org.apache.commons.validator.Validator.validate&#40;Validator.java&#58;367&#41;
        at org.apache.struts.validator.DynaValidatorForm.validate&#40;DynaValidatorForm.java&#58;110&#41;
        at org.apache.struts.action.RequestProcessor.processValidate&#40;RequestProcessor.java&#58;927&#41;
        at org.apache.struts.action.RequestProcessor.process&#40;RequestProcessor.java&#58;204&#41;
        at org.apache.struts.action.ActionServlet.process&#40;ActionServlet.java&#58;1196&#41;
        at org.apache.struts.action.ActionServlet.doGet&#40;ActionServlet.java&#58;414&#41;
        at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;697&#41;
        at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;810&#41;
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
        at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
        at org.apache.catalina.core.ApplicationDispatcher.invoke&#40;ApplicationDispatcher.java&#58;703&#41;
        at org.apache.catalina.core.ApplicationDispatcher.processRequest&#40;ApplicationDispatcher.java&#58;463&#41;
        at org.apache.catalina.core.ApplicationDispatcher.doForward&#40;ApplicationDispatcher.java&#58;398&#41;
        at org.apache.catalina.core.ApplicationDispatcher.forward&#40;ApplicationDispatcher.java&#58;312&#41;
        at org.apache.jasper.runtime.PageContextImpl.doForward&#40;PageContextImpl.java&#58;670&#41;
        at org.apache.jasper.runtime.PageContextImpl.forward&#40;PageContextImpl.java&#58;637&#41;
        at rhmasterweb.servlet.controller.ServletController.doPost&#40;ServletController.java&#58;80&#41;
        at rhmasterweb.servlet.controller.ServletController.doGet&#40;ServletController.java&#58;27&#41;
        at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;697&#41;
        at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;810&#41;
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;237&#41;
        at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
        at org.apache.catalina.core.ApplicationDispatcher.invoke&#40;ApplicationDispatcher.java&#58;703&#41;
        at org.apache.catalina.core.ApplicationDispatcher.processRequest&#40;ApplicationDispatcher.java&#58;463&#41;
        at org.apache.catalina.core.ApplicationDispatcher.doForward&#40;ApplicationDispatcher.java&#58;398&#41;
        at org.apache.catalina.core.ApplicationDispatcher.forward&#40;ApplicationDispatcher.java&#58;312&#41;
        at rhmasterweb.filter.verify.VerifyFilter.doFilter&#40;VerifyFilter.java&#58;100&#41;
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter&#40;ApplicationFilterChain.java&#58;186&#41;
        at org.apache.catalina.core.ApplicationFilterChain.doFilter&#40;ApplicationFilterChain.java&#58;157&#41;
        at org.apache.catalina.core.StandardWrapperValve.invoke&#40;StandardWrapperValve.java&#58;214&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
        at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
        at org.apache.catalina.core.StandardContextValve.invokeInternal&#40;StandardContextValve.java&#58;198&#41;
        at org.apache.catalina.core.StandardContextValve.invoke&#40;StandardContextValve.java&#58;152&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke&#40;SecurityAssociationValve.java&#58;72&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
        at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke&#40;JBossSecurityMgrRealm.java&#58;275&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke&#40;AuthenticatorBase.java&#58;540&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
        at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
        at org.apache.catalina.core.StandardHostValve.invoke&#40;StandardHostValve.java&#58;137&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
        at org.apache.catalina.valves.ErrorReportValve.invoke&#40;ErrorReportValve.java&#58;117&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;102&#41;
        at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
        at org.apache.catalina.core.StandardEngineValve.invoke&#40;StandardEngineValve.java&#58;109&#41;
        at org.apache.catalina.core.StandardValveContext.invokeNext&#40;StandardValveContext.java&#58;104&#41;
        at org.apache.catalina.core.StandardPipeline.invoke&#40;StandardPipeline.java&#58;520&#41;
        at org.apache.catalina.core.ContainerBase.invoke&#40;ContainerBase.java&#58;929&#41;
        at org.apache.coyote.tomcat5.CoyoteAdapter.service&#40;CoyoteAdapter.java&#58;160&#41;
        at org.apache.coyote.http11.Http11Processor.process&#40;Http11Processor.java&#58;799&#41;
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection&#40;Http11Protocol.java&#58;705&#41;
        at org.apache.tomcat.util.net.TcpWorkerThread.runIt&#40;PoolTcpEndpoint.java&#58;577&#41;
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run&#40;ThreadPool.java&#58;683&#41;
        at java.lang.Thread.run&#40;Thread.java&#58;534&#41;

Oi pessoal, alguém pode me ajudar??? Preciso fazer essa validação no cliente.

Estou aprendendo um pouco ainda de struts, a tag

&lt;html&#58;javascript formName=&quot;candi_form&quot; /&gt; 

é para fazer a validação no cliente não é? Então… parece que ela não está tendo efeito nenhum na minha página, pois eu tiro ela e coloco e não muda em nada…

O q será? Ele não está lendo meu validation.xml? Mas ele funciona numa boa…

Alguém me ajuda, por favor…???

Aí pessoal, consegui, eu troquei o arquivo validator-rules.xml por um quetem menos codigo

&lt;validator name=&quot;required&quot;
            classname=&quot;org.apache.struts.validator.FieldChecks&quot;
               method=&quot;validateRequired&quot;
         methodParams=&quot;java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest&quot;
                  msg=&quot;errors.required&quot;/&gt;