N
N
nepster-web2014-03-06 02:04:42
Yii
nepster-web, 2014-03-06 02:04:42

Conflict js scripts in Yii2 when working with forms, how to fix?

In general, I'm digging into Yii2 and came across this situation:
I broke my template into parts and there is a head.php part where scripts are connected in the usual way:

...
<script type="text/javascript" src="/js/jquery.cookies.min.js"></script>
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/main.js"></script>
...

Next, I needed to add a form to the page:
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
  <?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
     'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
   <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>

So, when the form is connected, it pulls more js scripts that are connected to the bottom of the site:
<script src="/assets/5ff4d296/jquery.js"></script>
<script src="/assets/28da076d/yii.js"></script>
<script src="/assets/28da076d/yii.validation.js"></script>
<script src="/assets/28da076d/yii.captcha.js"></script>
<script src="/assets/28da076d/yii.activeForm.js"></script>
<script type="text/javascript">jQuery(document).ready(function(){
jQuery('#contactform-verifycode-image').yiiCaptcha({"refreshUrl":"\/site\/c
...

And it seems that connecting 2 jquery libraries spoils the work of the scripts that were connected above. Please tell me how to put things in order here? Either disable the jquery connection or how to control it?
I wrote this question in the Russian community, but so far there is silence ... Please help me solve the problem

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Morozov, 2014-03-06
@nepster-web

You have two options:
Option 1. Collect all your scripts in an asset (see /assets/AppAsset.php for an example) and make the asset dependent on JqueryAsset:

public $depends = [
    // 'yii\web\YiiAsset',
    'yii\web\JqueryAsset',
];

Accordingly, register the asset in the right place.
Option 2. In the config, in the "components" section, write:
'assetManager' => [
    'bundles' => [
        'yii\web\JqueryAsset' => [
            'sourcePath' => null,
            'js' => ['//code.jquery.com/jquery-1.11.0-beta3.min.js'] // тут путь до Вашего экземпляра jquery
        ],
    ],
],

Similarly, by the way, you can override any scripts and css in general. It is very convenient in cases when a new version is released (for example, BS 3.1) and in Yii2, for example, it is not expected yet.
PS. Leave the Russian community alone, it's dead. All movement is in English.

S
smileonl, 2014-03-06
@smileonl

For this you need to use ScriptMap www.yiiframework.com/forum/index.php/topic/13988-h...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question