Open YGugnin opened 8 years ago
i guess this is my problem to...
Did anyone found the solution?
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"
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);
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);
}
}
Holly shit, how do you come up with this solutions. @fonemi's solution works great. Hope this gets fixed in the future.
@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 ?
@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
@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 ?
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;
@contreras2004 I just changed to use app\components\MaskedInput;. Thank you.
Additional, after added the custom class, I met error "initSelect2Loading is not defined". So, following #57 make me solved my problem perfectly.
yiisoft/yii2/pull/14321
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)