Activity Forums Salesforce® Discussions Adding error messages to nonfield inputs

  • Adding error messages to nonfield inputs

    Posted by Ravi on March 29, 2016 at 9:47 am

    I am creating a Visualforce page to allow a user to create or edit a contact
    record. The contact standard controller and a controller extension manage the page. The ID
    of the account that the contact is associated with is entered via an apex:selectList
    component, which is bound to a controller property rather than an sObject field.

    I want to display an error message under the apex:selectList component if the user does not select an account to associate with the contact.

    AddError

    • This discussion was modified 8 years, 1 month ago by  Ravi.
    • This discussion was modified 8 years, 1 month ago by  Ravi.
    • This discussion was modified 8 years, 1 month ago by  Ravi.
    • This discussion was modified 8 years, 1 month ago by  Ravi.
    • This discussion was modified 8 years, 1 month ago by  Ravi.
    • This discussion was modified 7 years, 10 months ago by  Forcetalks.
    • This discussion was modified 6 years, 5 months ago by  Forcetalks.
    shariq replied 5 years, 7 months ago 4 Members · 3 Replies
  • 3 Replies
  • Satya

    Member
    March 29, 2016 at 10:16 am
  • Parul

    Member
    September 19, 2018 at 9:14 pm

    Hi,

    I used below logic for my same requirement:

    public class Wrapper
    {
    public String FirstName {get; set;}
    public String MiddleName {get; set;}
    public String LastName {get; set;}
    public String Email {get; set;}

    public String FirstNameError {get; set;}
    public String MiddleNameError {get; set;}
    public String LastNameError {get; set;}
    public String EmailError {get; set;}

    public Wrapper()
    {
    FirstNameError='';
    MiddleNameError='';
    LastNameError='';
    EmailError='';
    }
    }

    Validation in save method is:-

    for (Wrapper wrap : wrappers)
    {
    if ( ((null==wrap.FirstName) || (wrap.FirstName.trim().length()==0)) &&
    ((null==wrap.MiddleName) || (wrap.MiddleName.trim().length()==0)) )
    {
    wrap.FirstNameError='Either first name or middle name must be defined';
    wrap.MiddleNameError='Either first name or middle name must be defined';
    }
    }

    And in visualforce page:-

    <apex:column headerValue="First Name">
    <apex:inputText value="{!wrapper.FirstName}" rendered="{!LEN(wrapper.FirstNameError)==0}"/>
    <apex:outputPanel rendered="{!LEN(wrapper.FirstNameError)!=0}">
    <apex:inputText styleClass="error" value="{!wrapper.FirstName}"/>
    <div class="errorMsg"><strong>Error:</strong>&nbsp;{!wrapper.FirstNameError}</div>
    </apex:outputPanel>
    </apex:column>

    Hope this helps.

    Thanks.

  • shariq

    Member
    September 19, 2018 at 10:31 pm

    Hi,

    I have similar example try this-

    Vf code - 

    <apex:page controller="q204269">
    <apex:form>
    <apex:pageBlock>
    <apex:pageBlockSection columns="1">
    <apex:inputField value="{!record.Name}" />
    </apex:pageBlockSection>
    <apex:pageBlockButtons>
    <apex:commandButton action="{!save}" value="Save" />
    </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Controller

    public class Cont{
    public Account record { get; set; }
    public Cont() {
    record = new Account();
    }
    public void save() {
    if(record.Name == null) {
    record.Name.addError('You must enter a value!');
    }
    }
    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos