仅当验证失败时,输入字段才保留以前的值 [英] Input fields hold previous values only if validation failed

查看:24
本文介绍了仅当验证失败时,输入字段才保留以前的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想到了一个奇怪的问题.我试图隔离问题,因此以下是我的简化代码.

I came up with a strange problem. I tried to isolate the problem so following is my simplified code.

public class MyBean {

  private List<Data> dataList;
  Data selectedData;

  public MyBean() {
    dataList = new ArrayList<Data>();
    dataList.add(new Data("John", 16));
    dataList.add(new Data("William", 25));
  }

  public List<Data> getDataList() {
    return dataList;
  }

  public void edit(Data data) {
    selectedData = data;
  }
  public void newData() {
    selectedData = new Data(null, null);
  }

  public Data getSelectedData() {
    return selectedData;
  }

  public class Data {
    String name;
    Integer age;
    Data(String name, Integer age) {
      this.name = name;
      this.age = age;
    }
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public Integer getAge() {
      return age;
    }
    public void setAge(Integer age) {
      this.age = age;
    }
  }
}

xhtml:

<rich:modalPanel id="pop">
  <h:form>
    Name: <h:inputText value="#{myBean.selectedData.name}" required="true" id="txtName"/><br/>
    Age : <h:inputText value="#{myBean.selectedData.age}" required="true" id="txtAge"/>
    <a4j:commandButton value="Save"/>
    <a4j:commandButton value="Close" onclick="Richfaces.hideModalPanel('pop');return false;"/>
    <br/>
    <rich:message for="txtName"/><br/>
    <rich:message for="txtAge"/>
  </h:form>     
</rich:modalPanel>

<h:form>
  <rich:dataTable value="#{myBean.dataList}" var="data">
    <rich:column>#{data.name}</rich:column>
    <rich:column>
      <a4j:commandLink value="Edit" action="#{myBean.edit(data)}" reRender="pop" oncomplete="Richfaces.showModalPanel('pop')"/>
    </rich:column>
  </rich:dataTable>
  <a4j:commandButton value="New" action="#{myBean.newData()}" reRender="pop" oncomplete="Richfaces.showModalPanel('pop')"/>
</h:form>

这是错误的路径:

  1. 加载页面
  2. 点击第一行的编辑"链接(弹出显示)
  3. 在弹出窗口中,清除年龄"字段并单击保存".(显示必需的消息)
  4. 点击取消(不填写年龄"字段)
  5. 点击第二个链接.
  6. 现在显示不相关的数据(以前的数据).- 这就是问题
  7. 即使我点击新建"按钮,它也会显示不正确的数据.

仅当弹出窗口中的验证失败时才会发生这种情况.
有解决办法吗?

This happens only if a validation is failed in the popup.
Is there a solution for this?

推荐答案

这个问题在 JSF 2 中也有认识并在下面的回答中有详细解释:如何在发生验证错误后使用 PrimeFaces AJAX 填充文本字段? 如果您使用的是 JSF 2,您可以使用 OmniFaces 的 ResetInputAjaxActionListener 或 PrimeFaces 的 resetValues="true" 为此.

This problem is in JSF 2 also recognized and explained in detail in the following answer: How can I populate a text field using PrimeFaces AJAX after validation errors occur? If you were using JSF 2, you could have used OmniFaces' ResetInputAjaxActionListener or PrimeFaces' <p:resetInput> or resetValues="true" for this.

到此为止,需要清除EditableValueHolder 组件即将被 ajax 渲染,但不包含在 ajax 执行中.要清除状态,在 JSF 2 中您将使用便利方法 resetValue() 用于此,但这在 JSF 1.2 中不可用,您需要调用 4 个单独的方法 setValue(null), setSubmittedValue(null), setLocalValueSet(false), setValid(true) 清除状态.

To the point, you need to clear the state of the EditableValueHolder component when it's about to be ajax-rendered, but which isn't included in the ajax-execute. To clear the state, in JSF 2 you would have used the convenience method resetValue() for this, but this isn't available in JSF 1.2 and you need to invoke the 4 individual methods setValue(null), setSubmittedValue(null), setLocalValueSet(false), setValid(true) to clear the state.

要确定哪些组件将被 ajax 渲染,但没有被 ajax 执行,在 JSF 2 中,您将使用 PartialViewContext 方法来实现这一点,但这在 JSF 中不可用1.2 还没有标准化ajax.您需要摆弄 RichFaces 特定的 ajax API 才能弄清楚.我不能从头顶上说出来,所以这里有一个启动示例,假设您已经知道需要清除的组件.想象一下,您的弹出窗口中的表单具有 id="popForm" 并且名称输入字段具有 id="nameInput",这是您在 中清除它的方法newData() 方法:

To figure out which components are to be ajax-rendered, but aren't been ajax-executed, in JSF 2 you would have used PartialViewContext methods for this, but this is not available in JSF 1.2 which hasn't standardized ajax yet. You'd need to fiddle with RichFaces specific ajax API in order to figure that. I can't tell that from top of head, so here's a kickoff example assuming that you already know the components which needs to be cleared. Imagine that the form in your popup has id="popForm" and the name input field has id="nameInput", here's how you could clear it inside the newData() method:

UIInput nameInput = (UIInput) context.getViewRoot().findComponent("popForm:nameInput");
nameInput.setValue(null);
nameInput.setSubmittedValue(null);
nameInput.setLocalValueSet(false);
nameInput.setValid(true);

这篇关于仅当验证失败时,输入字段才保留以前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆