samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.45k stars 296 forks source link

Cannot read property 'attributes' of undefined #174

Closed xiaoliushifu closed 6 years ago

xiaoliushifu commented 6 years ago

today I use add method in yiiActiveForm.js for a attribute, I do it as this below address tell,

https://github.com/samdark/yii2-cookbook/blob/master/book/forms-activeform-js.md

but it always throw Error:

Uncaught TypeError: Cannot read property 'attributes' of undefined
    at n.fn.init.add (yii.activeForm.js:228)
    at n.fn.init.$.fn.yiiActiveForm (yii.activeForm.js:16)
    at HTMLDocument.<anonymous> (VM1026862 update-exchangestatus?serial_code=Psl000053:138)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Function.ready (jquery.min.js:2)
    at HTMLDocument.J (jquery.min.js:2)

I wonder whether the jsFiles order is right. I debut it when it comes here in yii.activeForm.js below:

add: function (attribute) {
            var $form = $(this);
            attribute = $.extend({value: getValue($form, attribute)}, attributeDefaults, attribute);
            $form.data('yiiActiveForm').attributes.push(attribute);
            watchAttribute($form, attribute);
        },

I put my code partially below:

jQuery(document).ready(function(){
        $('#dialogForm').yiiActiveForm('add', {
            id: 'stockoutdetail-damagecause',
            name: 'damagecause',
            container: '.field-stockoutdetail-bear_from',
            input: '#stockoutdetail-damagecause',
            error: '.help-block',
            validate:  function (attribute, value, messages, deferred, $form) {
                if ((function(attr,val) {
                    var radiovalue = $('input[type=radio][name=exchangeStatus]:checked').val();
                    return radiovalue != 2;
                    })(attribute, value))
                { yii.validation.required(value, messages, {"message":"this is a error。"}); }
            }
        });
    });

I know the code above is right,because I modify the original code from Official into my app code. anyone meet this error? why that attributes is undifined ? I need your help!

samdark commented 6 years ago

How's the form configured?

xiaoliushifu commented 6 years ago

Oh,now I have figured it out。as I preveously think that exactly it is the jsFiles order。I change other way to insert my Js code like this:

<?php $this->beginBlock('damagecause');?>
$('#dialogForm').yiiActiveForm('add', {
            id: 'stockoutdetail-damagecause',
            name: 'damagecause',
            container: '.field-stockoutdetail-bear_from',
            input: '#stockoutdetail-damagecause',
            error: '.help-block',
            validate:  function (attribute, value, messages, deferred, form) {
                if ((function(attr,val) {
                    var radiovalue = $('input[type=radio][name=exchangeStatus]:checked').val();
                    return radiovalue != 2;
                    })(attribute, value))
                { yii.validation.required(value, messages, {'message':'some error message。'}); }
            }
});
<?php $this->endBlock();?>
<!--**use View::registerJs**-->
<?= $this->registerJs($this->blocks['damagecause']);?>

View::registerJs will insert my customer js code into the view bottom defaultly ,like this:

Jquery(document).ready(function(){
 // **my customer below here**。
});
</body>

I know that I borrow server's registerJS() method to insert my customer Js code。but I still don't know why I directly use this Jquery(document).ready(function(){}); would throw errors.

samdark commented 6 years ago

Something seems to be not initialized yet.