Search code examples
symfony1datepickersymfony-1.4symfony-forms

sfJQueryUIPlugin: No Picker for new records


Everyday is a new day with Symfony, but I'm loving it! This morning I installed the sfJQueryUIPlugin. It has very little dependencies & accepts themeRoller styles. However, it has 2 issues:

[Feature_Request] There is no way to specify the year range. By default, it shows a 20 year range around the year in the field value. eg. if field value is 1993-01-20, the range will be 1983 to 2003. ??? Has anyone found a way out???

The DatePicker does not appear when the field is empty, Thus it does not show up during new record creation. To solve this, I tried setting up the default value in the date input field (which now appears as a text input) using $this->setDefault('date_of_birth',date('Y-m-d')); ??? Is anybody facing this problem of picker now available during new record creation ??? ??? Also is it the right way to set default value ???

Thanks in advance.


Solution

  • To get the date picker on the new registration form, I had to include javascripts in the indexSuccess template of my form (my fault)

    as for the year range, I modified the plugin file to include additional parameter

    class sfWidgetFormDateJQueryUI extends sfWidgetForm
    {
      protected function configure($options = array(), $attributes = array())
      {
    
        if(sfContext::hasInstance())
         $this->addOption('culture', sfContext::getInstance()->getUser()->getCulture());
        else
         $this->addOption('culture', "en");
        $this->addOption('change_month',  false);
        $this->addOption('change_year',  false);
        $this->addOption('number_of_months', 1);
        $this->addOption('show_button_panel',  false);
        $this->addOption('theme', '/sfJQueryUIPlugin/css/ui-lightness/jquery-ui.css');
        $this->addOption('year_range', '-30:+0');
        parent::configure($options, $attributes);
      }
    
      public function render($name, $value = null, $attributes = array(), $errors = array())
      {
        $attributes = $this->getAttributes();
    
        $input = new sfWidgetFormInput(array(), $attributes);
    
        $html = $input->render($name, $value);
    
        $id = $input->generateId($name);
        $culture = $this->getOption('culture');
        $cm = $this->getOption("change_month") ? "true" : "false";
        $cy = $this->getOption("change_year") ? "true" : "false";
        $nom = $this->getOption("number_of_months");
        $sbp = $this->getOption("show_button_panel") ? "true" : "false";
        $yrs = $this->getOption("year_range");
    
        if ($culture!='en')
        {
        $html .= <<<EOHTML
    <script type="text/javascript">
        $(function() {
        var params = $.datepicker.regional['$culture'];
        params.changeMonth = $cm;
        params.changeYear = $cy;
        params.numberOfMonths = $nom;
        params.showButtonPanel = $sbp;
        params.yearRange = "$yrs";
        $("#$id").datepicker(params);
        });
    </script>
    EOHTML;
        }
        else
        {
        $html .= <<<EOHTML
    <script type="text/javascript">
        $(function() {
        var params = {
        changeMonth : $cm,
        changeYear : $cy,
        numberOfMonths : $nom,
        showButtonPanel : $sbp,
        yearRange : "$yrs"
            };
        $("#$id").datepicker(params);
        });
    </script>
    EOHTML;
        }
    
        return $html;
      }
    
      public function getStylesheets()
      {...
      }
    
      public function getJavaScripts()
      {...
      }
    
    }
    

    and setup the widget as:

    $this->widgetSchema['date_of_birth']= new sfWidgetFormDateJQueryUI(array("change_month" => true, "change_year" => true, "theme" => "smoothness/jquery-ui-1.8.custom.css", "year_range" => "-30:+0"));