wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
435 stars 438 forks source link

Masked input global variable error #132

Open YGugnin opened 8 years ago

YGugnin commented 8 years ago

https://github.com/yiisoft/yii2/blob/master/framework/widgets/MaskedInput.php#L149 this line put code to jQuery ready block, so your code not work here :

https://github.com/wbraganca/yii2-dynamicform/blob/master/src/assets/yii2-dynamic-form.js#L346

(inputmask_a43b1ce9 is not defined)

YGugnin commented 8 years ago

https://github.com/yiisoft/yii2/issues/11170

kidzen commented 8 years ago

i guess this is my problem to...

atakajlo commented 8 years ago

Did anyone found the solution?

YGugnin commented 8 years ago

I have changed original widget, and remove it from composer. Same for masked input. If this normal for you, you can use it common.zip This is temporary solution, so I don't want put it to git sources, and have no time for best solution. Sorry.

P.S. You have to add next rows to composer for use it

"symfony/dom-crawler": "~2.6.6", "symfony/css-selector": "~2.6.6"

azbuco commented 8 years ago

I think the simpliest solution is to extend MaskedInput and overwrite the hashPluginOptions method. To move the maskedinput configuration outside the jquery block change last line from

$view->registerJs("var {$this->_hashVar} = {$encOptions};", View::POS_READY);

to

$view->registerJs("var {$this->_hashVar} = {$encOptions};", View::POS_END);

fonemi commented 7 years ago

My solution is (as azbuco suggests)

<?php

namespace app\components;

use yii\web\View;
use yii\helpers\Json;

class MaskedInput extends \yii\widgets\MaskedInput
{
    /**
     * @inheritdoc
     */
    protected function hashPluginOptions($view)
    {
        $encOptions = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
        $this->_hashVar = self::PLUGIN_NAME . '_' . hash('crc32', $encOptions);
        $this->options['data-plugin-' . self::PLUGIN_NAME] = $this->_hashVar;
        $view->registerJs("var {$this->_hashVar} = {$encOptions};", View::POS_END);
    }
}
contreras2004 commented 7 years ago

Holly shit, how do you come up with this solutions. @fonemi's solution works great. Hope this gets fixed in the future.

nagaokashi commented 7 years ago

@fonemi I tried using $view->registerJs("var {$this->_hashVar} = {$encOptions};", View::POS_END); inside vendor\yiisoft\yii2\widgets\MaskedInput.php, it works fine. As your solution, I have created a MaskedInput.php in components, but I dont know how to execute this component ?

contreras2004 commented 7 years ago

@nagaokashi I think it's not a good idea to modify the files in the Vendor folder. Just create a new custom class using what @fonemi proposed

nagaokashi commented 7 years ago

@contreras2004 I fixed vender just for testing, and this is solved. I have created a MaskedInput.php in components as @fonemi proposed, same error. How to make MaskedInput.php work ?

contreras2004 commented 7 years ago

I placed the new class just in my backend/models folder and changed the namespace to namespace backend\models; then instead of using use yii\widgets\MaskedInput; in your form you can use the new class you just created use backend\models\MaskedInput;

nagaokashi commented 7 years ago

@contreras2004 I just changed to use app\components\MaskedInput;. Thank you.

nagaokashi commented 7 years ago

Additional, after added the custom class, I met error "initSelect2Loading is not defined". So, following #57 make me solved my problem perfectly.

DaveFerger commented 7 years ago

yiisoft/yii2/pull/14321