How do I know if a form has been successfully validated?
I have a form with a bunch of text fields. When users submit() the form, I display the updated form in "Read-Only" mode, by setting up textField.setReadOnly(true). However, I don't want the form to go Read-Only if the validation was not successfull. Is there a way to figure out of the validation was successful or not? (something like this.form1.isValid(), etc.)
Thanks
[385 byte] By [
Sabir] at [2007-11-26 9:10:58]

# 1
I haven't done it, but there must be a way to get the messages associated with the page.
However, I'd just create a second, read-only page. The submit/save button navigates to the new page. If validation fails, the server redisplays the original page. It's cleaner and allows you to use the built-in validation behaviour.
# 2
I have used that approach in some of the non-JSF apps, but I thought its more effecient to use just one form for read and write, since we can easily control it. Also, its easier to maintain one form - if you need to make changes, just do it in one place, like adding new fields, data binding, changing form colors, etc.
I agree that there should be a way to find out if form has been validated. Gurus?
Sabir at 2007-7-6 23:30:34 >

# 3
I found the solution. There is an event afterUpdateModelValues(), which is called only if the validation is successfull. I have moved my commit and setReadOnly code to that event and that seems to do the magic.
public void afterUpdateModelValues(){
try {
info("afterUpdateModelValues");
getWeb_userDataProviderUserDetail().commitChanges();
getSessionBean1().setNewUser(false);
getSessionBean1().setReadOnly(true);
} catch (Exception ex) {
error("Error updating user ID "+ getSessionBean1().getUserID()+ ": "+ex.getMessage());
log("Error Description", ex);
}
}
Sabir at 2007-7-6 23:30:34 >

# 4
Let's say you have a button called submitButton and you have a submitButton_action method associated with the button. I would think that you could put the read-only code there because the action method does not get called if there are any conversion or validation errors.
# 5
That's what I had in btnSave_action() method. Turns out the setReadOnly was getting called regardless. I ended up moving the code to afterUpdateModelValues() and that did the trick.
Sabir at 2007-7-6 23:30:34 >

# 6
I agree that putting afterUpdateModelValues will do the trick, but I do not agree that it is necessary to do so.
With the JSF lifecycle, the action method is not called if there are conversion or validation errors.
Here is a very simple example that illustrates this. Create a page (make it the start page)
Add textField1, button1, and messageGroup1.
Set required to true (check the box) for textField1.
Double-click the button and change the action method as follows:
public String button1_action() {
error("action method called");
return null;
}
Run the program. Don't put anything in the text field and click the button. You will not see the message "action method called". Now put something in the text field and click the button. Now you see the message.
So, how are you validating the fields? (1) Are you using validators from the palette, as shown in http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/val idators_converters.html? (2) Or are you writing your own custom validators, as shown in http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/cus tomvalidator.html.
If (2), are you throwing ValidatorExceptions?
# 7
I just have some required text fields on the forms which are read-write before save, and then I change them to read-only after the commit in the btnSave_action().
I just did your test page and found out that you are right, action event doesn't run if its form is not validated. I must have been doing something else which was causing that problem. Thanks for your help and clarification. You da man.
Sabir at 2007-7-6 23:30:34 >

# 8
Sabir,Just so you know, my full name is Christine (aka Diva #2). With a name like Chris and an alias like Jetsons (there is a Mrs. Jetsons too) lot's of people make that mistake so I don't mind. Chris http://blogs.sun.com/roller/page/divas
# 9
Oops.. You da lady :) I see that you are very active on this forum. Hopefully other Creator bloggers will become as active too.
Sabir at 2007-7-6 23:30:34 >

# 10
I've a problem in validation could you please help to solve it.
In the resource file I've added a .js file and in the design page I linked the .js file. I've a text box and a submit button. During the submit onclick function I am calling the validate javascript function.
If the javascript function return false how I will know, after the .js execution control directly transfers to btn_action method I guess.
Since we are converting the existing application we need to re-use the existing javascript code. This is one of the major tasks for us. Other wise we have to move the. js code to server side which is a teadous job.
Guru's please advice me.
# 11
I thought that if the function returned false then the page doesn't get submitted (and thus the server action code won't get invoked).