Yii Framework Forum: [SNIPPET] Dropdown Date Picker - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

[SNIPPET] Dropdown Date Picker Potential solution Rate Topic: -----

#1 User is offline   Alex Muir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 25-April 09

Posted 04 September 2009 - 07:08 AM

Hi,

Quite a few people have previously asked on the forum about having dropdown lists for day, month and year when entering dates.

I have come up with a rough solution which works for me. I want to improve my code so please give me your thoughts.

Extend the CHtml helper - I have saved this in /protected/components is this the best place?
class Html extends CHtml
{
        public static function activeDateField($model,$attribute,$htmlOptions=array())
	{
		// SET UP ARRAYS OF OPTIONS FOR DAY, MONTH, YEAR
                $x = 1;
		$dayOptions = array('0'=>' - ');
		while ($x < 31)
		{
			$dayOptions[$x] = $x;
			$x++;
		}

		$monthOptions = array(
			'0' => ' - ',
			'1'=>'January',
			'2'=>'February',
			'3'=>'March',
			'4'=>'April',
			'5'=>'May',
			'6'=>'June',
			'7'=>'July',
			'8'=>'August',
			'9'=>'September',
			'10'=>'October',
			'11'=>'November',
			'12'=>'December',
		);

		$yearOptions = array('0'=>' - ');
		$x = 1901;
		while ($x < 2030)
		{
			$yearOptions[$x] = $x;
			$x++;
		}


		parent::resolveNameID($model,$attribute,$htmlOptions);
		
		if ($model->$attribute != '0000-00-00' && isset($model->$attribute))
		{
			// intval removes leading zero
			$day = intval(date('j',strtotime($model->$attribute)));
			$month = intval(date('m',strtotime($model->$attribute)));
			$year = intval(date('Y',strtotime($model->$attribute)));
		} else
		{
			// DEFAULT TO 0 IF THERE IS NO DATE SET
                        $day = 0;
			$month = 0;
			$year = 0;
		}
		

		$return  = parent::dropDownList($htmlOptions['name'].'[day]', $day,$dayOptions);
		$return .= parent::dropDownList($htmlOptions['name'].'[month]', $month,$monthOptions);
		$return .= parent::dropDownList($htmlOptions['name'].'[year]', $year,$yearOptions);
		return $return;
}

I don't like the code at the start of this function to get options for day, month and year.
This also lacks some functionality for active input fields - it doesn't add an 'error' class etc.

Extend the CActiveRecord class - The SetAttributes function looks for all date fields (given in an array in the model) and merges the three separate input fields into a single MySQL date string.
class ActiveRecord extends CActiveRecord
{
	/**
	 * Extends setAttributes to handle active date fields
	 *
	 * @param $values
	 * @param $scenario
	 */
	public function setAttributes ($values,$scenario='')
	{
		foreach ($this->dateAttributes() as $dateAttribute)
		{
			if (is_array($values[$dateAttribute]))
			{
				$day = $values[$dateAttribute]['day'];
				$month = $values[$dateAttribute]['month'];
				$year = $values[$dateAttribute]['year'];

				$values[$dateAttribute] = date('Y-m-d',mktime(0,0,0,$month,$day,$year));
			}
		}

		parent::setAttributes($values,$scenario);
	}
}


Usage:

In your model add the following function to specify which fields are dates:
class Passport extends ActiveRecord
{
    ....

        public function dateAttributes()
	{
		return array('expiryDate');
	}

    ....
}


And finally, in your view use Html::activeDateField($model,'attributeName') to get date dropdowns.
<div class="simple">
	<? echo CHtml::activeLabelEx($model, 'expiryDate'); ?>
	<? echo Html::activeDateField($model, 'expiryDate'); ?>
</div>

My sites: The Big Eat and The Big Space
0

#2 User is offline   KJedi 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 381
  • Joined: 19-October 08
  • Location:Nikolaev, Ukraine (Europe)

Posted 10 September 2009 - 03:47 PM

I think, that for the displaying purposes it's better to create widget rather than extending CHtml.
And the presentation -> mysql solution is really great. Thanks for sharing. I implemented it in the beforeSave method which is not a good idea.
Posted Image
My Blog: http://programmersnotes.info/ - tips, design patterns, client and server-side coding, yii articles and simply my experience.

Follow me on twitter: http://twitter.com/KonstantinMirin
Add me to your friends on facebook: http://www.facebook....hp?id=784832902
0

#3 User is offline   lynxi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 27-December 09

Posted 28 December 2009 - 07:37 AM

Hi Alex,
That's great. I use your solution in my project. I added error-class support by adding
                if($model->hasErrors($attribute)){
                   parent::addErrorCss($htmlOptions);
                }

                $return  = parent::dropDownList($htmlOptions['name'].'[day]', $day,$dayOptions);
                $return .= parent::dropDownList($htmlOptions['name'].'[month]', $month,$monthOptions);
                $return .= parent::dropDownList($htmlOptions['name'].'[year]', $year,$yearOptions);
                return $return;


thanks,
~holly~
0

#4 User is offline   CoLT 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 22-November 09

Posted 03 January 2010 - 05:46 AM

Can you explain in more details in your snippet usage?

I placed code in components dir named Html.php and made other changes to model and view, but it does not work..

- '); while ($x < 31) { $dayOptions[$x] = $x; $x++; } $monthOptions = array( '0' => ' - ', '1'=>'January', '2'=>'February', '3'=>'March', '4'=>'April', '5'=>'May', '6'=>'June', '7'=>'July', '8'=>'August', '9'=>'September', '10'=>'October', '11'=>'November', '12'=>'December', ); $yearOptions = array('0'=>' - '); $x = 1901; while ($x < 2030) { $yearOptions[$x] = $x; $x++; } parent::resolveNameID($model,$attribute,$htmlOptions); if ($model->$attribute != '0000-00-00' && isset($model->$attribute)) { // intval removes leading zero $day = intval(date('j',strtotime($model->$attribute))); $month = intval(date('m',strtotime($model->$attribute))); $year = intval(date('Y',strtotime($model->$attribute))); } else { // DEFAULT TO 0 IF THERE IS NO DATE SET $day = 0; $month = 0; $year = 0; } $return = parent::dropDownList($htmlOptions['name'].'[day]', $day,$dayOptions); $return .= parent::dropDownList($htmlOptions['name'].'[month]', $month,$monthOptions); $return .= parent::dropDownList($htmlOptions['name'].'[year]', $year,$yearOptions); return $return; }
Fatal error: Class 'Html' not found in C:\wamp\www\ssis\protected\views\klientas\_form.php on line 23

0

#5 User is offline   CoLT 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 246
  • Joined: 22-November 09

Posted 25 January 2010 - 02:07 PM

View PostColt, on 03 January 2010 - 05:46 AM, said:

Can you explain in more details in your snippet usage?

I placed code in components dir named Html.php and made other changes to model and view, but it does not work..

- '); while ($x < 31) { $dayOptions[$x] = $x; $x++; } $monthOptions = array( '0' => ' - ', '1'=>'January', '2'=>'February', '3'=>'March', '4'=>'April', '5'=>'May', '6'=>'June', '7'=>'July', '8'=>'August', '9'=>'September', '10'=>'October', '11'=>'November', '12'=>'December', ); $yearOptions = array('0'=>' - '); $x = 1901; while ($x < 2030) { $yearOptions[$x] = $x; $x++; } parent::resolveNameID($model,$attribute,$htmlOptions); if ($model->$attribute != '0000-00-00' && isset($model->$attribute)) { // intval removes leading zero $day = intval(date('j',strtotime($model->$attribute))); $month = intval(date('m',strtotime($model->$attribute))); $year = intval(date('Y',strtotime($model->$attribute))); } else { // DEFAULT TO 0 IF THERE IS NO DATE SET $day = 0; $month = 0; $year = 0; } $return = parent::dropDownList($htmlOptions['name'].'[day]', $day,$dayOptions); $return .= parent::dropDownList($htmlOptions['name'].'[month]', $month,$monthOptions); $return .= parent::dropDownList($htmlOptions['name'].'[year]', $year,$yearOptions); return $return; }
Fatal error: Class 'Html' not found in C:\wamp\www\ssis\protected\views\klientas\_form.php on line 23



Any news?:-)
0

#6 User is offline   rrkprasad 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 16-March 10

Posted 20 March 2010 - 12:02 AM

I tried to use the snippet for date of birth field. I have name, username, password, password repeat, email fields before date of birth field. When submitting the form the password repeat field error comes up even when I enter correct values.

My guess is it has got to do something with setattributes function.

Any idea?
0

#7 User is offline   Alex Muir 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 25-April 09

Posted 22 March 2010 - 05:44 AM

What is the error it comes up with? Is your confirmPassword field set as safe. Is it defined in the model?
My sites: The Big Eat and The Big Space
0

#8 User is offline   burgulgoth 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-August 11
  • Location:Croatia

Posted 26 September 2011 - 03:38 PM

View PostAlex Muir, on 22 March 2010 - 05:44 AM, said:

What is the error it comes up with? Is your confirmPassword field set as safe. Is it defined in the model?


I have the same problem. Everything with the password field is fine. When I remove snippet, it works again.
:)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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