Yii Framework Forum: Combining activeTextField and a widget (datepicker) - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

Combining activeTextField and a widget (datepicker) Rate Topic: -----

#1 User is offline   alf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 22-April 09

Posted 20 September 2009 - 01:02 PM

I have a date attribute that is part of a model, and this is how i want to get the date:
<? echo CHtml::activeTextField($model, "my_date"); ?>

But I want the date to be chosen by the datepicker widget:
<?
$this->widget('application.extensions.jui.EDatePicker',
              array(
                    'name'=>'publish_date',
                    'language'=>'no',
                    'mode'=>'focus',
                    'dateFormat'=>'d.M yy',
                    'value'=>date('j.M Y'),
                    'fontSize'=>'0.8em',
                    'htmlOptions'=>array('size'=>30,'class'=>'date'),
                   )
             );
?>


Can this be done? And if so, how?
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 20 September 2009 - 02:24 PM

View Postalf, on 20 September 2009 - 01:02 PM, said:

I have a date attribute that is part of a model, and this is how i want to get the date:
<? echo CHtml::activeTextField($model, "my_date"); ?>

But I want the date to be chosen by the datepicker widget:
<?
$this->widget('application.extensions.jui.EDatePicker',
              array(
                    'name'=>'publish_date',
                    'language'=>'no',
                    'mode'=>'focus',
                    'dateFormat'=>'d.M yy',
                    'value'=>date('j.M Y'),
                    'fontSize'=>'0.8em',
                    'htmlOptions'=>array('size'=>30,'class'=>'date'),
                   )
             );
?>


Can this be done? And if so, how?


This seems to work:
...
'name'=>'My_model[my_date]',
...


/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
1

#3 User is offline   alf 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 22-April 09

Posted 21 September 2009 - 10:09 AM

View Posttri, on 20 September 2009 - 02:24 PM, said:

This seems to work:
'name'=>'My_model[my_date]',



Well, not really. I mean, it will post the result in a array-variable, but it wont link to the model in the "yii way". I still will need to validate the input etc.

I think the way to go is to keep the activeTextField part, put a 'id=my_date_id' in the html options, and somehow make the widget datepicker link to this id. I haven't dug into how I would change the widget to make this work yet, but hopefully I will find this out or someone else can point me to this who has experience with widgets?!
0

#4 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 21 September 2009 - 10:18 AM

View Postalf, on 21 September 2009 - 10:09 AM, said:

Well, not really. I mean, it will post the result in a array-variable, but it wont link to the model in the "yii way". I still will need to validate the input etc.

I think the way to go is to keep the activeTextField part, put a 'id=my_date_id' in the html options, and somehow make the widget datepicker link to this id. I haven't dug into how I would change the widget to make this work yet, but hopefully I will find this out or someone else can point me to this who has experience with widgets?!


The base class CInputWidget needs the properties "model" and "attribute" to be set, then it will select an activeTextField for you. I assumed I could just set the name (looks the same in the http request), but didn't check which input field got selected.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#5 User is offline   Lineke 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 22-September 09

Posted 22 September 2009 - 05:03 AM

I have the same problem, but haven't looked into it. If you found a solution, could you post it here?
0

#6 User is offline   Shaked 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 10-January 10

Posted 10 January 2010 - 06:00 PM

I`m using yii 1.1 and used this:

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    
    'name'=>'user[Birthdate]',
    //'id'=>'user_Birthdate',
    'model'=>$model,
    
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;',

    ),
));

?>


I think that you can use it too, or maybe change a bit but the idea is the same.

one important thing is when I used "model->$model" before "name->... " it didn't work.

hope I help..
2

#7 User is offline   Alvin Lim 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 15-April 10

Posted 15 April 2010 - 11:36 PM

I am having the same problem too. When I click submit form button, the value is blank. It didn't really attach to active model like other textbox which retain value after postback. Is there anyone has solution for this?
0

#8 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 16 April 2010 - 04:47 AM

View PostAlvin Lim, on 15 April 2010 - 11:36 PM, said:

I am having the same problem too. When I click submit form button, the value is blank. It didn't really attach to active model like other textbox which retain value after postback. Is there anyone has solution for this?


Seems like CHtml::activeName(model, attribute) will generate the correct name for you. In my case, I have to take care of an additional data type issue (validation error due to type mismatch).

/Tommy

Note: the properties $model and $attribute was (IIRC) added in an updated CJuiDatePicker, but that was still beyond my knowledge when this post was created.

This post has been edited by tri: 04 June 2011 - 04:45 AM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#9 User is offline   Sivanantham 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 21-February 10

Posted 26 May 2010 - 09:05 AM

View Posttri, on 16 April 2010 - 04:47 AM, said:

Seems like CHtml::activeName(model, attribute) will generate the correct name for you. In my case, I have to take care of an additional data type issue (validation error due to type mismatch).

/Tommy



Extend the CJuiDatePicker as given below and save at applications' widget folder


<?php
Yii::import('zii.widgets.jui.CJuiDatePicker');

class ECJuiDatePicker extends CJuiDatePicker
{
/**
* Run this widget.
* This method registers necessary javascript and renders the needed HTML code.
*/
public function run()
{
list($name,$id)=$this->resolveNameID();

if(isset($this->htmlOptions['id']))
$id=$this->htmlOptions['id'];
else
$this->htmlOptions['id']=$id;
if(isset($this->htmlOptions['name']))
$name=$this->htmlOptions['name'];
else
$this->htmlOptions['name']=$name;

if($this->hasModel())
echo EHtml::activeTextField($this->model,$this->attribute,$this->htmlOptions);
else
echo EHtml::textField($name,$this->value,$this->htmlOptions);

$options=CJavaScript::encode($this->options);

$js = "jQuery('#{$id}').datepicker($options);";

if (isset($this->language)){
$this->registerScriptFile($this->i18nScriptFile);
$js .= "\njQuery('#{$id}').datepicker('option', jQuery.datepicker.regional['{$this->language}']);";
}
Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id, $js);
}
}
?>


Apply the new extended class as given below in your view file and will work definitively


<?php $this->widget('application.widgets.base.ECJuiDatePicker', array('model'=>$model,'attribute'=>'scheduleEndDate',
// additional javascript options for the date picker plugin
'options'=>array(
'minDate'=>"0",
'maxDate'=> "60",
'showOn'=>'button',
'buttonImage'=>'images/calendar.gif',
'buttonImageOnly'=>true,
'dateFormat'=>Common::clientDateFormat(),
),
'htmlOptions'=>array(
'style'=>'height:20px;width:100px;'
,'readonly'=>'true'
),
));
?>
0

#10 User is offline   Khan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 09-September 10

Posted 09 September 2010 - 02:08 PM

View Postalf, on 20 September 2009 - 01:02 PM, said:

I have a date attribute that is part of a model, and this is how i want to get the date:
<? echo CHtml::activeTextField($model, "my_date"); ?>

But I want the date to be chosen by the datepicker widget:
<?
$this->widget('application.extensions.jui.EDatePicker',
              array(
                    'name'=>'publish_date',
                    'language'=>'no',
                    'mode'=>'focus',
                    'dateFormat'=>'d.M yy',
                    'value'=>date('j.M Y'),
                    'fontSize'=>'0.8em',
                    'htmlOptions'=>array('size'=>30,'class'=>'date'),
                   )
             );
?>


Can this be done? And if so, how?




Hi, I got the 'jui.EDatePicker' and using on my one of Form with following setting where a user can select any date by jui.EDatePicker and it save to right table through right Model here is setting...

<div class="row">
    <?php echo $form->labelEx($model,'expire_time'); ?>
    <?php $this->widget('application.extensions.jui.EDatePicker',
     array(
          'name'=>'expire_time',
          'attribute'=>'expire_time', // Model attribute filed which hold user input
          'model'=>$model,            // Model name
          'language'=>'en',
          'mode'=>'imagebutton',
          'theme'=>'redmond',
          'value'=>date('Y-m-d'),
          'htmlOptions'=>array('size'=>15),
          'fontSize'=>'0.8em'
         )
      );?>
      <?php echo $form->error($model,'expire_time'); ?>
</div>


I hope it will work with all as mine work like charm
0

#11 User is offline   gaboesquivel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 11-October 10

Posted 11 October 2010 - 07:16 PM

View PostShaked, on 10 January 2010 - 06:00 PM, said:

I`m using yii 1.1 and used this:

<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
    
    'name'=>'user[Birthdate]',
    //'id'=>'user_Birthdate',
    'model'=>$model,
    
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;',

    ),
));

?>


I think that you can use it too, or maybe change a bit but the idea is the same.

one important thing is when I used "model->$model" before "name->... " it didn't work.

hope I help..


Thanks Shaked!!
This solution worked for me

-Gabo
2

#12 User is offline   Mohamed Y. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 01-January 11

Posted 22 January 2011 - 11:20 AM

View Postgaboesquivel, on 11 October 2010 - 07:16 PM, said:

Thanks Shaked!!
This solution worked for me

-Gabo



Thanx for sharing :)


0

#13 User is offline   Mohamed Y. 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 01-January 11

Posted 22 January 2011 - 05:07 PM

Worked for me :)
Thx

View Postalf, on 21 September 2009 - 10:09 AM, said:

Well, not really. I mean, it will post the result in a array-variable, but it wont link to the model in the "yii way". I still will need to validate the input etc.

I think the way to go is to keep the activeTextField part, put a 'id=my_date_id' in the html options, and somehow make the widget datepicker link to this id. I haven't dug into how I would change the widget to make this work yet, but hopefully I will find this out or someone else can point me to this who has experience with widgets?!




0

#14 User is offline   rahul.vit09 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-February 11

Posted 13 February 2011 - 02:23 AM

View PostKhan, on 09 September 2010 - 02:08 PM, said:

Hi, I got the 'jui.EDatePicker' and using on my one of Form with following setting where a user can select any date by jui.EDatePicker and it save to right table through right Model here is setting...

<div class="row">
    <?php echo $form->labelEx($model,'expire_time'); ?>
    <?php $this->widget('application.extensions.jui.EDatePicker',
     array(
          'name'=>'expire_time',
          'attribute'=>'expire_time', // Model attribute filed which hold user input
          'model'=>$model,            // Model name
          'language'=>'en',
          'mode'=>'imagebutton',
          'theme'=>'redmond',
          'value'=>date('Y-m-d'),
          'htmlOptions'=>array('size'=>15),
          'fontSize'=>'0.8em'
         )
      );?>
      <?php echo $form->error($model,'expire_time'); ?>
</div>


I hope it will work with all as mine work like charm



Hi there When I was trying to use the above code i got error saying

Alias "application.extensions.jui.EDatePicker" is invalid. Make sure it points to an existing PHP file.

C:\xampp\xampp\htdocs\yii\framework\YiiBase.php(310)

298         if($isClass && (class_exists($className,false) || interface_exists($className,false)))
299             return self::$_imports[$alias]=$className;
300 
301         if(($path=self::getPathOfAlias($alias))!==false)
302         {
303             if($isClass)
304             {
305                 if($forceInclude)
306                 {
307                     if(is_file($path.'.php'))
308                         require($path.'.php');
309                     else
310                         throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file.',array('{alias}'=>$alias)));
311                     self::$_imports[$alias]=$className;
312                 }
313                 else
314                     self::$classMap[$className]=$path.'.php';
315                 return $className;
316             }
317             else  // a directory
318             {
319                 if(self::$_includePaths===null)
320                 {
321                     self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
322                     if(($pos=array_search('.',self::$_includePaths,true))!==false)


plz tell me how to resolve this error.
0

#15 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 29 May 2011 - 11:28 PM

Perhaps rahul.vit09 has a newer version of Yii, so the code provided by Khan didn't work for him. It happened to me as well. I looked into the Class Reference and found this:

$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;'
 	),
));


I applied it to my own project and got this working fine:

<div class="row">
   <?php echo $form->labelEx($model,'Date Of Birth'); ?>
   <?php 
	$this->widget('zii.widgets.jui.CJuiDatePicker', array(
   	'name'=>'dateofbirth',
   	'options'=>array('changeMonth'=>'true', 'changeYear'=>'true'),
   	'htmlOptions'=>array(''=>'')  // None at the moment
	));
   ?>
   <?php echo $form->error($model,'dateofbirth'); ?>
 </div>


The difference, as you can see, is the path to the class.
First it was:
application.extensions.jui.EDatePicker

But now it is located at:

zii.widgets.jui.CJuiDatePicker

0

#16 User is offline   kiran sharma 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 492
  • Joined: 21-May 11
  • Location:India

Posted 04 June 2011 - 04:06 AM

No need to use additional textfield , only widget required .

<?php
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'name'=>'LeaveSummary[LS_FROM]', // LeaveSummary (Model) , LS_FROM (Attribute)
'model'=>$model,
'attribute'=>'LS_FROM',
// additional javascript options for the date picker plugin
'options'=>array(
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
),
'htmlOptions'=>array(
'style'=>'height:20px;'
),
));
?>

This will work
Thanks,
Kiran Sharma.
1

#17 User is offline   rin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 45
  • Joined: 21-July 11

Posted 25 July 2011 - 02:35 AM

are you add javascript ? i use that but no changes value only 0
0

#18 User is offline   hollyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 09-June 11

Posted 01 August 2011 - 12:36 PM

Are you trying to save the value selected by datepicker in a different form field, one associated with a model? You should be able to simply assign an ID to the form field and use that as the selector for the 'altField' option. I've done this with hidden fields that stored the start date of one of my models. It worked like a charm. For example, the code below shows the datepicker widget populating an alternate (target) form field using an alternate format than datepicker's default.

$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'datepicker_field', // Name of the datepicker form field.
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
        'altField'=>'#some_selector', // The jQuery selector of the "target" field
	'altFormat'=>'yy-mm-dd', // If you want the target field to have a different format
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;',
    ),
));


When the value is selected in the datepicker, it should populate whatever form field has the unique id, 'some_selector'. For example:
<?php echo $form->hiddenField($model, 'start_date', array('id'=>'#some_selector', 'value'=>'')); ?>

Mine was a hidden field, but I don't see why this wouldn't work with a text field.

Good luck!
0

#19 User is offline   arip 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 25-September 11

Posted 19 October 2011 - 09:13 PM

I use datepicker like
<?php echo $form->labelEx($model,'birthday'); ?>
			<?php
			$this->widget('zii.widgets.jui.CJuiDatePicker', array(
			'name'=>'MutationOrder[birthday]',
			'value'=>$model->birthday,
			'options'=>array(
			'showAnim'=>'fold',
			'dateFormat'=>'dd MM yy',
			 ),
			));
			?>
			<?php echo $form->error($model,'Birthday'); ?>


and usefull.
1

#20 User is offline   Javed Iqbal 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 35
  • Joined: 25-October 11

Posted 13 February 2012 - 03:44 AM

Hi
I want to repeat the post once again as posted by member already:

I am having the same problem too. When I click submit form button, the value is blank. It didn't really attach to active model like other textbox which retain value after postback. Is there anyone has solution for this?
Javed Iqbal

Web Developer
syntecX Solution
skype:javedboqo
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users