You need to sign in to do that
Don't have an account?
makarandparab38
Displaying Error Message on VF page
Hi All
I have a VF page called as Home Page, with some fields on it. On click of submit button, apex controller class is executed, which has a validation method. I do some validation on the field, if the validation fails, i add error messages [ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'First Name is required field'));].
I want to display the error messages just below the fields on the Home Page. How to do that. I am able to display all the error messages together just above the first field on the form.
Please let me know
It might help to see your page.
Can you just use the "required" attribute in this case?
Hi
Thanks for thr response. But where should i use the "required" attribute.
Can you please elaborate on it if possible.
I come from a Java background where i have worked on struts framework. We have the same implemenation to display the error messages. Only difference is when we add Message object to Messages object we provide a name attribute like a key and the same key is used in the message tag on the front end.
Are the same fundamentals used here in apex also.
Regards
Makarand Parab
When you say you "have some fields on it" I presume you are using one of our standard input components like inputField or inputText. Each of these has an attribute named "required" that will, if set to true, automatically display a field-level error message if they don't supply a value.
This is why I asked if you could post your page/controller, so we can be more explicit with the recommended solution. As you are new it's not unlikely that there are additional improvements that could be recommended. Have you gone through the quick start tutorial in the developer guide?
Hi
The code for VF page is as follows
<apex:page controller="Lead" id="lead" title="SaaS" showHeader="false" sidebar="false" >
<html>
<head>
</head>
<body>
<apex:form >
<apex:pageBlock title="Enquiry">
<apex:pageBlockButtons >
<apex:commandButton value="Submit" action="{!submit}"/>
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockSection title="Enquiry Form - Please Enter All Your Details">
<apex:dataTable align="center" rows="6" var="lead" value="{!title}" border="0" columns="2" width="60%" cellpadding="2" cellspacing="2">
<apex:column >
<apex:outputLabel value="First Name:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="firstname" value="{!firstname}"/>
</apex:column>
<apex:column breakBefore="true">
<apex:outputLabel value="Last Name:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="lastname" value="{!lastname}"/>
</apex:column>
<apex:column breakBefore="true">
<apex:outputLabel value="Company Name:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="companyname" value="{!companyname}"/>
</apex:column>
<apex:column breakBefore="true">
<apex:outputLabel value="Email:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="email" value="{!email}"/>
</apex:column>
<apex:column breakBefore="true">
<apex:outputLabel value="Phone:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="phone" value="{!phone}"/>
</apex:column>
<apex:column breakBefore="true">
<apex:outputLabel value="Address:" style="font-size:16px"/>
</apex:column>
<apex:column >
<apex:inputText id="address" value="{!address}"/>
</apex:column>
</apex:dataTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</body>
</html>
</apex:page>
And code for Apex class is as follows
public class Lead
{
private String firstname = '';
private String lastname = '';
private String title = '';
private String companyname = '';
private String email = '';
private String phone = '';
private String address = '';
PageReference pageRef;
public Lead()
{
}
public String getFirstName()
{
return firstname;
}
public String getLastName()
{
return lastname;
}
public String getTitle()
{
return title;
}
public String getCompanyName()
{
return companyname;
}
public String getEmail()
{
return email;
}
public String getPhone()
{
return phone;
}
public String getAddress()
{
return address;
}
public void setFirstName(String strFirstName)
{
this.firstname = strFirstName;
}
public void setLastName(String strLastName)
{
this.lastname = strLastName;
}
public void setCompanyName(String strCompanyName)
{
this.companyname = strCompanyName;
}
public void setEmail (String strEmail)
{
this.email = strEmail;
}
public void setPhone (String strPhone)
{
this.phone = strPhone;
}
public void setAddress (String strAddress)
{
this.address = strAddress;
}
public PageReference submit()
{
CG_Contact__c cgcontact;
Database.SaveResult result;
if(checkValidation())
{
return null;
}
try
{
cgcontact = new CG_Contact__c ( First_Name__c = firstname,
Last_Name__c = lastname,
Company_Name__c = companyname,
Email__c = email,
Phone_Number__c = phone,
Address__c = address
);
result = Database.insert(cgcontact);
}
catch(Exception e)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'All the fields are required'));
return null;
}
if(!result.isSuccess())
{
pageRef = new PageReference('/apex/HelloWorld');
cgcontact.addError('Couldn\'t create new CG Contact!');
pageRef.setRedirect(true);
return pageRef;
}
pageRef = new PageReference('/apex/ThankYou');
pageRef.setRedirect(true);
return pageRef;
}
private boolean checkValidation()
{
boolean retFlag = false;
if(firstname == null || firstname.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'First Name is required field'));
retFlag = true;
}
if(lastname == null || lastname.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Last Name is required field'));
retFlag = true;
}
if(companyname == null || companyname.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Company Name is required field'));
retFlag = true;
}
if(email == null || email.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Email is required field'));
retFlag = true;
}
if(phone == null || phone.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Phone is required field'));
retFlag = true;
}
if(address == null || address.equals(''))
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Address is required field'));
retFlag = true;
}
return retFlag;
}
}