I want to set focus on inputText field in JSF when there is a validation error on that particular inputText. How can I implement this?
I thought validatorMessage on the inputText would do this out of the box but it appears not.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no such default behaviour. You need to implement it yourself or use a 3rd party library for the job. Basically, during the render response you need to walk through the submitted form for all
UIInputcomponents and find the first one whoseisValid()returnsfalseand then store its client ID in the request map so that you can print it as a JS variable.Then you can use the following script to set the focus:
The JSF utility library OmniFaces has a reuseable component for this, the
<o:highlight>(source code here and showcase demo here). It additionally also sets a styleclass on the invalidated inputs so that you can specify for example a different background color by CSS.