PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 27, 2022

[FIXED] Yii, CJuiDatePicker how to block certain days of the Month

 February 27, 2022     datepicker, jquery-ui-datepicker, php, yii, yii-widgets     No comments   

Issue

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.


Solution

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:

  • jqueryUi datePicker api
  • javascript Date methods


Answered By - hamed
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing