1

i have an input text and i want to select the value inside the text area upon validation error. it is an inputtext. I am using focus to set the focues on the field. Code does throw an error but i want the input values to be selected too

<p:focus for="inputText" />
   <p:inputText id="inputText" value="#{bean.value}" required="true" rendered="#{bean.method}" styleClass="xyz"/>
  • this may help you: http://stackoverflow.com/questions/9617914/how-to-find-indication-of-a-validation-error-required-true-while-doing-ajax , but it would be good if you provide more code, like when this validation error is fired, upon clicking on an commandButton? – Ouerghi Yassine Jan 14 '16 at 19:24
  • Yes, the command button fires the validation. It throws an error just fine. All i need is it could select the value inside the text area too – Ayush Khandelwal Jan 14 '16 at 19:39

1 Answers1

2

The PrimeFaces p:focus automagically focusses the first invalid input. So effectively, there is nothing PrimeFaces related the rest of the solution. Client-side it is all html

For selecting text when the input focussed, use something like

<p:inputText onfocus="this.setSelectionRange(0, this.value.length)" .../>

You can extend it to first check if the input is invalid by checking for the presence of an error class and not select anything if it is not invalid (not tested):

<p:inputText onfocus="$(this).hasClass('ui-state-error' ? this.setSelectionRange(0, this.value.length) : return" .../>

if you want this for all inputs or don't want any inline javascript, create a jquery eventhandler

Kukeltje
  • 12,223
  • 4
  • 24
  • 47