3

I have a custom object Team member contains four text fields First Name , Surname, location and job title and First Name and Surname is a required fields (When i create them i make them required).

and I have a Controller like :

public class TeamMemberController {
    private Team_Member__c teamMember;    
    public TeamMemberController (ApexPages.StandardController stdController) {
        this((Team_Member__c) stdController.getRecord());
    }

    private TeamMemberController (Team_Member__c teamMember) {
        this.teamMember = teamMember;
    }

    public Team_Member__c GetTeamMember(){
        return teamMember;
    }    

    public List<String > GetFieldsNames () {
        List<String> myList = new List<String>();
        myList.add('Job_Title__c');
        myList.add('Location__c');
        return myList;
    }
}

and VF page like :

<apex:page standardController="Team_Member__c" extensions="TeamMemberController">
    <apex:form >
        <apex:pageMessages />
         <apex:pageBlock title="{!teamMember.name}" mode="edit">
         <apex:pageBlockButtons >
                <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
                <apex:commandButton action="{!save}" value="Save" />
          </apex:pageBlockButtons>
          <apex:pageBlockSection title="Team Member Details" columns="1">
                   <apex:inputField value="{!teamMember.First_Name__c}" style="width:250px;" />
                   <apex:inputField value="{!teamMember.Surname__c}" style="width:250px;" />
                 <apex:repeat value="{!fieldsNames}" var="myTeamMemberFieldName">
                     <apex:pageBlockSectionItem >
                        <apex:outputLabel value="{!myTeamMemberFieldName}" />
                     <apex:inputText id="myInputField" value="{!teamMember[myTeamMemberFieldName]}" 
                                        required="true" />
                    </apex:pageBlockSectionItem>
                 </apex:repeat>
               </apex:pageBlockSection>
         </apex:pageBlock>
      </apex:form>         
</apex:page>

but when i run this code , and do the below steps, I found that I can't submit the form even if i entered a data in the field as the validation doesn't see the field value.

Steps :

  1. Fill the first name and surname
  2. Submit the form, you will find error saying you have to enter the job tile only without mentioning the location although both of them are required.
  3. Enter some data into the job title and press submit
  4. The validation error will be thrown saying Job title is required although there is a value on the field.

enter image description here

2

Try adding the label attribute to your inputText and the errors should show up correctly. From the docs starting with v.23:

<apex:inputText id="myInputField" value="{!teamMember[myTeamMemberFieldName]}" 
                                    required="true"  label="{!myTeamMemberFieldName}"/>

A text value that allows to display a label next to the control and reference the control in the error message

  • I think OP wants to use inputText, not inputField in your code sample – cropredy Oct 26 '14 at 22:26
  • Oops I grabbed wrong line but it works for input text I'll fix it up. Thanks. – Jenny B Oct 26 '14 at 22:37
1

My first thought was, that the reason for this behavior is that you use an id="SomeStaticText" in your apex:repeat and you could get implicitly duplicate ids on your page. But it's actually not true and in my case the platform has generated this markup for the Job_Title__c:

<input id="j_id0:j_id1:j_id28:j_id32:j_id35:0:j_id36:myInputField" type="text" name="j_id0:j_id1:j_id28:j_id32:j_id35:0:j_id36:myInputField">

For the Location__c it generated only a slight difference (id35:0 vs. id35:1) but should be good enough:

<input id="j_id0:j_id1:j_id28:j_id32:j_id35:1:j_id36:myInputField" type="text" name="j_id0:j_id1:j_id28:j_id32:j_id35:1:j_id36:myInputField">

I also tried to omit the id, but getting the same strange behavior. To set the id dynamically like id="{!myTeamMemberFieldName}" is not allowed, since the id must be a literal value.

But what actually does work is this:

Don't use apex:inputText, use again apex:inputField inside of the repeat. This should come very close to what you want to achieve, I guess.

<apex:page standardController="Team_Member__c" extensions="TeamMemberController">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock title="{!teamMember.name}" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Team Member Details" columns="1">
                <apex:inputField value="{!teamMember.First_Name__c}" style="width:250px;" />
                <apex:inputField value="{!teamMember.Surname__c}" style="width:250px;" />
                <apex:repeat value="{!fieldsNames}" var="myTeamMemberFieldName">
                    <apex:inputField   value="{!teamMember[myTeamMemberFieldName]}"  style="width:250px;" required="true" />
                    <!--<apex:pageBlockSectionItem >-->
                    <!--    <apex:outputLabel value="{!myTeamMemberFieldName}" />-->
                    <!--    <apex:inputText   value="{!teamMember[myTeamMemberFieldName]}" required="true" />-->
                    <!--</apex:pageBlockSectionItem>-->
                </apex:repeat>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>         
</apex:page>

As a small candy, you get proper red bars in front of the inputs to indicate that the fields are required and the validation seems to work just fine:

enter image description here

apex:inputText

In my opinion what you are trying with the inputText should work, too. Since it reproducibly fails, at this point, I would consider it as a flaw of the platform, unless someone else here tells us different.

  • Thanks for your reply I know that <apex:inputField> works perfect with the validation but if i'm using a field like a lockup field instead of text field and I want this field to looks like in a Auto-complete text box so and if that field is required that where i will have the issue again as i can't use the input field which will be interpreted to a different html elements. I hope that salesforce can fix this issue soon as i'm using it on a lot of cases. – Amgad Souleiman Oct 23 '14 at 18:35
0

Thanks Jenny B :), Your solution works perfect so the final page will looks like :

So the final solution is VF page will looks like :

<apex:page standardController="Team_Member__c" extensions="TeamMemberController">
<apex:form >
    <apex:pageMessages />
     <apex:pageBlock title="{!teamMember.name}" mode="edit">
     <apex:pageBlockButtons >
            <apex:commandButton action="{!cancel}" value="Cancel" immediate="true" />
            <apex:commandButton action="{!save}" value="Save" />
      </apex:pageBlockButtons>
      <apex:pageBlockSection title="Team Member Details" columns="1">
               <apex:inputField value="{!teamMember.First_Name__c}" style="width:250px;" />
               <apex:inputField value="{!teamMember.Surname__c}" style="width:250px;" />
             <apex:repeat value="{!fieldsNames}" var="myTeamMemberFieldName">
                 <apex:pageBlockSectionItem >
                    <apex:outputLabel value="{!myTeamMemberFieldName}" />
                 <apex:inputText id="myInputField" value="{!teamMember[myTeamMemberFieldName]}" label="{!myTeamMemberFieldName}"
                                    required="true" />
                </apex:pageBlockSectionItem>
             </apex:repeat>
           </apex:pageBlockSection>
     </apex:pageBlock>
  </apex:form>         

Although you are passing the field name as Output label which will show the field name next to the input text, the label field at the input text is required to get the validation working.

Thanks for help.

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.