Get the range of possible values for an attribute from a model

Hi,

I am new to YII and frameworks in general. I am trying to create a drop down list in my view with the possible values for my model.

I have a model called ‘State’ with an attribute called ‘order’. I have defined in the rules so that ‘order’ must be an integer with a minimum value of -10 and a maximum of 10. I want a dropdown for the ‘order’ in the view.

Obviously, I could write the drop down with these values hard coded:




$range = array();

foreach(range(-10, 10) as $value){

	$range[$value] = $value;

}

echo $form->dropDownList($model, 'order', $range); 



But it would be better to ask the State model what the possible values are for the order attribute. Or at least what the min and max are. So that when they change, I don’t have to update the view. Is this possible?

Thanks

Brendan

I don’t see a clear cut way to query these values from the rules for a model.

I would side step it by declaring constants in your model




class MyModel extends CActiveModel

{

    const MIN_RANGE = -10;

    const MAX_RANGE = 10;


	public function rules() 

	{

		return array(

			array('order', 'numeric', 'min'=>self::MIN_RANGE, 'max'=>self::MAX_RANGE),

		);

	}

}



Also a quick trick to make this more concise:




$range = range(MyModel::MIN_RANGE, MyModel::MAX_RANGE);

echo $form->dropDownList($model, 'order', array_combine($range, $range)); 



Thanks for the help Karl!