DataPicker

[font="Trebuchet MS"]Hi, i am using this code for date picker


<div class="row">

                <?php echo $form->labelEx($model,'launchDate'); ?>

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

                 'name'=>'launchDate',

                 'value'=>$model->launchDate,

                 'options'=>array('changeMonth'=>'true', 'changeYear'=>'true'),

                 'htmlOptions'=>array(''=>'')  // None at the moment

                   ));

   ?>

                <?php echo $form->error($model,'launchDate'); ?>

</div>



but the date filed is not getting in the $_POST, while other fields are getting in $_POST.

What is the issue in this? do i need to change anywhere in model or controller? please help me out [/font]

You have to pass ‘model’ attribute to the widget when you are working with CActiveForm.


<div class="row">

                <?php echo $form->labelEx($model,'launchDate'); ?>

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

                 'model'=>$model, // this is it

                 'name'=>'launchDate',

                 // 'value'=>$model->launchDate, // no need to pass it

                 'options'=>array('changeMonth'=>'true', 'changeYear'=>'true'),

                 'htmlOptions'=>array(''=>'')  // None at the moment

                   ));

   ?>

                <?php echo $form->error($model,'launchDate'); ?>

</div>



[EDIT]

I’m sorry. The code above is not right. Please see to the following post by bennouna.

@softark, according to my experience, it’s rather


…

'model'=>$model,

'attribute'=>'launchDate',

//'name'=>'Model[launchDate]', //I specify that if needed to handle $_POST, AFAIR

…

Ah! You are right, bennouna.

hi thanks. its working. i am getting the posted value but the issue is it is not been inserting to the db

code used


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

                                'model'=>$model,

				'name'=>'Phase[startDate]',

                                'attribute'=>'startDate',

                                'options'=>array('showAnim'=>'fold','changeMonth'=>'true', 

                                'changeYear'=>'true'),

                                'htmlOptions'=>array(

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

                                ),

                                )); 

   		?>

In controller, i am getting all value


Array ( [projectId] => 1 [phase] => 1 [block] => A Block [noOfUnits] => 12 [noOfFloors] => 10 [location] => Indira Nagar [description] => sdsd [workStatus] => Open for Sale [startDate] => 04/02/2012 [launchDate] => 04/09/2012 ) 

its inserting all fields into DB but except 2 date fields.

DB table:


CREATE TABLE IF NOT EXISTS `phase` (

  `id` mediumint(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> unsigned NOT NULL AUTO_INCREMENT,

  `projectId` tinyint(3) unsigned NOT NULL COMMENT 'CONSTRAINT FOREIGN KEY (projectId) REFERENCES Projects(id)',

  `phase` varchar(20) NOT NULL,

  `block` varchar(40) NOT NULL,

  `noOfUnits` int(10) NOT NULL,

  `noOfFloors` int(10) NOT NULL,

  `location` varchar(40) NOT NULL,

  `description` varchar(255) NOT NULL,

  `workStatus` varchar(40) NOT NULL,

  `startDate` varchar(30) NOT NULL,

  `launchDate` varchar(30) NOT NULL,

  PRIMARY KEY (`id`),

  KEY `projectId` (`projectId`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

Since you are using VARCHAR it shouldn’t be a parse problem.

Can you post your controller and your model rules array (validation rules)?

rules:


public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('projectId, phase, block,workStatus', 'required'),

			array('projectId, noOfUnits, noOfFloors', 'numerical', 'integerOnly'=>true),

			array('phase', 'length', 'max'=>20),

			array('block, location, workStatus', 'length', 'max'=>40),

			array('description', 'length', 'max'=>255),

			//array('launchDate', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, projectId, phase, block, noOfUnits, noOfFloors, location, description, workStatus, startDate, launchDate', 'safe', 'on'=>'search'),

		);

	}

Controller for create:


public function actionCreate()

	{

		$model=new Phase;


		//Uncomment the following line if AJAX validation is needed

		//$this->performAjaxValidation($model);

		

		if(isset($_POST['Phase']))

		{

			//print_r($_POST['Phase']); exit;

			$model->attributes=$_POST['Phase']; 

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

Ok. So you have to add validation rule for launchDate and startDate.

If they are always required, the most logical is ‘required’


…

array('projectId, phase, block, workStatus, launchDate, startDate', 'required'),

…

Otherwise, maybe a simple non-blocking rule, just to feed it to the model


…

array('launchDate, startDate', 'length', 'max'=>10),

…