0

Consider I have 4 fields. Field 1, Field 2, Field 3, Field 4. Field 3 is displayed(rendered) on the page based on the selections of Field 1 and Field 2. Field 4 is displayed based on some selections of Field 1 and Field 2. And both Field 3 and 4 are displayed based on some other selections of first 2 fields. Now I want to validate all those fields and if all fields are selected then a alert box should be displayed.

Now, I don't want to use required as true as it is giving me rendering issues. I using apex for logic based on which the fields are displayed and I am having save method in apex. The main issue I am having with is when I click submit button the validation rule fails or the alert box doesn't show up. If anyone can show me a sample example I would really appreciate it. The alert box should appear only when all picklists fields are selected and then saved.

1

There are a few ways to approach this. You can't use layout-side validation (e.g., required="true"), because you may not always have all values in all fields. I'm going to assume "apex" means validation in the apex controller and not in apex triggers (following an insert operation) or in validation rules (fired during a DML operation).

You also don't mention if the actionFunction or commandButton that kicks off the validation process redirects to a new page, or stays on the same page. I'm going to assume it stays on the same page (by returning null or something).

The easiest way to do this is actually with an instead of trying to do it with a javascript alter box. Just do something like:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Validation Ok!'));

and then on the Visualforce side you simply embed a

<apex:pageMessages/>

in the area that you are rerendering once the validation is complete.

Edit 1:

To show an alert box, first set a Boolean property on the controller side. Let's say "showAlert" as an example. Then, on the page side, when the rerender action is complete it will either include or exclude the following code:

<apex:outputPanel layout="none" rendered="{!showAlert}">
    <script type="text/javascript">
    alert('{!showAlertMessage}');
    </script>
</apex:outputPanel>

What happens here is that the "showAlert" boolean control whether the script tag gets rendered to the page or not. If false, no script. If true, an alert message..

  • I actually need an alert box, when all values are selected that is how I want it, I am using command button. Thanks. – cartman Feb 11 '14 at 2:34
  • As I understand you need other solution? Because as I can see it this fit your requiments... – Artur Kępczyński Feb 11 '14 at 5:47
  • Thanks, the edit actually works. I cannot use apex add message because I am rerendering the whole page each time the field is changed. I didn't even actually think about Boolean variable. – cartman Feb 11 '14 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.