CActiveForm, jquery

Hi there.

I use jQuery in my project, but I don’t want to rely on jquery catered with the framework, so I publish jquery on my own:


<script src="<?php echo Yii::app()->assets->publish('js/jquery-1.4.2.min.js'); ?>"></script>

But if I use the CActiveForm, it publishes yiiactiveform plugin along with jquery, thus my activeform does not work.




<script type="text/javascript" src="/assets/9c56d13/jquery.js"></script> 

<script type="text/javascript" src="/assets/9c56d13/jquery.yiiactiveform.js"></script>

<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 

<script type="text/javascript" src="/assets/fb562c42/jquery-1.4.2.min.js"></script>

How do I fix it slightly? Any ideas? Steel, I want to manage which jquery lib to use (in case of i need older or earlier version, but not provided by the framework).

you should create a active-form class of yourself which extends CActiveForm, and then override the run() method.

Here is an example, you can set it as protected/components/MyActiveForm




class MyActiveForm extends CActiveForm

{

    public function run()

    {

	echo CHtml::endForm();

	if(!$this->enableAjaxValidation || empty($this->_attributes))

		return;

	$options=$this->clientOptions;

	if(isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))

		$options['validationUrl']=CHtml::normalizeUrl($this->clientOptions['validationUrl']);

	$options['attributes']=array_values($this->_attributes);

	if($this->_summary!==null)

		$options['summaryID']=$this->_summary;

	$options=CJavaScript::encode($options);

        /*

         * Pay attention that this lines below are used to published the yiiactiveform plugin along with jquery.

         */

	//Yii::app()->clientScript->registerCoreScript('yiiactiveform');

	//$id=$this->id;

	//Yii::app()->clientScript->registerScript(__CLASS__.'#'.$id,"\$('#$id').yiiactiveform($options);");

    }

}



for details, you can read the code of framework/web/widgets/CActiveForm.php, framework/web/web/CClientScript.php, framework/web/js/packages.php

Try this in your main layout file:


<?php

Yii::app()->clientScript->scriptMap=array(

	'jquery.js'=>false,

);

?>

Thank you for your answers, GSTARs solution is perfectly fits my needs. I’ve added into my main config:


    'clientScript'=>array(

      'class' => 'CClientScript',

      'scriptMap' => array(

        'jquery.js'=>false,

      ),

      'coreScriptPosition' => CClientScript::POS_BEGIN,

    ),

I had to point scriptPosition to POS_BEGIN cause, if the position is set to default (POS_HEAD), yiiactiveform loads before jquery.

Davidhhuan, I considered this solution once I ran into problem, but I don’t like to produce classes by extending “consistent” classes like CActiveForm. For me it is the same as extending Controller class. I just don’t see it right, so I go with CClientScript::scriptMap

I actually extend “consistent” classes all the time! Even though I usually choose the solution with the least amount of code, I think I like Davidhhuan’s solution better in this case. It seems to me to be more flexible, unless you plan to maintain all of the appropriate jquery includes yourself. I would be worried that my widgets would break. Does overriding the jquery.js scriptMap property, does it also override jquery-ui?

~thinkt4nk

aha, everybody will enjoy what he like. :P

thinkt4nk, in my case I’d just check out why it is broken by checking version of the jquery used in the Yii only. In your case you have to check out whether methods you have overrided did not changed. If they are changed, you have to rewrite all your methods in derived classes. I have to replace jquery lib with the appropriate (usually last one). Maybe it is a matter of what you like more - leave this classes to be maintained by the yii developers, dont touch its methods and find another solution that satisfy your needs or rewrite this methods and always check if they are the same from version to version.

I just did svn update:





$ svn up


Fetching external item into 'framework'

U    framework/logging/CFileLogRoute.php

U    framework/logging/CLogRoute.php    

U    framework/zii/widgets/grid/CDataColumn.php

U    framework/zii/widgets/CDetailView.php 	

U    framework/zii/widgets/jui/CJuiResizable.php

U    framework/zii/widgets/jui/CJuiSlider.php   

U    framework/zii/widgets/jui/CJuiProgressBar.php

U    framework/zii/widgets/jui/CJuiTabs.php   	

U    framework/zii/widgets/jui/CJuiDialog.php 	

U    framework/zii/widgets/jui/CJuiAccordion.php  

U    framework/zii/widgets/jui/CJuiAutoComplete.php

U    framework/zii/widgets/jui/CJuiSliderInput.php 

U    framework/zii/widgets/jui/CJuiSelectable.php  

U    framework/zii/widgets/jui/CJuiDroppable.php   

U    framework/zii/widgets/jui/CJuiDraggable.php   

U    framework/zii/widgets/jui/CJuiDatePicker.php  

U    framework/zii/widgets/jui/CJuiButton.php      

U    framework/zii/widgets/jui/CJuiSortable.php    

U    framework/caching/CApcCache.php

U    framework/caching/CDbCache.php

U    framework/caching/CXCache.php

U    framework/caching/CEAcceleratorCache.php

U    framework/caching/CZendDataCache.php

U    framework/caching/CDummyCache.php

U    framework/caching/CWinCache.php

U    framework/caching/CMemCache.php

U    framework/caching/CFileCache.php

U    framework/caching/CCache.php

U    framework/views/nl/profile-summary.php

U    framework/views/nl/profile-summary-firebug.php

U    framework/views/nl/error400.php

U    framework/views/nl/error500.php

U    framework/views/nl/exception.php

U    framework/views/nl/profile-callstack.php

U    framework/views/nl/error403.php

U    framework/views/nl/profile-callstack-firebug.php

U    framework/views/nl/error503.php

U    framework/views/nl/error404.php

U    framework/views/nl/log-firebug.php

U    framework/views/zh_tw/profile-summary.php

U    framework/views/zh_tw/profile-summary-firebug.php

U    framework/views/zh_tw/profile-callstack.php

U    framework/views/zh_tw/log-firebug.php

U    framework/views/zh_cn/profile-summary.php

U    framework/views/zh_cn/profile-summary-firebug.php

U    framework/views/zh_cn/profile-callstack.php

U    framework/views/zh_cn/log-firebug.php

U    framework/messages/ta_in/yii.php

U    framework/messages/zh_tw/yii.php

U    framework/validators/CRangeValidator.php

U    framework/base/interfaces.php

U    framework/web/CSort.php

U    framework/web/CActiveDataProvider.php

U    framework/web/js/source/jquery.yiiactiveform.js

U    framework/web/CDataProvider.php

U    framework/web/widgets/CAutoComplete.php

U    framework/web/widgets/captcha/CCaptchaAction.php

U    framework/web/widgets/CMaskedTextField.php

U    framework/web/widgets/CStarRating.php

Updated external to revision 2559.


Updated to revision 47.



What you have to do if you have extended

CFileLogRoute.php,

CLogRoute.php,

all the jui classes,

CRangeValidator.php,

CDataProvider.php, etc… ? Sure. You go to your code and compare all the methods you overrided. Thats why I don’t see it right and trying to extend only those classes that are meant to be extended:




class createPost extends CAction {

public function run() {

$controller = $this->controller;

}

}



but not




class MyAction extends CAction {

public function getOwner() { return $this->_controller; }

}

class createPost extends MyAction {

public function run() {

$controller = $this->owner;

}

}



If I need to add new functions to this classes, I will better attach behaviors.




class OwnerBehavior extends CBehavior {

public function getOwner() {

return $this->_controller;

}

}


class createPost extends CAction {

public function run() {

$this->attachBehavior('OwnerBehavior');

$controller = $this->owner;

}

}