CHtml::activeDropDownList() removes leading zero in value

Hey guys,

I’m stuck at this one:

<?php echo CHtml::activeDropDownList($model, ‘bday’, DateHelper::getDays(), array(‘prompt’ => ‘Day’));

DateHelper::getDays() does the following:




public static function getDays() {

		$days = array();

		for($i=1;$i<=31;$i++) {

			$days[$i] = ($i<10 ? '0'.$i : $i);

		}

		return $days;

	}



So, when $model->bday is less then 10 (like, 05 - May 5th), CHtml::activeDropDownList won’t apply “selected” on that row, because of 5!==05

I would appreciate if you help me to deal with this bug without modifying core (framework) scripts.

Cheers!

What type is $model->bday? If it’s a string like ‘05’ then it’s clear why it doesn’t work: The keys of your getDays() array are numeric values.

You were right. So, I did:

EditProfileForm:




public $bday, $bmonth, $byear;

protected function afterFind() { // public function beforeGetter() ? 

	list($this->byear, $this->bmonth, $this->bday) = explode('-', $this->birthday);

	$this->bday=intval($this->bday);

	$this->bmonth=intval($this->bmonth);

	return parent::afterFind();

}

protected function beforeValidate() {

	$this->birthday = date('Y-m-d',strtotime($this->bday.'-'.$this->bmonth.'-'.$this->byear));

	return parent::beforeValidate();

}



$this->bday=intval($this->bday); is a key to happiness ^^.

My view goes like this:


<?php echo CHtml::activeLabel($model,'birthday'); ?>

<?php 

echo CHtml::activeDropDownList($model, 'bday', DateHelper::getDays(), array('prompt' => 'День'));

echo CHtml::activeDropDownList($model, 'bmonth', DateHelper::getMonths(), array('prompt' => 'Месяц'));

echo CHtml::activeDropDownList($model, 'byear', DateHelper::getYears(), array('prompt' => 'Выберите год'));?>

<?php echo CHtml::error($model,'birthday'); ?>




class DateHelper extends CComponent{

	public static function getMonths() {

		$m=array('Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь');

		return array_combine(range(1,12),$m);

	}

	public static function getDays() {

		return range(1,31);

	}

	public static function getYears() {

		return array_combine($a=range((date('Y') - 16),(date('Y') - 60)),$a);

	}

p.s.

getYears() returns only those values registration policy allows.