Guest
Guest

Reputation: 7

How to load other Jquery version in Yii2 and keep yiactiveform function for validation of fields?

When I load Jquery from CDN, it is working but the problem is I cant do validation of fields that are defined in rules() method. But when I don't load custom Jquery version from CDN then validation is working.

Also, I am getting jQuery(...).yiiActiveForm is not a function in the console when I load Jquery from CDN and if I use inbuilt Jquery then no error.

Upvotes: 0

Views: 239

Answers (1)

dgtal
dgtal

Reputation: 303

You can use jQuery.noConflict for your second, non-yii jQuery and use another var name instead of $. In this example i use jq

var jq = jQuery.noConflict();

// Do something with jQuery
jq( "div p" ).hide();

// Do something with another library's $()
$( ".content" ).hide();

You can add this line in your template after your tag with jQuery.

<script>var jq = jQuery.noConflict();</script>

If you use an Asset, just load an additional .js script with that line.

Anyway, you should avoid this entirely if you can.

From the documentation:

If for some reason two versions of jQuery are loaded (which is not recommended), calling $.noConflict( true ) from the second version will return the globally scoped jQuery variables to those of the first version.

Upvotes: 1

Related Questions