h:inputFile value becomes blank after validation f

2019-02-26 09:13发布

With many other standard JSF components, when a form is submitted and server side validation fails, the page is rendered again with the previously submitted form fields filled in for the user to edit and resubmit. I am looking for that same behavior from the new h:inputFile and I'm not finding it.

As a simple example, I have a form with an h:inputText and an h:inputFile. The inputText has server side validation enabled. If the user enters invalid text and selects a file and submits, the file is uploaded and the form is rendered with the invalid text in the text field and a h:message indicating the validation result. The issue I'm trying to address is at this point they correct their text input, but have to select a file again, and upload it again. Is there something basic I am missing, or is this the intended behavior?

1条回答
甜甜的少女心
2楼-- · 2019-02-26 09:37

HTML disallows prefilling an <input type="file"> as that otherwise opens a huge security hole. As JSF is in this context just a HTML code generator, it can't do much against that.

Your best bet is to ajax-submit the form and ajax-render only the parts which really need to be updated, such as messages (i.e. don't use render="@form"). This way the initial input value stays.

E.g.

<h:form enctype="multipart/form-data">
    <h:inputText id="text" />
    <h:message id="m_text" for="text" />

    <h:inputFile id="file" />
    <h:message id="m_file" for="file" />

    <h:commandButton value="submit">
        <f:ajax execute="@form" render="m_text m_file" />
    </h:commandButton>
</h:form>

See also:

查看更多
登录 后发表回答