Suggestion

Came across this today while i was building some form. Yii has lots of CHtml method and properties but one thing that i missed or doesn't exists is the ability to display a yes/no radio buttons? I know you can display drop downs, checkboxed, textareas, text inputs but didn't really think of any way for doing this for Yes/No radio button with Yii (i did built one home made function to display it but think that Yii should have something built in for this, to ease up things).

Thanks.

Could you please show me your code? I am not quite sure the requirement.

Ahh the full code is in my workstation at work. Will give a full code tomorrow. But should be something like this:



public static function YesNoButton( $name, $defaultval="")


{





	


	$yeschecked = "";


	$nochecked = "";





	if( $defaultval )


	{


		$yeschecked = " checked='checked'";


	}


	else


	{


		$nochecked = " checked='checked'";


	}





	$yes = "Yes <input type='radio' name='{$name}' value='1'{$yeschecked} />";


	$no = "<input type='radio' name='{$name}' value='0'{$nochecked} /> No";





	return $yes . "&nbsp;&nbsp;" . $no;


	


}


Thanks.

So it's actually a special case of radiobutton list.

Do you think CHtml::radioButtonList() helps?

Well it could but then again doing this:

CHtml::YesNoButton('something', 1);

is easier and quicker then this:

CHtml::radioButtonList('something', 1, array('No', 'Yes'));

Cause if you look at it from a programmers perspective it will end up being:

CHtml::radioButtonList('something', 1, array(Yii::t('global', 'No'), Yii::t('global', 'Yes')));

For multi language support and such, eventually it comes to more writing and a good thing a framework should have is to reduce the amount of writing a programmer needs to do and eventually if he copies and pastes things over and over they would rather become a function then copy-paste.

I agree with you that a framework should reduce the amount of typing rather than increasing it. It is often a difficult task to decide whether or not to include a piece of code into the framework. Without restrictions, a framework could easily become a messy package of shortcuts.

Back to this thread topic, I am not sure whether we should include yesNoButton into CHtml. I am actually trying to limit the size of CHtml because it could easily become a monster with more and more helper methods. Maybe we should start with a new static class to hold more shortcut methods like yesNoButton? Or do you have any suggestions about this?

I agree with qiang on not putting a yesNoRadioButton shortcut into the html helper.  It's not general enough, and it's not that much of a shortcut.

Another way is to write custom helpers in the controller (or a base controller) and then call it from the view (the $this variable it the current controller)



blah...


<?php $this->renderYesNoButton(...); ?>


more...


being a base function or not is really no important, I already made my own home made function which is by the way displayed below, So i am set, Yes i know CHtml can be very large so if you don't want to include that's really ok with me like said what ever i need that doesn't exists i just write my own code, if anyone will come across the same issue i am attaching my code so others could use.



	public static function YesNo( $name, $default_val="" ) 


	{


		$random = md5( uniqid( microtime() ) );


		


		$yes = '<label for="yesNo'.$random.'YesInput" class="inline"><input name="'.$name.'" type="radio" class="inline" id="yesNo'.$random.'YesInput" value="1" />'.Yii::t('global', 'Yes').'</label>';


		$no = '<label for="yesNo'.$random.'NoInput" class="inline"><input name="'.$name.'" type="radio" class="inline" id="yesNo'.$random.'NoInput" value="0" />'.Yii::t('global', 'No').'</label>';





		


		if ($default_val == 1)


		{


			$yes = '<label for="yesNo'.$random.'YesInput" class="inline"><input name="'.$name.'" type="radio" class="inline" id="yesNo'.$random.'YesInput" value="1" checked="checked" />'.Yii::t('global', 'Yes').'</label>';


		}


		else


		{


			$no = '<label for="yesNo'.$random.'NoInput" class="inline"><input name="'.$name.'" type="radio" class="inline" id="yesNo'.$random.'NoInput" value="0" checked="checked" />'.Yii::t('global', 'No').'</label>';


		}


		


		


		return '<span class="yes_no">' . $yes.$no . '</span>' ;


	}