CFormInputElement and CJuiDatePicker config

Hi All,

Might eb a quick one for someone out there. Im trying to use a CJuiDatePicker in a CForm. Ive figured out that this works:




...

'elements'=>array(

    'dateOfBirth'=>array(

        'type'=>'zii.widgets.jui.CJuiDatePicker',

    ),

),

...



But cant figure out where to put config for the CJuiDatePicker e.g.




array(

    'changeYear'=>true,

    'yearRange'=>'c-100'

)



Anyone know how to do this?

Many thanks

Ross

Ross,

You can put the config in attributes property, inherited from CFormElement. CFormInputElement initializes the widget using the path alias ‘type’ and configuration in ‘attributes’ in renderInput.

So your example would look like:




...

'elements'=>array(

    'dateOfBirth'=>array(

        'type'=>'zii.widgets.jui.CJuiDatePicker',

        'attributes' => array(

            'firstDay' => 1,

            'numberOfMonths' => 2,

            ...

        ),

    ),

),

...



  • Sunpreet.

Hi Sunpreet,

Thanks for replying!

I had tried that already as it seemed the most obvious place to put it but i got the follwoing error:

Fatal error: Method CForm::__toString() must not throw an exception in C:\wamp\nffc\protected\views\users\register.php on line 4

My code is below. When i comment out the CJuiDatePicker it works, when its in i get the error above. Any ideas?





<?php

return array(

    'elements'=>array(

        'user'=>array(

            'type'=>'form',

            'title'=>'Login information',

            'elements'=>array(

                'username'=>array(

                    'type'=>'text',

                ),

                'password'=>array(

                    'type'=>'password',

                ),

                'password_repeat'=>array(

                    'type'=>'password',

                ),

                'emailAddress'=>array(

                    'type'=>'text',

                )

            ),

        ),

 

        'profile'=>array(

            'type'=>'form',

            'title'=>'Profile information',

            'elements'=>array(

                'firstname'=>array(

                    'type'=>'text',

                ),

                'surname'=>array(

                    'type'=>'text',

                ),

                'country'=>array(

                    'type'=>'text',

                ),

                'dateOfBirth'=>array(

                	'type'=>'zii.widgets.jui.CJuiDatePicker',

                	'attributes' => array(

                		'firstDay' => 1,

                		'numberOfMonths' => 2,

        			),

        		),

				'avatar'=>array(

                    'type'=>'text',

                ),

				'information'=>array(

                    'type'=>'textarea',

                ),

				'emailSubscribed'=>array(

                    'type'=>'checkbox',

                ),

				'emailDisplay'=>array(

                    'type'=>'checkbox',

                ),

            ),

        ),

    ),

 

    'buttons'=>array(

        'register'=>array(

            'type'=>'submit',

            'label'=>'Register',

        ),

    ),

);




Hi Ross,

No problem. Yeah, I have encountered that fatal CForm::__toString() error as well. So I changed the view script to call render() directly so that Yii could add its own error handling:

Change


<?php echo $form; ?>

to


<?php echo $form->render(); ?>

This showed me more helpful error message with a full stack trace. I was then able to identify the bug and fix it. May be you can try this approach? Let me know how it goes.

Cheers,

Sunpreet.

Hi Sunpreet,

Great tip. That gave me the error at least. From what i can see the options passed are being applied to the input tag rather than to the JUI Date Picker. Could this be a bug in CWidgetFactory?

I tried it with:




'attributes' => array(

    'name' => 'test',

    'id' => 'abc',

),



Which gave:




<input id="abc" class="hasDatepicker" type="text" value="1970-01-01" name="test">



then with:




'attributes' => array(

    'name' => 'test',

    'class' => 'abc',

),



which threw an error saying Property "CJuiDatePicker.class" is not defined.

Im a bit lost with whats happening here but to me it looks like its trying to set the wrong property values e.g htmlOptions instead of options perhaps?

Any ideas anyone?

Thanks

Ross




C:\wamp\yii-1.1.8.r3324\framework\web\CWidgetFactory.php(162)


150         if(isset($this->widgets[$className]))

151             $properties=$properties===array() ? $this->widgets[$className] : CMap::mergeArray($this->widgets[$className],$properties);

152         if($this->enableSkin)

153         {

154             if($this->skinnableWidgets===null || in_array($className,$this->skinnableWidgets))

155             {

156                 $skinName=isset($properties['skin']) ? $properties['skin'] : 'default';

157                 if($skinName!==false && ($skin=$this->getSkin($className,$skinName))!==array())

158                     $properties=$properties===array() ? $skin : CMap::mergeArray($skin,$properties);

159             }

160         }

161         foreach($properties as $name=>$value)

162             $widget->$name=$value;

163         return $widget;



Ross,

Normally you would initialize CJuiDatePicker as below (from CJuiDatePicker class comment):




$this->widget('zii.widgets.jui.CJuiDatePicker', array(

    'name'=>'publishDate',

    // additional javascript options for the date picker plugin

    'options'=>array(

        'showAnim'=>'fold',

    ),

    'htmlOptions'=>array(

        'style'=>'height:20px;'

    ),

));



Within a CForm’s elements configuration, you would use below:




    'publishDate' => array(      // publishDate is the attribute of the model associated to this CForm.

        'type' => 'zii.widgets.jui.CJuiDatePicker',

        'attributes' => array(   // A list of attributes to initialize in CJuiDatePicker.

            'options' => array(  // A list of jQuery date picker options, as per http://jqueryui.com/demos/datepicker/#options

                'showAnim' => 'fold',

            ),

            'htmlOptions' => array( // A list of HTML attributes associated to the date picker input element.

                'style' => 'height: 20px;'

            ),

        ),

    ),



Notice that the array passed in as the second parameter to $this->widget in the first snippet is the same as ‘attributes’ in the second snippet, except for ‘name’, which is handled when CForm initializes its elements.

Hope this helps.

  • Sunpreet.

Hi Sunpreet,

Works a charm! Many thanks for the help. Still learning Yii and was a bit worried at first about sites saying that Yii had a small community but it seems to be growing every day and they havent let me down yet!

Thanks!

Ross

Glad to be of help. :)