You need to sign in to do that
Don't have an account?
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?
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
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
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.
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
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
Thank You! for the red mark suggestion
Thanks for this tricky. It is working perfectly.