function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
developer-forcedeveloper-force 

Validation Message not showing on InputField

I have a custom object MyObject__c. I have created a validation rule for a field in the custom object such as amount__c < 0. I have defined the field in VisualForce page as follows
Code:
<apex:inputField id="amount" value="{!myObject.amount__c}"/> 

 
When I submit the validation work but it does not show the error message similar to what it does in default validation for required rule.

Message Edited by developer-force on 10-14-2008 10:02 AM

Message Edited by developer-force on 10-14-2008 10:03 AM
amar joshiamar joshi
i m guessing

may be in your command button code like
 <apex:commandbutton immediate="true"/>
if it is then remove immediate="true"
developer-forcedeveloper-force
Hi Amar,

I don't have immediate="true" in my command button. The standard validation are performed. Also the validation is being performed but the message is not displayed.

I observed that if I use <apex:messages /> component it displays the message as expected.


dchasmandchasman
In your validation rule have you specified that the Error Location = Field?
developer-forcedeveloper-force
Yes I have specified Error Location as Field in the validation.
developer-forcedeveloper-force
I have also seen that some of my custom object field which are required(built-in validation for Custom Object fields) are not displaying the error message when the validation for required fails.

Below is the VisualForce page code and the custom controller code associated with it
The pageBlockSection with id "consignor_group" is the Consignor custom object. The consignor_name__c  is required in the Consignor object. It performs the validation but does not show the error message when validation fails.

As I mentioned in my earlier post using the <apex:messages> component displays the message. So don't have a clue is to what I am doing wrong. I appreciate all the help.

Visual Force Page code:
Code:
<apex:page controller="ConsignmentController" sidebar="false" tabStyle="Consignment__c">
 <apex:sectionHeader title="Consignment Booking" />
     <apex:form >
         <apex:pageBlock >     
             <apex:pageBlockSection id="consignor_group" title="Consignor" collapsible="true" columns="2">
                 <apex:inputField id="consignor_name_id" value="{!consignor.consignor_name__c}" />
                 <apex:inputField id="consignor_phone_id" value="{!consignor.consignor_phone__c}" />   
                 <apex:inputField id="consignor_mobile_id" value="{!consignor.consignor_mobile__c}" />  
                 <apex:inputField id="consignor_address_id" value="{!consignor.consignor_address__c}" />    
             </apex:pageBlockSection>
             <apex:pageBlockSection id="consginee_group" title="Consignee" collapsible="true" columns="2">
                 <apex:inputField id="consignee_name_id" value="{!consignee.consignee_name__c}"/>
                 <apex:inputField id="consignee_phone_id" value="{!consignee.consignee_phone__c}" />
                 <apex:inputField id="consignee_mobile_id" value="{!consignee.consignee_mobile__c}" />
                 <apex:inputField id="consignee_address_id" value="{!consignee.consignee_address__c}" />
                 <apex:inputField id="consignee_pincode_id" value="{!consignee.consignee_pin_code__c}" />
             </apex:pageBlockSection>
             <apex:pageBlockSection id="consignment_group" title="Consignment Details" collapsible="true" columns="2">                 
                 <apex:inputField id="consignment_number_id" value="{!consignment.consignment_number__c}" />
                 <apex:inputField id="consignment_amount_id" value="{!consignment.consignment_amount__c}"/>
                 <apex:inputField id="consignment_date_id" value="{!consignment.booking_date_time__c}" />
                 <apex:inputField id="consignment_weight_id" value="{!consignment.consignment_weight__c}" />                 
                 <apex:pageBlockSection columns="3" title="Destination details" collapsible="false">                 
                     <apex:pageBlockSectionItem >
                        <apex:outputLabel for="consignment_city_id">City</apex:outputLabel>
                             <apex:actionRegion > 
                            <apex:selectList id="consignment_city_id" value="{!consignment.city_id__c}" size="1">
                                <apex:selectOptions value="{!lookupCities}"/>
                                <apex:actionSupport event="onchange" rerender="consignment_branch_id" status="loadingStatus_id"/>                       
                            </apex:selectList>
                            </apex:actionRegion>
                     </apex:pageBlockSectionItem>                    
                     <apex:pageBlockSectionItem >
                         <apex:outputLabel for="consignment_branch_id">Branch</apex:outputLabel>
                          <apex:actionRegion >                      
                         <apex:selectList id="consignment_branch_id" value="{!consignment.branch_id__c}" size="1">                          
                                <apex:selectOptions value="{!lookupBranches}"/>
                                <apex:actionSupport event="onchange" rerender="areacode_id" status="loadingStatus_id"/>
                        </apex:selectList>
                         </apex:actionRegion> 
                     </apex:pageBlockSectionItem>
                     <apex:pageBlockSectionItem >
                         <apex:outputLabel for="areacode_id">Area Code</apex:outputLabel>                     
                         <apex:outputText id="areacode_id" value="{!areaCode}" />   
                     </apex:pageBlockSectionItem>
                     <apex:actionStatus id="loadingStatus_id" startText="Loading..." startStyle="background-color:red;color:#FFFFFF;font-style:italic;"/>
                  </apex:pageBlockSection>                                       
             </apex:pageBlockSection>                     
             <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" />
                 <apex:commandButton value="Save & New" action="{!saveAndNew}" />
             </apex:pageBlockButtons>
            </apex:pageBlock>
     </apex:form>       
</apex:page>

Controller code:
Code:
public class ConsignmentController {
    Consignor__c consignor;
    
    Consignee__c consignee;
    
    Consignment__c consignment;
    
    public String multiple {get; set;}
        
    Service.ILookupService lookupService;
    Service.IConsignmentService consignmentService;
    
    public ConsignmentController() {
        lookupService = ServiceFactory.newLookupService();
        consignmentService = ServiceFactory.newConsignmentService();
    }
    
    private void saveData() {
        Status__c status = StatusDao.findById('B');
        consignment.status_id__c = status.id;
        String consignorId = ApexPages.currentPage().getParameters().get('consignor');
        System.debug('Method saveData():' + consignorId);
        if(consignorId == null || consignorId == '') {
            insert consignor;
        }       
        insert consignment;        
        ConsignorConsignment__c consignorConsignment = new ConsignorConsignment__c();
        consignorConsignment.consignment_id__c = consignment.id;
        consignorConsignment.consignor_id__c = consignor.id;
        insert consignorConsignment;
        consignee.consignment__c = consignment.id;
        insert consignee;
    } 
    
    public PageReference save(){
        System.debug('invoked save method');   
        try {
            saveData();
        }catch(DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return reset(false);
    }
    
    public PageReference saveAndNew() {
        System.debug('invoked saveAndNew method');   
        try {
            saveData();
        }catch(DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
        return reset(true);
    }
    
        
    public Consignment__c getConsignment() {        
        System.debug('Method:getConsignment()');
        if(consignment == null) {            
            consignment = new Consignment__c();
        }
        return consignment;
    }
    
    public Consignor__c getConsignor() {
        String consignorId = ApexPages.currentPage().getParameters().get('consignor');
        System.debug('Method getConsignor()' + consignorId);
        if(consignorId != null || consignorId != '') {
            consignor = ConsignorDao.findById(consignorId);
        }
        if(consignor == null) {
            consignor = new Consignor__c();
        }
        return consignor;
    }
    
    public Consignee__c getConsignee() {
        if(consignee == null) {
            consignee = new Consignee__c();
        }
        return consignee;
    }    
    
   
    public List<SelectOption> getLookupCities(){        
        return lookupService.getCities();
    }
    
    public List<SelectOption> getLookupBranches() {
        return lookupService.getBranches(consignment.city_id__c);
    }
    
    public String getAreaCode() {
        String areaCode = 'N.A.';
        Branch__c branch = BranchDao.findById(consignment.branch_id__c);
        if(branch != null) {
            areaCode = branch.area_code__c;
        }
        return areaCode; 
    }
    
    public PageReference reset(Boolean saveAndNew) {
        PageReference pageRef = Page.booking;
        pageRef.setRedirect(true);        
        if(saveAndNew) {    
            pageRef.getParameters().put('consignor', consignor.id);
        }
        return pageRef;
    }
}

 

 



Message Edited by developer-force on 10-16-2008 07:38 AM

Message Edited by developer-force on 10-16-2008 07:39 AM
dchasmandchasman
There is a fix in the update being deployed tonight that should improve some or all of this
developer-forcedeveloper-force
My built in validation for required started to work again. But the additional validation rules added by me are not yet displaying the message when validation fails.

Message Edited by developer-force on 10-18-2008 12:39 AM
dchasmandchasman
This is a known limitation of the API that VF relies on. Basically the api only returns the first validation failure which results in only the first failure message being displayed. Correcting this is on the VF product backlog - no ETA just yet on this but I do not expect this to be supported until at least the next major release.
developer-forcedeveloper-force
Well returning the first failed validation is fine, but the problem is that if I have a validation like  for example below (from Consignment__c.object file)
Code:
     <validationRules>
        <fullName>weight_validation</fullName>
        <active>true</active>
        <errorConditionFormula>consignment_weight__c &lt;0</errorConditionFormula>
        <errorDisplayField>consignment_weight__c</errorDisplayField>
        <errorMessage>should not be less than 0.00</errorMessage>
    </validationRules>

I expect it to appear near the Field in the form as I have selected the Location of the Error to be Field in the validation rule. But this does not work. In order to get the message displayed I have used the <apex:messages> component. This display the error but its behavior is not what I excepted. It displays the validation message as below.
  • Weight:should not be less than 0.00
  • You must enter a value
  • You must enter a value
The "You must enter a value" message is for the standard required validation of the Custom object, which also is displayed near the Input field in the form in red font Error: You must enter a value as expected.

I was looking at <apex:messages> component as my work around for my validation message not being displayed near the field in the form as defined.

If I select the validation to appear at location Field, should it be displaying the message very next to the field in the form? If yes this is not happening in my case and what should be the  work around?

Thanks a lot for all the help and support I really appreciate the effort taken to answer my queries

Cheers

 
 



Message Edited by developer-force on 10-19-2008 02:56 PM