Friday, December 21, 2012

Skip Validation for ADF Required Tabs

Tell me - how often it happend to implement such basic layout where required attributes from one Entity are located on the same fragment/page but in different tabs? I guess quite often, this is common requirement. However, is not so obvious how to implement it properly. Most likely you will end up into infinite loop of validation errors for required fields when trying to switch between tabs. I will describe in this post how to implement multiple tabs with required attributes from the same Entity and avoid unexpected validation errors.

Sample application - TestValidationApp.zip is based on Employees EO and contains two tabs - Contact and Details. Half of Employees attributes are located under Contact and the rest under Details tab:


Firstly I will show you how it works by default. Press Create button, this will call CreateInsert operation and insert new row (attributes from this row will be initialized blank in both tabs):


Type values for required fields and try to open second tab - Details. Validation errors are displayed for missing attribute values in the second tab. But wait a bit - we want to open Details tab and already getting validation errors? This makes our form unusable:


Let's make first attempt to solve this problem - set Immediate = true for both tabs. In theory this should help, in practice it doesn't. Immediate = true allows to skip validation lifecycle, but as a side effect it prevents Model update and value is not set.  Immediate = true will not solve described issue, but still let's test it - I will show you why Immediate = true doesn't help at all. Select both tabs in JDeveloper:


Set Immediate = true property for both tabs:


Do the same test again - create new record and type required values in the first tab:


Because of Immediate = true - we can open second tab without getting validation errors. Type required values in the second tab and press Save. Validation errors are reported for the required values in the first tab. This is quite a magic, actually not - this happens because of Immediate = true - entered data is not stored on the Model:


Set Immediate = true to be false for both tabs, as it was originally:


Solution will be described now. Open page definition file and select root tag in the structure window:


Go to Properties window and search for SkipValidation property. Set SkipValidation to true:


This allows to prevent such silly validation execution as you saw above. Enter required values in the first tab:


Switch to the second tab - enter required values there. Press Save button to commit transaction - no errors this time:


SkipValidation = true doesn't affect regular ADF validation behavior - if required field value is missing during transaction commit - ADF will stop processing and ask user to provide a values as expected:

22 comments:

ojk said...

Is there any way to skip required fields validation when switching between tabs and perform it only on submit?

Andrejus Baranovskis said...

Its what this post about. Isnt it?

Andrejus

ojk said...

I meant: switch to the second tab without entering required values in the firs tab :).

Andrejus Baranovskis said...

This is what I describe in this post: "Because of Immediate = true - we can open second tab without getting validation errors"

Andrejus

JoseAróstegui said...

Hi Andrejus,

Don't know what I'm doing wrong but I still get the not null validation messages. I've:

1) Downloaded the zip sample

2) Changed "SkipValidation" to both emplsViewPageDef and mainPageDef

3) Both Contact and Details showDetailItem have Immediate property to false.

Thanks a lot in advance.
Jose.

Andrejus Baranovskis said...

There is no need to change anything - skipValidation = true is already set on Employees fragment.

This sample was implemented with ADF 11g R2, which ADF version are you using?

Andrejus

Alex said...

Hi Andrejus,

I'm trying to use this solution with a series of Show Detail Headers (these are populated by an iterator). When I add a row and try to disclose the (empty) contents, I'm getting validation errors; even after following the steps described. Should this solution work for these Show Detail Header elements as well?

Using 11g R2.

Thanks,
Alex

Andrejus Baranovskis said...

It should work, I would need to test it.

Andrejus

Anonymous said...

Thank you for this.. really helpful :)

-adfBeginner

Anonymous said...

I have added SkipValidation="true" to my pageDef page and also I added immediate="true" and also immediate="false" to af:showDetailItem,but both are not working.For both case,I can't go to select tabs.
Can you please give idea?

Andrejus Baranovskis said...

Does it work when you test with my sample app?

Andrejus

Med Achraf said...

Hello Andrejus
I have a quite similar problem, but not exactly the same.

I ve 2 tabs on each one there are required filed. The submit button is outsite the af:paneltabbed.

Suppose I am on the first tab, I ve not inserted anything yet on the second one, I submit -> the required fileds of the second tab are escaped, no error message is displayed.

How can I make the submit button controls the required fields of both tabs.

I ve tried to put both tabs to immediate=true, I also tried childCreation=immediate on the panelTabbed: don't work

Thanks a lot for help

Andrejus Baranovskis said...

Did you try to use skipValidation=true as this post describes?

Andrejus

Shujauddin Khan said...

Hi Andrejus,

I have two tabs, one is having mandatory fields and the other showdetails tab is not having any mandatory fields.

Now the problem is when I try to submit the form from the second tab without giving the mandatory attribute on the first tab, it gets submitted.

I want to prevent this form submission by prompting some error message or something.

any help will be highly appreciated.

Aslam Anwer said...

Hi Andrejus.

Why we need skipValidations here, we dont' have any entitylevel validation.

i have run this project with skip validations = false.not finding any difference. Can you please tell what impact it has.

Thanks.

Andrejus Baranovskis said...

You should read blog more carefully. There are EO level validations - required attribute check. With skipValidation = false, it will report errors from other Tab, on Commit. Double check what I describe in the blog post.

Andrejus

łukaszG said...

hey Andrejus,

so what would be the preferred way of dealing with view level validation? Say, if I would set the "required = true" on one of the attributes and try to change tabs without giving it any value?
I have a case with rather complex form spread on multiple (nested) tabs and some of the fields become required depending on phase of some process.

Thanks,
lukasz

Andrejus Baranovskis said...

This is described in this post, set immediate=true for the tab and skipValidation=true in Page Def. Unless your case is different.

Regards,
Andrejus

łukaszG said...

Maybe I'm missing something then.. From how I understand your post "Immediate = true" will not solve described issue plus entered data is not stored on the Model.
I figured out, that setting the top-most tab containing the required fields as immediate actually helps (errors on empty fields are not show on tab change but the values go to the model on save just fine) yet I'm not quite sure why..
According to the doc setting "immediate = true" on components that invoke disclosure events will only pull conversion, validation and event delivery up to the 'Apply Request Values' phase so required fields with no value should throw an error..

So my satisfaction from getting this to work is not full ;)

Still, thanks for the post(s),
lukasz

Andrejus Baranovskis said...

Yes, this is why you need to set additionally skipValidation=true, as I describe in the post.

Andrejus

łukaszG said...

:) you got me confused, Andrejus.. In your post you are setting immediate back to false before setting "skipValidation = true". I, on the other hand, have "immediate = true" and "skipValidation = false" (I don't have any Model validation that needs skipping). Everything is working (almost) as should, I'm just trying to figure out why.

Andrejus Baranovskis said...

This post described to leave immediate as default and set skipValidation=true. I don't know about your use case, but in this case it works - I can navigate across tabs, without firing validation messages on tab disclosing.

To set immediate=true doesnt work well, as it will not submit values from the current tab, while moving to the next tab.

I hope it is more clear. I'm not sure about your code and use case, may be there is something else. It would require debugging :)

Andrejus