我使用seam 2.2.0.GA. 我的密码/确认字段有redisplay =“false”,因此当前密码不在html中。 validateEquality位于确认字段中。
我想要以下行为:当我创建一个新实体时,应该都需要密码/确认字段。当我编辑实体时,不应该要求这两个字段,但在任何情况下都应该检查相等性。为此,我有h:inputSecret required =“#{required}”并计算参数“required”。
有两个问题:
1)s:validateEquality也具有“required”属性,但它看起来有些错误:如果我硬编码为true或false,它按预期工作,但如果我使用required =“#{required}”并且在对话期间param更改后,验证程序的行为仍然就像param没有更改一样。
2)s:如果确认字段为空,则validateEquality required =“false”不检查相等性。
#{sandboxController.now}
<ui:param name="label" value="password"/>
<ui:param name="labelConfirm" value="confirm password"/>
<ui:param name="message" value="not the same"/>
<ui:param name="id" value="bbb"/>
<ui:param name="redisplay" value="#{sandboxBean.redisplay}"/>
<ui:param name="required" value="#{sandboxBean.required}"/>
<ui:param name="value" value="#{sandboxBean.password}" />
<a4j:form id="personalForm">
<a4j:region>
required: <h:selectBooleanCheckbox value="#{required}">
<a4j:support event="onclick" reRender="passwordpanel" />
</h:selectBooleanCheckbox><br/>
redisplay: <h:selectBooleanCheckbox value="#{redisplay}">
<a4j:support event="onclick" reRender="passwordpanel" />
</h:selectBooleanCheckbox><br/>
</a4j:region>
<a4j:outputPanel id="passwordpanel">
password: #{value}<br/>
<s:decorate styleClass="fieldForm #{formClass}">
<div class="fieldLabel #{labelClass}">
<s:label styleClass="#{invalid?'error':''}" >
#{label}
<s:span styleClass="required" rendered="#{required}">*</s:span>
</s:label>
</div>
<div class="fieldInput">
<s:validateAll>
<h:inputSecret id="#{id}" value="#{value}" required="#{required}" redisplay="#{redisplay}">
</h:inputSecret>
</s:validateAll>
<s:message styleClass="error"/>
</div>
</s:decorate>
<s:decorate styleClass="fieldForm #{formClass}">
<div class="fieldLabel #{labelClass}">
<s:label styleClass="#{invalid?'error':''}" >
#{labelConfirm}
<s:span styleClass="required" rendered="#{required}">*</s:span>
</s:label>
</div>
<div class="fieldInput">
<s:validateAll>
<h:inputSecret value="#{value}" required="#{required}" redisplay="#{redisplay}">
<s:validateEquality for="#{id}" required="#{required}" message="#{message}"/>
</h:inputSecret>
</s:validateAll>
<s:message styleClass="error"/>
</div>
</s:decorate>
</a4j:outputPanel>
<a4j:commandLink value="submit" action="#{sandboxController.getNow}" reRender="personalForm" /><br/>
</a4j:form>
SandboxBean.java:
package org.foo.model;
import java.io.Serializable;
public class SandboxBean implements Serializable {
private static final long serialVersionUID = 1L;
private String password = "abcde";
private boolean required=false;
private boolean redisplay=false;
public void setRequired(boolean required) { this.required = required; }
public boolean isRequired() { return required; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public void setRedisplay(boolean redisplay) { this.redisplay = redisplay; }
public boolean isRedisplay() { return redisplay; }
}
SandboxController.java:
package org.foo.logic.model;
import java.io.Serializable;
import java.util.Date;
import org.foo.model.SandboxBean;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
@Name("sandboxController")
@Scope(ScopeType.CONVERSATION)
public class SandboxController implements Serializable{
private static final long serialVersionUID = 1L;
@Logger Log log;
@Out(required = false)
SandboxBean sandboxBean;
@Begin(join=true)
@Factory("sandboxBean")
public void initSandBoxBean(){
sandboxBean = new SandboxBean();
}
public Date getNow() {
return new Date();
}
}
答案 0 :(得分:0)
问题#1:
以下代码会转换您的&#34; required&#34;现场静态,我相信:
<ui:param name="required" value="#{sandboxBean.required}"/>
问题#2:
http://seamframework.org/Community/CustomValidatorUsingTwoFields
使用标有相应注释的自定义验证器,然后可以使用自定义验证器调用。
@Validator
public class PasswordValidator implements javax.faces.validator.Validator, Serializable{
...
throw new ValidatorException(new FacesMessage("Passwords do not match."));
答案 1 :(得分:0)
public class PasswordConverter
implements Converter
{
public static String DONT = "__dontchange__";
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
if (DONT.equals(value) && component instanceof UIOutput) {
return ((UIOutput)component).getValue();
} else {
return value;
}
}
public String getAsString(FacesContext context, UIComponent component, Object value)
{
if (value instanceof String && component instanceof UIOutput && ((String)value).length() > 0) {
return DONT;
} else if (value == null) {
return null;
} else {
return value.toString();
}
}
}