【问题标题】:h:inputSecret redisplay="false" required="false"><s:validateEquality : confirm field either required or not checked at allh:inputSecret redisplay="false" required="false"><s:validateEquality : 确认字段是必需的还是根本不检查
【发布时间】:2010-11-12 13:29:34
【问题描述】:

我使用接缝 2.2.0.GA。 我的密码/确认字段有 redisplay="false" 所以当前密码不在 html 中。 validateEquality 在确认字段中。

我想要以下行为:当我创建一个新实体时,密码/确认字段都应该是必需的。当我编辑一个实体时,这两个字段都不应该是必需的,但无论如何都应该检查相等性。为此,我有 h:inputSecret required="#{required}" 并计算参数“required”。

有两个问题:

1) s:validateEquality 也有属性“required”,但它看起来有问题:如果我硬编码 true 或 false,它会按预期工作,但如果我使用 required="#{required}" 并且在对话过程中这个参数改变了,验证器仍然表现得好像参数没有改变一样。

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;        }
}

沙盒控制器.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();
  }
}

【问题讨论】:

    标签: seam


    【解决方案1】:

    问题 #1:

    我相信以下代码会将您的“必填”字段变为静态:

    <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."));
    

    【讨论】:

    • 要明确,UI参数将字段变为静态的原因是第一次加载页面时将EL引用“#{sandboxBean.required}”转换为值。当底层 bean 值更改时,不会重新解释该值。是否有某些原因您不只是在页面其余部分的 xhtml 中使用“#{sandboxBean.required}”引用?
    • 这么长时间过去了...然后我决定使用这个: required="true" redisplay="true" converter="passwordConverter" 并使用自定义转换器,因此字段永远不会为空
    【解决方案2】:
    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();
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-12
      • 2015-07-22
      • 2016-01-05
      • 2014-10-15
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多