Where to set beforeRequiredLabel property?

Hey,

How do I set the CHtml::beforeRequiredLabel once, so it applies to all my forms? I don’t want to set it each time I initiate a form so I was wondering if there is some piece of code that is auto loaded at every request. This might come in handy for other things as well…

Thanks in advance,

Thomas

Hi and welcome to the forums.

One place could be the init() method of your base controller (protected/Controller.php). Remember to call parent::init() if you override. But there are many more possibilities, where to put this code. Depends on e.g. how many default settings you want to change, wether to make it more configurable (e.g. loading config from file), wether to make it more reusable… All this can be done easily, but i’d start simple, so using init() is probably fine.

Hello,

Thank you for your answer. Unfortunately I cannot get it working yet :( I created a controller.php in the protected folder with one function (init):


<?php


class Controller extends CController

{

	public function init()

	{

		parent::init();

		

		echo 'hello';

		echo CHtml::afterRequiredLabel;

	}

}


?>

But nothing gets echoed. I tried __construct as well, but since that doesn’t work either my guess is that the file is never called.

Thomas

I think Mike wanted to say ‘protected/components/Controller.php’, not ‘protected/Controller.php’

Key is right, sorry.

Check out protected/components. Starting from some Version of Yii, the code created from yiic (and gii) always uses a "base controller" in this directory. All your controllers extend from this, so if you need shared functionality, add it to the base controller.

I guessed it could be components/Controller.php too, so I tried that but it gave me an error:


Fatal error: Undefined class constant 'afterRequiredLabel' in C:\xampp\htdocs\eredivisie\protected\components\Controller.php  on line 29





<?php

/**

 * Controller is the customized base controller class.

 * All controller classes for this application should extend from this base class.

 */

class Controller extends CController

{

	/**

	 * @var string the default layout for the controller view. Defaults to 'application.views.layouts.column1',

	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.

	 */

	public $layout='application.views.layouts.column1';

	/**

	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.

	 */

	public $menu=array();

	/**

	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.

	 */

	public $breadcrumbs=array();

	

	public function init()

	{

		parent::init();

		

		echo 'hello';

		echo CHtml::afterRequiredLabel;

	}

}


?>




It must be:


CHtml::$afterRequiredLabel

I see, of course… :rolleyes:

Thanks!