Yii Framework Forum: Where to set beforeRequiredLabel property? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Where to set beforeRequiredLabel property? Rate Topic: -----

#1 User is offline   ThomasB 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-May 10

Posted 06 May 2010 - 04:02 PM

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
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 07 May 2010 - 01:55 AM

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.
0

#3 User is offline   ThomasB 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-May 10

Posted 07 May 2010 - 12:17 PM

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
0

#4 User is offline   Key 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 07-May 10

Posted 07 May 2010 - 07:30 PM

I think Mike wanted to say 'protected/components/Controller.php', not 'protected/Controller.php'
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 08 May 2010 - 03:42 AM

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.
0

#6 User is offline   ThomasB 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-May 10

Posted 08 May 2010 - 05:48 AM

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;
	}
}

?>


0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 08 May 2010 - 07:10 AM

It must be:

CHtml::$afterRequiredLabel

0

#8 User is offline   ThomasB 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-May 10

Posted 08 May 2010 - 12:00 PM

I see, of course.. :rolleyes:

Thanks!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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