Olá Galera!
Bom estou com um “probleminha” em que após a validação agir e não deixar os dados serem gravados no banco por estarem de forma errada, ele não conserva o valor dos campos… simplesmente some com eles deixando os input’s vazios.
Gostaria de saber uma forma de após a ação da validação ocorrer, como mantenho os valores em seus devidos input’s!!!
Abaixo segue os fontes que poderão ajudar:
Este é o template que não guarda os valores após validar:
#parse("header.vm")
<center>
<h1>Editar CD</h1>
<p/>
</center>
#foreach($CdUpdateForm in $!v_collec)
#set($id = $CdUpdateForm.getCodCd())
#set($singer = $CdUpdateForm.getCantor())
#set($album = $CdUpdateForm.getTitulo())
#set($year = $CdUpdateForm.getAnoLanc())
#set($price = $CdUpdateForm.getPreco())
#set($gen = $CdUpdateForm.getCodGenFK())
#end
<center >
#errorMarkup()
</center>
<form action="/vs/confirmUpdate.do" method="post">
<input type="hidden" name="codCd" value="$id">
Genero: <select name="codGenFK">
#foreach ($item in $generos)
<option value="$item.codGenero"
#if($item.codGenero == $gen)
selected
#end
>$item.nome</option>
#end
</select><p/>
Cantor: <input type="text" name="cantor" value="$!test" maxlength="30" size="40"/><p/>
Titulo: <input type="text" name="titulo" value="$!album" maxlength="50" size="65"/><p/>
Ano Lançamento: <input type="text" name="anoLanc" value="$!year" maxlength="4"/><p/>
Preço: <input type="text" name="preco" value="$!price" maxlength="15" /><p/>
<table>
<td>
<tr>
<button type="submit">Alterar</button>
</form>
</tr>
<tr>
<form action="/vs/confirmDelete.do" method="post">
<input type="hidden" name="id" value="$id">
<button type="submit">Excluir</button>
</form>
</tr>
<tr>
<form action="/vs/showList.do">
<button type="submit">Cancelar</button>  
</form>
</tr>
</td>
</table>
#parse("footer.vm")
Validation.xml:
[code]<?xml version=“1.0” encoding=“UTF-8”?>
<!DOCTYPE form-validation PUBLIC “-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN”
“http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd”>
<!–
Copyright 2005 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
–>
<form-validation>
<formset>
<form name=“CdForm”>
<field property=“cantor” depends=“required,maxlength,minlength”>
<arg0 key=“label.cantor”/>
<arg1 name=“maxlength” key="${var:maxlength}" resource=“false”/>
<arg1 name=“minlength” key="${var:minlength}" resource=“false”/>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>2</var-value>
</var>
</field>
<field property="titulo" depends="required,maxlength,minlength">
<arg0 key="label.titulo"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>50</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>2</var-value>
</var>
</field>
<field property="precoMascked" depends="required,validwhen">
<arg0 key="label.preco"/>
<msg key="errors.validwhen.preco" name="validwhen"/>
<var>
<var-name>test</var-name>
<var-value>(*this* != 0)</var-value>
</var>
</field>
</form>
<form name="CdUpdateForm">
<field property="cantor" depends="required,maxlength,minlength">
<arg0 key="label.cantor"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>30</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>2</var-value>
</var>
</field>
<field property="titulo" depends="required,maxlength,minlength">
<arg0 key="label.titulo"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>50</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>2</var-value>
</var>
</field>
<field property="anoLanc" depends="required,maxlength,minlength">
<arg0 key="label.anoLanc"/>
<arg1 name="maxlength" key="${var:maxlength}" resource="false"/>
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var>
<var-name>maxlength</var-name>
<var-value>4</var-value>
</var>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field property="preco" depends="required,mask,validwhen">
<arg0 key="label.preco"/>
<msg key="errors.validwhen.preco" name="validwhen"/>
<msg name="mask" key="precomask.preco.maskmsg"/>
<var>
<var-name>test</var-name>
<var-value>(*this* > 0)</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^\d{1,6}(\.\d{1,2})?$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>[/code]
Parte do struts-config.xml que corresponde ao template de cima:
[code] <!-- UPDATE -->
<action path="/update"
type="vs.CdUpdateAction"
name="CdUpdateForm"
scope="request"
validate="false"
input="/update.do">
<forward name="f_update" path="/altera.vm"/>
</action>
<action path="/confirmUpdate"
type="vs.CdConfirmUpdateAction"
name="CdUpdateForm"
scope="request"
validate="true"
input="/altera.vm">
<forward name="f_success" path="/success.vm"/>
</action>[/code]
As classes estão fazendo o update corretamente, o erro só ocorre quando há algum campo fora dos padrões determinados pelo validator!
Qualquer duvida ou necessidade de outros fontes… só postar ai!
Valeu… Aguardo ajuda!
[]'s