Lets explore all the ways to form Date pickers
a)Using inputfield is best solution .
Disadvantage:Field level security and profile settings .If your object dont have edit access or field is hidden for any profile the same will apply and there is no way to bypass this security if you wish to by pass for various profiles (Note this is also advantage if your requirement is not to allow edit based on profile)
b)Hackish solution with input text
Visualforce page:
<apex:page controller="datePicker" id="mypage">
<apex:form>
Date: <apex:inputText value="{!datename}" size="10" id="demo" onfocus="DatePicker.pickDate(false, this , false);" />
</apex:form>
</apex:page>
Apex Controller:
public class DatePicker {
public String datename {get; set;}
}
Disadvantage :hacked the JavaScript of salesforce and not officially recommended .
c)New trend after winter 14
<apex:pageBlockSection >
<apex:outputLabel ></apex:outputLabel>
<apex:input label="datePicker" value="{! fDate }" type="auto"/>
</apex:pageBlockSection>
public class winter14Ctrl {
public Date fDate { get; set; }
}
Advantage :This is native to salesforce and safely recommended
Disadvantages:
Not supported in Firefox and IE. Source: http://caniuse.com/#search=input%20type
Your all the instances must be updated to winter 13
d)Using jquery component to do this .Jquery library or jquery UI will have this component
disadvantage:More developer skills needed and lot of testing with different browser
When showDatePicker
is true the onfocus of the input field calls some JavaScript:
<input onfocus="DatePicker.pickDate(true, 'p:f:x', false);" ...>
and when it is false that is missing. (Bear in mind that this is not part of Salesforce's published API so may change in future.)
So jQuery can be used to remove the onfocus
handler when the page loads, and that logic can depend on client-side logic (which I presume is what you are looking for as server-side logic could just use the showDatePicker
attribute). Here is an example page that has 2 date fields with the picker and 2 without:
<apex:page standardController="Contact">
<apex:form>
<apex:inputField value="{!Contact.Birthdate}" styleClass="noDatePicker"/>
<apex:inputField value="{!Contact.Birthdate}"/>
<apex:inputField value="{!Contact.Birthdate}" styleClass="noDatePicker"/>
<apex:inputField value="{!Contact.Birthdate}"/>
</apex:form>
<apex:includeScript value="{!URLFOR($Resource.Jquery)}"/>
<script>
var j$ = jQuery.noConflict();
// JQuery code could add the noDatePicker class or otherwise make this logic conditional
j$(document).ready(function() {
j$('input.noDatePicker').each(function() {
// Clear the DOM element onfocus
this.onfocus = null;
});
});
</script>
</apex:page>
Best Answer
You can do in a complicated way using visual force and custom date picker. However I feel the best way to write a validation rule upon save to save if the selected date is in future or not, and throw the error respectively.