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

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

问题描述

我想到了一个奇怪的问题.我试图找出问题所在,因此下面是我的简化代码.

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中也得到认可,并在以下答案中进行了详细说明: ResetInputAjaxActionListener 或PrimeFaces为此, <p:resetInput>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.

到目前为止,您需要清除

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中不可用,而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天全站免登陆