Yii2: Problems with yiiActiveForm

Created on 20 Jul 2017  路  6Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

I disabled all Yii dependencies in AppAsset and config/web.php:

    public $css = [
        //'css/site.css',
        'css/ui.css',
        'css/additional.css',
    ];
    public $js = [
        'js/vendor/jquery.min.js',
        'js/vendor/bootstrap.min.js',
        'js/ui.js',
        'js/app.js',
    ];
    public $depends = [
        //'yii\web\YiiAsset',
       // 'yii\bootstrap\BootstrapAsset',
    ];

and

        'assetManager' => [
            'appendTimestamp' => true,
            'bundles' => [
                'yii\web\YiiAsset' => [
                    'js'=>[]
                ],
                'yii\widgets\ActiveFormAsset' => [
                    'js'=>[]
                ],
                'yii\web\JqueryAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapPluginAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapAsset' => [
                    'css' => [],
                ],
            ],
        ],

What is the expected result?

But I still have this JS code at the bottom of pages where I have active forms:

<script type="text/javascript">jQuery(document).ready(function () {
--
聽 | jQuery('#edit-form').yiiActiveForm([], {"validateOnSubmit":false});
聽 | jQuery('#password-form').yiiActiveForm([], {"validateOnSubmit":false});
聽 | });</script>

What do you get instead?

Therefore:

1) I don't have connected yiiActiveForm.js

2) I have JS snippet which tries to call yiiActiveForm.js

3) I have the following error in Chrome's console:

Uncaught TypeError: jQuery(...).yiiActiveForm is not a function

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 5.6.26
| Operating system | Windows 10

Most helpful comment

<?php $form = ActiveForm::begin([
'enableClientScript' => false,
]); ?>
    /**
     * @var bool whether to hook up `yii.activeForm` JavaScript plugin.
     * This property must be set `true` if you want to support client validation and/or AJAX validation, or if you
     * want to take advantage of the `yii.activeForm` plugin. When this is `false`, the form will not generate
     * any JavaScript.
     * @see registerClientScript
     */
    public $enableClientScript = true;

All 6 comments

I.e. active form must check you dependences and generated code must depends on it?

Or you wish avoid the error with fake JQuery plugin like this?

(function ($) {

    $.fn.yiiActiveForm = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.yiiActiveForm');
            return false;
        }
    };

    var methods = {
        init: function (attributes, options) {
        }
    };
})(window.jQuery);


If I fully disabled all Yii JS dependencies, there mustn't be any JS code related to Yii JS.

This JS code is unnecessary in this case:

<script type="text/javascript">jQuery(document).ready(function () {
--
  | jQuery('#edit-form').yiiActiveForm([], {"validateOnSubmit":false});
  | jQuery('#password-form').yiiActiveForm([], {"validateOnSubmit":false});
  | });</script>

@bohdan-vorona If you disable assets this only disables inclusion of yiiActiveForm.js. It does not prevent the init snippet in the HTML source.

Try setting enableClientScript to false on your ActiveForm instead.

<?php $form = ActiveForm::begin([
'enableClientScript' => false,
]); ?>
    /**
     * @var bool whether to hook up `yii.activeForm` JavaScript plugin.
     * This property must be set `true` if you want to support client validation and/or AJAX validation, or if you
     * want to take advantage of the `yii.activeForm` plugin. When this is `false`, the form will not generate
     * any JavaScript.
     * @see registerClientScript
     */
    public $enableClientScript = true;

@bscheshirwork @mikehaertl Thanks for recommendations. It's working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skcn022 picture skcn022  路  3Comments

kminooie picture kminooie  路  3Comments

AstRonin picture AstRonin  路  3Comments

SamMousa picture SamMousa  路  3Comments

Locustv2 picture Locustv2  路  3Comments