+ Start a Discussion
dwwrightdwwright 

required attribute not working correctly on apex:selectList

I'm trying to create a picklist using <apex:selectList>, populate it with my own selections, and require the field to contain a value. Here's my code:

 

 

<apex:selectList required="true" size="1" value="{!SDQA__c.Inspector_Stamp1__c}" > <apex:selectOption itemLabel="{!SDQA__c.Inspector_Stamp1__c}" itemValue="{!SDQA__c.Inspector_Stamp1__c}" /> <apex:selectOption itemLabel="{!$User.FirstName} {!$User.LastName}" itemValue="{!$User.FirstName} {!$User.LastName}" /> <apex:selectOption itemLabel="N/A" itemValue="N/A" /> </apex:selectList>

 

The first value defaults to whatever the field currently contains (which will initially be null). The other two options are either the users First Name+Last Name, or N/A. The problem is that even though I have required="true", there is no validation enforced on this field (The red "required" bar doesn't even show up beside it). Anybody have an idea as to why that is?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ron HessRon Hess

try adding < apex : pageMessages />

 

and you can see that validation is performed

 

 

Select List is not an input field component, so it won't get the red bar , ever.

All Answers

WesNolte__cWesNolte__c

Hey

 

No validation at all? You won't get the red bar (unfortunately) although there is some trickery you can use to fix this.. If you submit the form with a null value for that field you don't get an error message? Does it save/update a record, and if you view that record post-save what is the value of that field?

 

Wes 

Ron HessRon Hess

try adding < apex : pageMessages />

 

and you can see that validation is performed

 

 

Select List is not an input field component, so it won't get the red bar , ever.

This was selected as the best answer
dwwrightdwwright
Thanks Ron, you were correct--the validation is being applied. I was looking for it to appear at the field level rather than as a page message. Is there a way to alter the error message so it's not cryptic (j_id0:j_id28:SDQA180:j_id99: Validation Error: Value is required is what I'm getting, that won't make much sense to my users)
Message Edited by dwwright on 03-01-2010 07:54 AM
WesNolte__cWesNolte__c

Hey

 

Unfortunately the message cannot be really friendly, you can make it a bit more readable by assigning human-friendly ids to all your elements e.g.

 

 <apex:page id="thePage">

  <apex:pageBlock id="theBlock">

... 

   <apex:pageBlockSectionItem id="ToUser">

       <apex:outputLabel >Transfer To User:</apex:outputLabel>

       <apex:outputPanel layout="block" styleClass="requiredInput">

       <apex:outputPanel layout="block" styleClass="requiredBlock"/>

       <apex:selectList value="{!toUserID}" size="1" required="false"  id="toUserID">

           <apex:selectOptions value="{!ToUsers}"></apex:selectOptions>

       </apex:selectList>

       </apex:outputPanel>

   </apex:pageBlockSectionItem> 

...

 

would result in the field being called 'thePage:theBlock:ToUser:toUserId', but that's as good as it gets (for now). The two lines highlighted in red will also give you the required red bar you asked about.. a bit of a hack but it's nice for consistency.

 

Wes 

WesNolte__cWesNolte__c

Hey

 

I've just posted an article with code on how to enhance form validation in VisualForce (using jQuery). There's an example too.

 

Wes 

rushrush

 

Thank You! for the red mark suggestion

ceptes softwareceptes software

Thanks for this tricky. It is working perfectly.