[SalesForce] Displaying error message besides the field responsible for occurrence of error from Apex code

I am trying to display a form that creates multiple Contact records from my Visualforce Page. I don't want to use inputField to achieve the same. The requirement is that if I do not enter LastName and click the Save button, the message should appear just besides the inputText field that I used to fill the LastName.

I cannot submit the form until all fields have been filled.

Right now the error message gets displayed against all the fields, even if I have filled all the fields, except one.

Markup

<apex:page Controller="AddTwoContactsController">
    <apex:form >
        <apex:pageBlock >
        <apex:pageBlockSection columns="1">

            <apex:pageBlockTable value="{!lstContacts}" var="con">
                <apex:column headerValue="Salutation">
                    <apex:inputField value="{!con.Salutation}"/>
                </apex:column>
                <apex:column headerValue="First Name">
                    <apex:inputText value="{!con.FirstName}" required="true"/>
                    <apex:messages />
                </apex:column>
                <apex:column headerValue="Last Name">
                    <apex:inputText value="{!con.LastName}" required="true"/>
                    <apex:messages />
                </apex:column>
            </apex:pageBlockTable>
            </apex:pageBlockSection>

            <apex:pageBlockSection columns="1">
            <apex:pageBlockTable value="{!lstContacts1}" var="con1">
                <apex:column headerValue="Salutation">
                    <apex:inputField value="{!con1.Salutation}"/>
                </apex:column>
                <apex:column headerValue="First Name">
                    <apex:inputText value="{!con1.FirstName}" required="true"/>
                    <apex:messages />
                </apex:column>
                <apex:column headerValue="Last Name">
                    <apex:inputText value="{!con1.LastName}" required="true"/>
                    <apex:messages />
                </apex:column>
            </apex:pageBlockTable>
             </apex:pageBlockSection>
            <apex:pageBlockButtons >

                <apex:commandButton value="Save Contacts" action="{!saveContact}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public with sharing class AddTwoContactsController {


   // public String con1 { get; set; }

    //public String Contact { get; set; }



    Contact ct = new Contact();
    public List<Contact> lstContacts{ get; set; }
    Contact ct1=new Contact();
    public List<Contact> lstContacts1{ get;set; }

    public AddTwoContactsController ()
    {
        lstContacts=new list<Contact>();
        lstContacts.add(ct);
        lstContacts1=new list<Contact>();
        lstContacts1.add(ct1);
    }
    public PageReference saveContact() {
        for(Integer i=1; i<=lstContacts.size(); i++)
        {
            upsert lstContacts;
        }
        for(Integer j=1; j<=lstContacts1.size(); j++)
        {
            upsert lstContacts1;
        }
        return Page.Allcontactssaved;

    }

The output looks like:

Screenshot

Best Answer

There are many available strategies. Probably the simplest is to use the required attribute:

<apex:inputText value="{!Contact.LastName}" required="true" />

The advantage there is that you do not require any Apex at all. It also works with inputText, whereas other strategies depend on using inputField.


Another option is to make the field required at the database level or use a Validation Rule:

ISBLANK(LastName)

Again, no Apex.


If you want to add an error to a specific field in Apex, you can as follows:

contactRecord.LastName.addError('error message');

Note that this functionality cannot be achieved dynamically.

Related Topic