0

I have created a datepicker inside the visualforce page using <apex:input> and I want to get value of that <apex:input> into a variable. After little bit bit of googling , I found that create a {get;set;} variable inside the controller apex class and use that varible as value field of the <apex:input>. So, here's the code:

<apex:page docType="html-5.0" showHeader="false" 
               controller="ShowResourcesClass" >

         <apex:form >
                <apex:pageBlock >
                    <apex:pageBlockSection >
                                 <apex:pageBlockSectionItem > 
                                Date: <apex:input type="date" value="{!chosenDate}"/>
                             </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock>
            </apex:form>

            <apex:form >
                <apex:commandButton action="{!getDateFun}" value="Submit" id="theButton"/>
            </apex:form>
    </apex:page>

And this is the controller class:

public class ShowResourcesClass {

public Date chosenDate {get;set;}

public void getDateFun() {
    System.debug('DATE: ' + chosenDate);
 }
}

After I choose the date and click the submit button, the value of chosenDate is null. I cant figure out why.

0

In the above mentioned visualforce page, the <apex:commandButton> and <apex:input> are in different <apex:form>s. Thats the reason why I wasn't getting the value of dateChosen. I got the value when I put both the buttons in the same form. ie,

<apex:page docType="html-5.0" showHeader="false" 
           controller="ShowResourcesClass" >

     <apex:form >
            <apex:pageBlock >
                <apex:pageBlockSection >
                             <apex:pageBlockSectionItem > 
                            Date: <apex:input type="date" value="{!chosenDate}"/>
                         </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:pageBlock>

            <apex:commandButton action="{!getDateFun}" value="Submit" id="theButton"/>

        </apex:form>
</apex:page>
| improve this answer | |

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.