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
YVYV 

Why i am able to save records through visualforce page even there are some required fields and some have active validation rule ?

  1.  I created a Visualforce page and added standard controller position__c object. There are some required fields like name,min_pay and max_pay. Min and max pay both have validation rule to check the minimum value criteria.  Without entering value in input text on visualforce page on those fields why i am able to save records.
This is my visualforce page code

<apex:page standardController="Position__c">
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock title="Position"> 
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="save"/>
                <apex:commandButton action="{Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Create records">
                <apex:inputText value="{!position__c.name}" />
                <apex:inputText value="{!position__c.min_pay__c}" />
                <apex:inputText value="{!position__c.max_pay__c}" />
                <apex:inputText value="{!position__c.status__c}" />
                <apex:inputText value="{!position__c.job_description__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

User-added image

if i edit this page and save without do anything it will also accept values and will take $0.0 value in min and max pay fields

Please help me
Thank you in advance 
sakhisakhi
Hi yagu ,

As you are using standard controller ,below is what you should check out .

https://help.salesforce.com/articleView?id=000331023&mode=1&type=1

But this can be done with extensions and custom controllers .
Below is code for reference
<apex:page standardController="Account" extensions="ErrorMessageInVfController">
 <apex:form >
   <apex:pageblock >
      <apex:pageMessages id="showmsg"></apex:pageMessages>
         <apex:panelGrid columns="2">
           Account Name: <apex:inputText value="{!acc.name}"/>
           Account Number: <apex:inputText value="{!acc.AccountNumber}"/>
           Account Phone: <apex:inputText value="{!acc.phone}"/>
           Account Site: <apex:inputText value="{!acc.site}"/>
           Account Industry: <apex:inputText value="{!acc.industry}"/>
           <apex:commandButton value="Update" action="{!save}" style="width:90px" rerender="showmsg"/>
         </apex:panelGrid>
    </apex:pageblock>
 </apex:form>
</apex:page>
Apex class :
public with sharing class ErrorMessageInVfController {
    public Account acc{get;set;}
    public ErrorMessageInVfController(ApexPages.StandardController controller) {
        acc = new Account();
    }
 
    public void save(){
      if(acc.name == '' || acc.name == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
 
      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
 
      if(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));
 
      if(acc.site == '' || acc.site == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
 
      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));
 
    }
}


 
Maharajan CMaharajan C
Hi Yagu,

Because you are using the apex:inputText . If you use <apex:inputField> then it will automatically consider the field properties. 

If you want to use the <apex:inputText> then you have to write the extenson controller.

Please try the below and let me know . 
<apex:page standardController="Position__c">
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock title="Position"> 
            <apex:pageBlockButtons>
                <apex:commandButton action="{!save}" value="save"/>
                <apex:commandButton action="{Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Create records">
                <apex:inputField value="{!position__c.name}" />
                <apex:inputField value="{!position__c.min_pay__c}" />
                <apex:inputField value="{!position__c.max_pay__c}" />
                <apex:inputField value="{!position__c.status__c}" />
                <apex:inputField value="{!position__c.job_description__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,
Maharajan.C
YVYV
hi Maharahan.C 

I tried all the things like i used inputfields and also use <apex:messages/> on pageBlock but it didn't work out.
Please help somebody i stuck here and confused
 
mukesh guptamukesh gupta
Hi Yagu,

Please use below code:
<apex:page standardController="Position__c">    
<apex:pageMessages id="errormsg" />
	<apex:form>
        <apex:pageMessages/>
        <apex:pageBlock title="Position"> 
            <apex:pageBlockButtons>
			 <apex:commandButton value="Save" action="{!savePosition}" reRender="errormsg"/>

             <apex:commandButton action="{Cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Create records">
                <apex:inputText value="{!position__c.name}" />
                <apex:inputText value="{!position__c.min_pay__c}" />
                <apex:inputText value="{!position__c.max_pay__c}" />
                <apex:inputText value="{!position__c.status__c}" />
                <apex:inputText value="{!position__c.job_description__c}" />
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex class:-
public with sharing class Youclass {
 
    public Position__c possition{get;set;}
     
    public StudentExt(ApexPages.StandardController controller) {
        possition = (Position__c)controller.getRecord();
    }
     
    public Pagereference savePosition() {
        try {
            Upsert possition;
            return new Pagereference('/' + possition.Id);
        }
        catch(DMLException de) {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, de.getDmlMessage(0)));
            return NULL;
        }
        catch(Exception e) {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, e.getMessage()));
            return NULL;
        }
    }
}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
YVYV
Thank you Mukesh
I tried your code but there are some errors. and i am unable to solve those errors.