2

I want to use CActiveForm's AjaxValidation.

My layout view file was like this before enabling AjaxValidation:

<html lang="tr-TR" dir="ltr">
<head>
<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.js"></script>
</head>

As you see i'm calling jquery framework on my layout page (because i'm using on every page).

And i decided to use CActiveForm's ajax validation. Firstly enable enableAjaxValidation while calling it:

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'otel-form',
    'enableAjaxValidation'=>true,
)); ?>

And then uncomment this on my controller

$this->performAjaxValidation($model);

But i got $(...).yiiactiveform is not a function error. When i check source code of page :

As you see, one more jquery library included, too. So there are 2 jquery files on page. Because of this i'm getting error. Next i put something like this for disabling jquery.

Yii::app()->clientscript->scriptMap['jquery.js'] = false;

Now jquery is loading only once. But this result is :

<html lang="tr-TR" dir="ltr">
<head>
<script type="text/javascript" src="/istanbulcityhotels/assets/cb2686c8/jquery.yiiactiveform.js"></script>
<script src="/istanbulcityhotels/js/jquery.js"></script>
</head>

jquery.yiiactiveform.js calling BEFORE jquery.js . It should called AFTER jquery.js. It confused a bit. What should i do?

ADDITIONAL Yes, i read this question because titles' are really similar, but question isnot same.

Thank you.

3 Answers 3

3

You should not be including jQuery manually from your layout. Instead of doing this, include it from within your Controller base class:

public function init() {
    Yii::app()->clientScript->registerCoreScript('jquery');
}

Don't forget to call parent::init() from within your concrete controllers.

Sign up to request clarification or add additional context in comments.

4 Comments

what do you mean with concrete controllers ? Sorry for bad English. Can you give an example?
@Eray: The classes that derive from Controller, for example UserController. Their init function (if you are using one) needs to call parent::init() -- this is standard PHP stuff but I mention it because it's important.
OK now i get it . I just modified my protected/components/Controller.php file . And than called parent::init() on my all Controllers. Working well. But doing this and adding parent::init() to all controllers is a long way. Is it possible to do this with my configuration file ? FOr example can i use clientScript component on my configuration file? So i can register jquery on my configuration file, and it'll be included every page.
@Eray: No, that's not possible. But consider that you now have a ready-made plan to configure similar things in the future: adding another script everywhere is now one additional line in Controller. Overriding this in a concrete controller, or for just a specific action, is now also super easy (it was not possible before).
1

It seems CActiveForm inserts the scripts before the title tag using CClientScript::POS_HEAD constant. So a workaround is to add this code

<?php
$cs=Yii::app()->clientScript;
$cs->scriptMap=array(
    'jquery.js'=>false
);?>

to the top of the main layout file in order stop it from loading jquery, then put the title tag after you load your jquery file

<script src="<?php echo Yii::app()->request->baseUrl; ?>/js/jquery.js"></script>

This way jquery.yiiactiveform.js will be loaded right after jquery.

Comments

-2

just put your own jquery on tag title, just like this:

<script src="/istanbulcityhotels/js/jquery.js"></script>
<title>your title</title>

1 Comment

Loading jQuery twice won't solve a thing, see yiiframework.com/forum/index.php/topic/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.