Validator
Hello
I have a form with 2 fields :
1) User Name
2) Password
Both are inside a managed bean.
I tried to add validator to the password field in order to check if password is legal.
The problem is that inside the validator , i can see only the password (by the 'object ' which has been sent to the validate method),
but i can't see the user_name, because the managed bean's setter hasn't invoked yet.
How do i access also to the user_name ?
# 1
2 general solutions:
1) Create single custom component which represents those two fields.
or
2) Validate during action method.
# 2
Thanks for the quick answer,
How do you recommand to validate during action method ? I can't do it with a java script, i must do it with validator.
regarding to the custom component, what do you mean ?
note that i have already a managed bean for these two fields.
Thanks
Message was edited by:
George_.Smith
Message was edited by:
George_.Smith
# 3
I wasn't talking about Javascript.
Anyway, another solution comes to mind: retrieve the component using UIViewRoot#findComponent(clientId), but then you have to hardcode the clientId in the validator, or to pass it as a f:attribute of the component to the validator.
# 4
This is the validator methos :
public void validate(FacesContext arg0, UIComponent arg1, Object value)
throws ValidatorException{
.......
........
.......
}
How exactly should i access the JSP's user_name field ? (which is also a member in the managed bean)
# 5
You can get the UIViewRoot by FacesContext.
# 6
Thats OK , i'm getting the component , but how should i read from it ?
Thats what i have :
public void validate(FacesContext arg0, UIComponent arg1, Object value)
throws ValidatorException{
arg1.findComponent("text3"); //this returns the user_name component
}
how should i read the user_name value from this component ?
PS: Is there another way of validating the user+password ? maybe Validator is not the best practice for that ?
# 7
> S: Is there another way of validating the
> user+password ? maybe Validator is not the best
> practice for that ?
I would agree with the statement that a validator is probably not the best place to validate a user name and password combination. I would use an action method or an actionListener method.
# 8
by action method you mean calling a method inside the bean itself which returns boolean value ?
# 9
Err, no. It's the action (or actionListener) method which is invoked by an UICommand element.
JSF<h:inputText value="#{myBean.userForm.username}" />
<h:inputSecret value="#{myBean.userForm.password}" />
<h:messages />
<h:commandButton value="login" action="#{myBean.login}" />
MyBeanpublic void login() {
User user = UserDAO.getUser(userForm.getUsername(), userForm.getPassword());
if (user == null) {
facesContext.addMessage(null, new FacesMessage("Invalid username and password combination"));
} else {
// Proceed.
}
}
You can also hardcode the client ID to add message to specific field, or bind the input fields instead of the values, then you can get their clientId's (preferred).
# 10
Thanks,
I have 2 questions :
1) how do you access the facesContext object within the login method ?
2) "Proceed" - how do you actually proceed to another jsf page ? (what is the method for that ?
# 11
Fill it in yourself. It was just a stub example to show how to set a generic facesmessage.
# 12
> 1) how do you access the facesContext object within
> the login method ?
>
> 2) "Proceed" - how do you actually proceed to
> another jsf page ? (what is the method for that ?
These are basic questions - I suggest you find a JSF tutorial and work through it first. Then if you still have questions, post here.