Input value does not disappear after validation failure

I searched a lot and did not find a solution.

I am using JSF 2.1 and RichFaces 4.2.3 and want to check user login details

I have two input fields. One username and one password, both with @NotEmpty

login.xhtml

 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:rich="http://richfaces.org/rich" xmlns:a4j="http://richfaces.org/a4j" xmlns:mt="http://java.sun.com/jsf/composite/components"> <h:body> <ui:composition template="/protected/user/login-template.xhtml"> <ui:define name="panel-navigation"> <ui:include src="/protected/user/login-left-menu.xhtml" /> </ui:define> <ui:define name="contentbody"> <h:form> <div id="content"> <div class="subcolumns"> <div class="c65l"> <div class="subcl"> <p class="sum-error-message" /> <fieldset> <h3> <h:outputText value="#{styleguideMessages.login}" /> </h3> <p> <h:outputText value="#{messages.login_text}" /> </p> <div class="subcolumns"> <mt:inputText id="loginName" value="#{authenticationPM.loginName}" label="#{messages.general_label_loginname}" required="true" /> <div class="c33r validation"></div> </div> <div class="subcolumns"> <mt:inputText id="password" value="#{authenticationPM.password}" label="#{styleguideMessages.db_password}" required="true" secret="true" /> <div class="c33r validation"></div> </div> <div class="subcolumns"> <h:commandLink id="loginButton" action="#{authenticationPM.doLogin}" value="#{styleguideMessages.login}" title="#{styleguideMessages.login}" styleClass="button last" /> </div> </fieldset> </div> </div> </div> </div> <mt:commandButton id="login" action="#{authenticationPM.doLogin}" value="hidden" style="visibility:hidden" /> </h:form> <script src="#{facesContext.externalContext.requestContextPath}/js/lib/loginEnter.js" type="text/javascript" charset="utf-8"></script> </ui:define> </ui:composition> 

AuthentificationPM.java

 import *; @Named @SessionScoped public class AuthenticationPM extends AbstractPM implements Serializable { /** * The user name from the login. */ private String userName; /** * password. */ private String password; /** * Returns the login name. * * @return String */ @NotNull @Pattern(regexp = "^[^\"/\\[\\]:;|=,+?*<>]*$", message = "{user.loginname.pattern}") public String getLoginName() { return userName; } /** * Returns the password. * * @return String */ @NotNull public String getPassword() { return password; } /** * Sets the login name. * * @param loginName * - the login name of the user */ public void setLoginName(String loginName) { this.userName = loginName; } /** * Sets the password. * * @param password * - the password of the user */ public void setPassword(String password) { this.password = password; } } 

If one of them (for example, the password) is not completed, the check fails and a message is displayed (as it should be).

Now I will delete the username and enter the password. Validation fails because the username is empty. It clears the password field and displays a message for the username.

And now an error occurs: the first username entered appears again! How can I prevent this behavior?

I know that after checking the process, the values ​​of the update model and application are skipped and a rendering response is executed. As described here. The render response accepts the values ​​stored in the ui component (during request requests) and uses them for reuse instead of deleting an invalid value.

I tried this solution as well as this idea . In both cases, my component was not found.

Any help or ideas are greatly appreciated.

hi? Kevin

+5
source share
1 answer

If you simply delete the username from the input field, the value should be represented as an empty string , which will be a legitimate replacement for the username, and also matches the pattern.

I can only assume that you have set up

 <context-param> <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name> <param-value>true</param-value> </context-param> 

in your web.xml. Therefore, when you try to pass an empty field, it is interpreted as null , which will not match your @NotNull annotation for the username attribute. Thus, the value setting is canceled, and it will maintain its previous state by calling it SessionScoped - this is the username that was entered once.

At least this is the only value szenario a (String-) is not updated when I present an empty string.

If this value is set to true , the described behavior can be reproduced.


You may ask why this does not apply to your text field, where do you enter the password when it is deleted? Since you use secret="true" , the text field will not display information about the entered string (ie Length), even if the bean attribute is not set to zero (after deleting the password) and still contains the wrong password.

But keep in mind that simply removing this attribute can cause functionality in which other developers rely to get null instead of an empty string . Therefore, you might consider removing the @NotNull annotation and manually checking for null .

+3
source

Source: https://habr.com/ru/post/1203719/


All Articles