NewbieCoder
NewbieCoder

Reputation: 706

Yii, CJuiDatePicker how to block certain days of the Month

So I have this widget on my Yii _form.php page.

Is it possible to do things like blocking a certain day of the month? Or maybe blocking all the Mondays of the Month, disallowing users to select any Monday.

UPDATES based on hamed's answer

<script type="text/javascript">

function disableSpecificDays(date) {
//date is an instance of Date
    var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
    if(weekDay == 1){ //weekDay == 1 means Monday 
        return false;
    }
    else {
        return true;
    }
}

</script>

And on the view side,

<?php $form->widget('zii.widgets.jui.CJuiDatePicker', array(
        'model' => $model,          
        'attribute' => 'date',          
        'value' => $model->date,
        'options' => array(
            'showAnim'=>'fadeIn',
            'showButtonPanel' => true,
            'minDate'=>'0',
            'changeYear' => true,
            'dateFormat' => 'yy-mm-dd',
            'beforeShowDay' => 'disableSpecificDays',
        ),              
    ));
?>

But for some reason, it blocks EVERYTHING on the date picker. Nothing can be chosen at all. At which point did I do wrong? Please advise.

Upvotes: 0

Views: 1407

Answers (3)

Sadee
Sadee

Reputation: 3180

This is bit legacy issue, but this is the code I made pass:

...
'options'=>array(
    'beforeShowDay'=> 'js:function(date){ 
        var weekDay = date.getDay(); 
        var monthDay = date.getDate()  
        if(monthDay == 27 || weekDay == 1) { //Disable all Mondays & 27th of the each month
            return [false];
        } else { 
            return [true];
        }',
...

Upvotes: 0

ifo
ifo

Reputation: 1

I know this is an old entry but i found that for Yii 1, putting the return value inside brackets [] does the job. So the JS function should be:

<script type="text/javascript">
    //DON'T SHOW SUNDAYS
    function disableSpecificDays(date) {
    //date is an instance of Date
        var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
        var monthDay = date.getDate();
        if(weekDay == 0){
            return [false];
        }
        else {
            return [true];
        }
    }
</script>

Upvotes: 0

hamed
hamed

Reputation: 8033

jqueryUi datepicker has beforeShowDay event. You can use this event in this way:

$this->widget('zii.widgets.jui.CJuiDatePicker',array(
    ...
   'options'=>array(
       'showAnim'=>'slide',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
       'showOtherMonths'=>true,// Show Other month in jquery
       'selectOtherMonths'=>true,// Select Other month in jquery,
       'beforeShowDay' => 'disableSpecificDays', //changed ':' to '=>' AND added quote in between function name.
    ),
  'htmlOptions'=>array(
      'style'=>''
  ),
));
?>

Now, you need to define disableSpecificDays function inside <script> tag:

function disableSpecificDays(date) {
    //date is an instance of Date
    var weekDay = date.getDay(); //  Get the weekday as a number (0-6)
    var monthDay = date.getDate()   //Get the day as a number (1-31)
    if(monthDay == 12 || monthDay == 13 || weekDay == 1) //weekDay == 1 means Monday 
       return false;
    else return true;
}

This will disable 12th an 13th days in each month and also disable Mondays.

Here are the two useful link:

Upvotes: 1

Related Questions