function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TehNrdTehNrd 

Dependent Picklists and required fields

So it took me less than 10 minutes to identify an issue with the new depending picklists in Visualforce. What if I want to make the child picklist required only if the picklist is active and has selectable options?

 

 

 

<apex:page standardController="Lead">
    <apex:form>
        <apex:inputField value="{!lead.EMEA_Country__c}"/>
        <apex:inputField value="{!lead.EMEA_Distributor__c}" required="true"/>
        <apex:commandButton value="save" action="save"/>
    </apex:form>
</apex:page>

 

 

 

Hmm. I feel like this shouldn't be required if the field is disabled.

 

Thoughts?

 

-Jason

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
stephanstephan

Thanks for bringing this up Jason. We actually weren't aware that standard pages handle this case in a special way. Certainly if standard pages handle this, then we want to be consistent across the board. While I can't promise when we'll be able to get this change in, we'll certainly strive to do so as soon as we can.

 

...stephan

 

 

All Answers

TehNrdTehNrd

And before anyone recommends this..... yes, you could bind a value for every parent field to a child value such as 'None' but this is sort of redundant, isn't good design, and provides a poor UX.

rtuttlertuttle

You could bind a value for every parent field to a child value such as 'None'.

 

Just kidding.

 

Yea this seems a bit lacking.  Did they give us the ability to see the size of the list?  ie:  rendered="{!fieldname.size>0}"

I haven't played with dependent picklists in vforce yet.  Seems like quite a big hole here.

 

 

-Richard

TehNrdTehNrd

Haha, didn't see who responded and when I read that first line I thought, "Oh my goodness, are you serious....".

 

The documentation doesn't mention anything about determining the size or if the field is active.

 

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_quick_start_dependent_picklists.htm

AvromAvrom

Hi Jason,

 

Well, "required" traditionally doesn't mean conditioinally required. If null might be the only possible value for a field, then the field shouldn't be marked as not accepting nulls.

 

What I'd suggest, in this case, is that you treat this like any other case of a conditional requirement. There are several ways you could implement that:

 

-As a validation rule

-As some custom Apex on your form submission action

-As some Javascript that fires before your form is submitted.

 

The main thing is that any of these would involve an if/else construct of some sort--the dependent field's only required if its controlling field has one of a particular set of values.

 

AvromAvrom

Unfortunately, you can't determine whether the list is active on the server-side, because this is figured out in the browser when the user selects a value for the controlling picklist. It's not like an ActionSupport; there's no server round-trip here: The dependency is managed entirely in your browser.

TehNrdTehNrd

Understood but I've still got some beef with the current behavior. The main thing is that it does not behave like the standard picklist functionality in salesforce.com. If you make a field required on a page layout (you can't make picklist required at the database level) it is not required if there are no options. This inconsistent behahvior is not good.

 

And really....it just doesn't make sense. Why would you wan't it to be required if there are no values to select?

rtuttlertuttle

 


Avrom wrote:

Unfortunately, you can't determine whether the list is active on the server-side, because this is figured out in the browser when the user selects a value for the controlling picklist. It's not like an ActionSupport; there's no server round-trip here: The dependency is managed entirely in your browser.


 

Avrom,

 

If vforce handles this client side, why does it not take into account the required="true".  If a list size = 0, and the value = null, then ignore required="true".  Would seem more like something that needs to be handled in the vforce client side code.

 

Jason,

 

I've never liked the required="true" parameter.  Perhaps you could use some custom jquery to validate before submit.  Seems like a lot of work for something that should be checked by the vforce code, but its the only workaround I can think of.

 

 

-Richard

TehNrdTehNrd

When required="true" is bound to inputfields it works pretty well as it has a nice clean output. It gets ugly and unusable if bound to a inputText or selectOption.

 

Some feedback on the suggested options:

 

Validation rule: This is only enforce on the DML. If a page has other required="true" attributes these will be displayed before save is performed. User will fill in all fields and then if they missed the one with the validation rule they will get another round of required fields. It is good design to display these all at once if possible.

 

Custom Apex rule: Same issue as validation rules. There will be two rounds of required messages.

 

Javascript: Probably the "best" solution from a UX perspective but more work on the dev side.

AvromAvrom

I wasn't aware that Layouts allow this; thanks for bringing that to my attention. I'm inclined to agree that we should be striving for similar behavior here. Although I of course can't promise anything in terms of a particular timeframe, I can say that we're looking in to implementing the behavior you suggest.

 

stephanstephan

Thanks for bringing this up Jason. We actually weren't aware that standard pages handle this case in a special way. Certainly if standard pages handle this, then we want to be consistent across the board. While I can't promise when we'll be able to get this change in, we'll certainly strive to do so as soon as we can.

 

...stephan

 

 

This was selected as the best answer
TehNrdTehNrd

Awesome, thanks for the update.

narsavagepnarsavagep

Thanks stephan... this fix is definitely needed.

 

Here is the "Idea" I found for this topic:  

https://sites.secure.force.com/ideaexchange/ideaView?id=08730000000J7JY