Friday, October 12, 2012

Code Snippet - Close dialog if all fields are valid

Today I was working on a dialog that had fields with validation. I only want to close the dialog if all fields are valid. I'm not aware of any inbuilt XSP methods that does this. This code snippet checks for any invalid fields in the dialog. If all fields are valid, the dialog is closed.
function closeDialogIfValid( dialogId ){
 var invalidCount = dojo.query( '[id="' + dialogId + '"] [aria-invalid="true"]' ).length;
 if( invalidCount === 0 ){
  XSP.closeDialog( dialogId );
 }
}
This probably only works if you're using server side validation.

If there is something like this in the XSP API, please let me know.

Share and enjoy!

6 comments:

Brad Balassaitis said...

I've been working on something similar lately and I've been using that aria-invalid="true" property for styling, but I've run into a limitation on dojo-parsed fields (such as type-ahead or name pickers). They seem to lose that property after the page (or panel, etc) refreshes. Have you seen this behavior?

Tommy Valand said...

I struggled with a similar issue a while back, but I can't remember how/if I resolved it.

Justin Hill said...
This comment has been removed by a blog administrator.
Tommy Valand said...

The best place to ask for help is probably StackOverflow. I'm not doing freelance work, but if it's a question I can answer, I'll try to help on SO. :)

Justin Hill said...

Hi Tommy,

Ok, that's unfortunate. If you change your mind, I don't think this will take you very long, but I do need some help and would prefer to communicate with you directly. If you could drop me an e-mail at some point I would appreciate it.

In any case, please remove my e-mail address comment from your blog so it is not posted publicly.

Thank you,

Justin

Tommy Valand said...

I've deleted the comment.