+ Start a Discussion

Problem with validation on server side and client side

Hi All,


I have developed a vf page in which once a user chooses a pricebook , he will be able to see the list of products tied to that pricebook in a pageblock table. In order to accomplish this I have used the wrapper class which has a checked field and some input field along with fields from PRoduct2 object.


I have two options to write a validation to check the quqntity is not 0 if the product checkbox is checked .


1) From javascript ---client side validation:


I want to write a validation in javascript , when the checkbox is checked i.e that product is selected, I need to validate the quantity column in th table and alert if the quantity is 0.


When I read the checkbox field from javascript though it is checked or unchecked I am getting  a value of null always :


alert(document.getElementById('{!$Component.theForm.ProductDetail.ProductDetails.table.selectLine1}'));---- null
and  alert(document.getElementById('{!$Component.selectLine1}')); ------null



2) From Apex controller ---server side validation


I ama ble to read the values from the apex class perfectly fine i.e, checked as false initially and once user selects the product it is true.


And if the product is checked and the quantity field is 0 I am displaying a error message like below:


ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Please Enter Quantity  for the Product Selected!!') );
                     return null; ---------> When I return this pagereference  all the information enters by the user disappears and the page is clear of all the user inputted data.


How can I retain the information entered by the user and display a validation message .


Any help on client side or server side validation is highly appreciated.








For option 2, are you using an action to validate? If you have the data stored in non-transient / non-static variables, it should still be there. It might help if you post your controller code here.



Below are the code snippets from page and controller:


From a commandButton action I am invoking a controller method.

<apex:commandButton value="Select" action="{!productSelected}"/>


// Called from the  Page on click of Select button
     public PageReference productSelected() {  // Here i am performing a check for validation and if the input fields are properly filled in there are dml operations performed

        for(productWrapper prod: productsWrapList) {
                if(prod.checked == true) {
                    if(prod.quantity ==0){
                                              ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Please Enter Quantity  for the Product Selected!!') );
                                             return null;  // Want to stay on the same page but here the user entered input values are cleared from the table i.e if othe input fields are filled in before validation they are blank after validation is perfomed
                     }                 }


Any help is appreciated.




Snippets seem ok to me. May need more complete listings to see what is wrong.


After performing the validation if everything is ok, I am getting the product records from the pageblock table and creating opptyline items with quantity,pricebookentryid,salesprice.


As I ahev mentioned the problem is when I say return null, all the user inputted values in the pageblock table are cleared off. If I do not add the return null the error messages are not displayed in the vf page.


Let's say I have checked a checkbox entered discount, calculated salesprice etc and my validation  kicks in after the click of a button, it performs the validations and displays error messages on the vf page and all the values entered are cleared off. that is the checkbox is cleared and the discount is zero etc.


Hope I have explained the problem I am facing.


Any help is appreciated.