0

I have a short question that has been bothering for last few hours:

How can I execute a javascript function before message that a value is required is shown? (on form submit some value inside of it is empty when it needs to be entere).

I have faced this problem while using RealPerson captcha plugin for jQuery in my jsp page. When I click on submit button with input field for captcha empty, the captcha disappears.

Update 1:

I tried with binding and rendered but the problem still seems to be there.

<h:outputLabel for="captcha" value="#{ui.pleaseEnterTextInTheImage}"  rendered="#{sessionBean.showCaptcha}"/>
<h:panelGroup rendered="#{sessionBean.showCaptcha}">
     <h:inputText id="captcha" styleClass="captcha" binding="#{captcha}"
                                                     validator="#{validationBean.captchaValidator}" required="true"/>
     <h:outputText value=" "/><h:message for="captcha" styleClass="captchaMsg"/>
</h:panelGroup>
<h:panelGroup rendered="#{!captcha.valid}">
     <script>alert('Foo input is invalid!');</script>
</h:panelGroup>

Update 2:

When page goes live:

before

After I click "Register" with captcha entry left blank: after

Update 3:

jQuery code that I use.

First on pageload, I assign the captcha to the field:

$(function() {
      $('.captcha').realperson();
});

Then, after I reassign a new captcha after the field gets rerendered by calling this function from my bean:

function updateCaptchaFromBean() {
    $(function() {
        $('.captcha').realperson();
    });
}

Found solution

I've found a simple javascript trick to solve this with onclick():

<h:commandButton styleClass="buttonSubmit" value="#{ui.registerBUTTON}"
     action="#{register.addAction}"
     onclick="if ($('.captcha').val() == '') return false"
     id="submitBtn" />
Pavlo Bazilnskyy
  • 336
  • 2
  • 6
  • 18
  • JSP? So may I assume that you're still on JSF 1.x? – BalusC Jun 15 '11 at 15:14
  • I am quite new to it. I am using JSF 2.0, at least partially. I was under impression that it also uses JSP... Please correct me if you have time. – Pavlo Bazilnskyy Jun 15 '11 at 15:43
  • Are you really using JSF 2.0 on `*.jsp` pages? Or are you using `*.xhtml` pages (which is actually Facelets, the successor of JSP and the default view technology of JSF 2.x). Anyway, my answer should also work out for Facelets, but better is to use `` instead of ``. – BalusC Jun 15 '11 at 15:47
  • By the way, how about your previous question? http://stackoverflow.com/questions/6271579/getting-a-path-to-a-resource-file-from-managed-bean-in-jsf This gives me the impression that you're *actually* using JSF 1.x. Or at least JSF 2.x in JSF 1.x modus because the `faces-config.xml` is incorrectly been declared conform JSF 1.x instead of JSF 2.x. What book/tutorials were you using? Do they cover JSF 2.x? – BalusC Jun 15 '11 at 15:51
  • I am making a project for a company, which is my first attempt to use that technology and I started it using a base that I was given, which was written in JSF 1.2. Basically, its mostly JSF 1.2 as I understand. – Pavlo Bazilnskyy Jun 15 '11 at 15:55
  • By the way, thanks for your answer to that one, I figured out what I was doing wrong. – Pavlo Bazilnskyy Jun 15 '11 at 15:56

1 Answers1

1

Assuming JSF 1.x on JSP without ajax fanciness, here's how you could do it at its simplest:

<h:panelGroup rendered="#{!foo.valid}">
    <script>alert('Foo input is invalid!');</script>
</h:panelGroup>
...
<h:inputText id="foo" binding="#{foo}" value="#{bean.foo}" required="true" />
<h:message id="fooMessage" for="foo" />

Update as per the comments, it doesn't work. As per your updated question you seem to be using a custom validator. This problem can only mean that you didn't throw a ValidatorException in your custom input validator, but just added a message. This is wrong. You should be throwing a ValidatorException so that JSF can mark the component invalid.

So, you shouldn't be doing the following in the validator method:

context.addMessage(component.getClientId(context), new FacesMessage("Fail"));

but you should rather do

throw new ValidatorException(new FacesMessage("Fail"));

this way the #{!captcha.valid} will resolve as true.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks a lot for your help but seems like it still doesn't work. I've tried adding a binding to it but no luck. – Pavlo Bazilnskyy Jun 15 '11 at 15:49
  • Are you using JSF 2.0 ``? Ensure that you also render the portion containing the panelgroup. – BalusC Jun 15 '11 at 15:50
  • No, I was not using ''. How can I use it in my situation. I have posted a code of a peace of view that I am working with. Sorry, for my little understanding of things in this area, I am still trying to learn it. I would appreciate it a lot if you could give me a hint on how to use it with it. – Pavlo Bazilnskyy Jun 15 '11 at 15:58
  • Likely you didn't throw `ValidatorException` in your validator. See answer update. – BalusC Jun 15 '11 at 16:10
  • My problem is that when it goes to `captchaValidator()`, everything works fine because I am able to call a js function, which makes sure that the captcha element is rendered properly, but in the case when this input is left empty and `required="true"` makes an error show up, the input gets rerendered and the captcha goes away. – Pavlo Bazilnskyy Jun 15 '11 at 16:10
  • I think, there can be 3 solutions to this problem: call javascript function when required message is shown, do not allow a form to be submitted when captcha field is empty and show a warning message or use different way of showing captcha, but I'd like to stick to first 2 – Pavlo Bazilnskyy Jun 15 '11 at 16:10
  • I don't see a `required="true"` in your question code snippet? – BalusC Jun 15 '11 at 16:12
  • Oh, very sorry my bad. I was playing around with calling javascript function on `onclick`and removed it. Now I will put it back. Basically, required message is a cause of my problem – Pavlo Bazilnskyy Jun 15 '11 at 16:13
  • My answer should work fine for `required="true"` as well. JSF will then also mark the component as invalid. Are you sure you're running the code you think you're running? – BalusC Jun 15 '11 at 16:14
  • Pretty much... When I run it a message that a value is required is shown, but `alert` one does not pop up. – Pavlo Bazilnskyy Jun 15 '11 at 16:17
  • I've added 2 pictures to the question, maybe they will help. The captcha goes away. Thanks a lot for your help, once again! – Pavlo Bazilnskyy Jun 15 '11 at 16:21
  • Are you validating empty fields by jQuery instead of JSF or something? Are you using a 3rd party JSF component library? I don't recognize the style of the required error icon nor the buttons as from any JSF component library I am familiar with. – BalusC Jun 15 '11 at 16:28
  • I don't validate the field in jQuery, it is validated in `captchaValidator()` IF something was put in there. If it's empty it never goes to validator and a `javax.faces.component.UIInput.REQUIRED` message is shown and the captcha disappears. I don't use 3rd party components. That image is a property assigned to `javax.faces.validator.NOT_IN_RANGE`as an tag. I tried putting text instead and the result is the same. – Pavlo Bazilnskyy Jun 15 '11 at 16:41
  • Hmm OK. That still doesn't explain why `#{!captcha.valid}` evaluates false on a `required="true"` failure. I tested it here with Mojarra 1.2_15 and it works fine. Can you figure what JSF impl/version you're using? You can determine it by reading server startup log or by extracting `jsf-impl.jar` file and reading its `META-INF/MANIFEST.MF` file (it's usually located at very bottom). – BalusC Jun 15 '11 at 16:49
  • From my `MANIFEST`: Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.0 Created-By: 1.5.0_19-b02 (Sun Microsystems Inc.) Specification-Title: JavaServer Faces Specification-Version: 1.2MR2 Implementation-Title: Mojarra Implementation-Version: 1.2_13-b01-FCS Implementation-Vendor: Sun Microsystems, Inc. Implementation-Vendor-Id: com.sun Extension-Name: com.sun.faces – Pavlo Bazilnskyy Jun 15 '11 at 16:58
  • Okay, Mojarra 1.2_13 it is. Sorry, still not reproduceable. To exclude JSF or JS from being the cause, please submit the form with an empty `required="true"` captcha and view the HTML source of the response (rightclick, view source). Is the to-be-rendered JS code there? If so, then JSF has done its job properly and the problem is in the JS code. – BalusC Jun 15 '11 at 17:04
  • Thanks a lot or your help! I've found a workaround solution. I've updated my question with it. – Pavlo Bazilnskyy Jun 17 '11 at 11:18