3 Replies Latest reply on Jun 21, 2012 5:42 PM by ealonso04

    setting focus back to current input control after validation error

    asookazian

      How do I set the focus back to current input control after validation error?  I know there is a focus attribute for the rich:calendar control but I only want the focus set back in the control if there is a validation error, not always (i.e. allow user to tab thru the control when there is no validation error).  I thought the focus would auto-set to current control by default when there is a validation error...

       

      <echo:decorate>
                          <rich:calendar id="newRateCategoryEffectiveDate"
                              inputClass="dateField"
                              required="false"
                              value="#{merchantBillingGroupEditorUI.rateCategory.priceEffectiveDate}"
                              oninputkeypress="return restrictDate(this, event);"                       
                              datePattern="MM/dd/yyyy"
                              enableManualInput="true"
                              isDayEnabled="isDayEnabled"
                              dayStyleClass="getDisabledStyle"                       
                              label="#{messages['/restricted/billing/add_merchant_billing_group_wizard_step1.xhtml/field.effectiveDate']}">
                              <a4j:support event="oninputblur"
                                              action="#{merchantBillingGroupEditorUI.validNewEffectiveDate()}"
                                              ajaxSingle="true"
                                              reRender="newRateCategoryEffectiveDate"
                                           requestDelay="0"
                                           eventsQueue="select"
                                           ignoreDupResponses="true"
                                           onsubmit="showProgress();"
                                           oncomplete="hideProgress();"/>
                          </rich:calendar>
                      </echo:decorate>

        • 1. Re: setting focus back to current input control after validation error
          ilya_shaikovsky

          you could:

           

          1) point focus attribute of support to some bean property which will holds the id of the component to be focused.

          2) write ajaxListener which will be invoked before renderResponce(so, it's fired even when validation errors occured). In the listener check if there was validation/conversion errors in added Faces Context. And if there are any pick the first message and put the id of the component which was risen this error to the property from 1) point.

           

          So if the focus attribute will remain empty - nothing will be changed for the user. And if it's contains id - this component thrown vaildation message and focus should be set to it.

          • 2. Re: setting focus back to current input control after validation error
            asookazian

            How do you set focus to a component that is rendered multiple times in multiple rows of a dataTable (this is actually the case here)?

             

            i.e. I have multiple rich:calendar controls that need the same validation and focus set to that one which failed validation.

            • 3. Re: setting focus back to current input control after validation error
              ealonso04

              Hi guys!

               

              I have the same issue right now! Did you find the solution to this?

               

              I almost understand the idea of Ilya but I don't know how to tell directly to the input control to obtain the focus, I mean, do I have to do that inside the java class where Im catching the listener and checking the FacesMessages, in order to know if there was at least one message error?

              Or is there a way to do that inside the xhtml file by some tag?

               

              I hope you can help out with this. Thanks in advance!