Dharmesh patel
Dharmesh patel

Reputation: 654

how to put datepicker in controller in yii

I have a form in yii and one field in form is come from controller by clicking on a button. if i pass text field instead of datepicker then it works otherwise it gives error so what to do now?

my form code is as below:

            <div class="row buttonproduct" hidden>
                <input class="btn btn-primary add-product" style="margin-left: 0px;" type="button" value="Add Lease Agreement">
            </div>

script in the form is like:

<script>

$('.add-product').live('click', function(){
        var value = parseInt(document.getElementById('service').value, 10);
        value = isNaN(value) ? 0 : value;
        value++;
        document.getElementById('service').value = value;

        $.ajax({
            url:"<?php echo Yii::app()->request->baseUrl;?>"+"/Landowner/addlease?id="+value,
            data:'req=add_more',
            dataType:'html',
            type:'POST',
            async: true,
            cache: false,
            success:function(resp){
                $('.addproduct').append(resp);
            },
            error:function(er){ 
                alert("An error has occured, Please reload/refresh the page and try again.");
            }
        });

   return false;
   });
   </script>

and my controller code is:

public function actionAddlease() {
    if (Yii::app()->request->isAjaxRequest) {
        $id = $_REQUEST['id'];
        $div = '<div class="row">';

        $data = $div.CHtml::label(Yii::t('LeaseAgreement[lease_agrmnt_id]', 'Lease Agreement ID '), 'LeaseAgreement[lease_agrmnt_id]');
        $data .= CHtml::textField('LeaseAgreement[lease_agrmnt_id][]', '', array('maxlength'=>'300')).'</div>';


        $data .= $div.CHtml::label(Yii::t('LeaseAgreement[lease_startdate]', 'Start Date '), 'LeaseAgreement[lease_startdate]');
        $data .= $this->widget('zii.widgets.jui.CJuiDatePicker', array(
                            'name'=>'LeaseAgreement[lease_startdate][]',
                            //'model'=>$model,
                            'id'=>'LeaseAgreement_lease_startdate',
                            //'value'=>Yii::app()->dateFormatter->format("'yy-mm-dd'",strtotime($model->datecolumn_name)),
                            'options'=>array(
                            'showAnim'=>'fold',
                            ),
                            'htmlOptions'=>array(
                            'style'=>'height:20px;'
                            ),
                    ));

        $data .= $div.CHtml::label(Yii::t('LeaseAgreement[filename]', 'Lease Agreement Document '), 'LeaseAgreement[filename]');;
        $data .= CHtml::fileField('LeaseAgreement[filename][]', '', array('maxlength'=>'300')).'</div>';

        echo $data;
    }
}

Here, if i dont use datepicker in this function then its works otherwise not, so where is the mistake. please help anyone i stuck here.

Upvotes: 1

Views: 275

Answers (1)

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

You can use Jquery ui which will work on the input class or id

text field code

$data .= CHtml::textField('LeaseAgreement[lease_agrmnt_id][]', '', array('maxlength'=>'300','class'=>'datepicker')).'</div>';

jquery

<script>
  $(function() {
    $( ".datepicker" ).datepicker();
  });
  </script>

you may need to include jquery Ui files for this .

Upvotes: 1

Related Questions