1

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

1
  • If you could post the vf page code here it will help us to provide more concrete solutions. May 6 '16 at 15:20
4

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.

4
  • Your first option , I think, has some things you have to workaround (1) getting the red bar to display (or similar indicator), and (2) Getting the error message to not refer to the ugly VF HTML id field for the VF component (e.g. use label= attribute)
    – cropredy
    May 6 '16 at 19:20
  • @cropredy Aren't those mitigated at least somewhat by using html-5.0?
    – Adrian Larson
    May 6 '16 at 19:21
  • geez - you may be right; in my orgs, all the pgs are still HTML 4.x - I'll have to try that!
    – cropredy
    May 6 '16 at 19:30
  • Lol stone ages! Can't believe they haven't just made it the default yet /smh. At least they don't still support IE6. @cropredy
    – Adrian Larson
    May 6 '16 at 19:33

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.