Удалось раскопать кое-что.
1. yiiActiveForm инициализируется после общего списка скриптов inline-кодом, соотвественно если я подключаю файл <script src="/js/testForm.js"></script>через AppAsset, то событие инициализации yiiActiveForm происходит позже, чем вызывается код в указанном файле.
Временно решил проблему через вызов кода по событию $(window).on('load') вместо jQuery(document).ready. В связи с этим вопрос - есть ли способ подключить скрипт "ниже" чем inline-код в yii? Либо, возможно, у yiiActiveForm есть какое-то событие инициализации?
скрипт подключается ТУТ (последняя строчка)
Код: Выделить всё
<script src="/assets/fabadf61/jquery.js"></script>
<script src="/assets/3d6b6680/yii.js"></script>
<script src="/assets/3d6b6680/yii.validation.js"></script>
<script src="/assets/3d6b6680/yii.captcha.js"></script>
<script src="/assets/3d6b6680/yii.activeForm.js"></script>
<script src="/js/testForm.js"></script>
Код инициализации идет ниже:
Код: Выделить всё
<script src="/assets/df91ef16/js/bootstrap.js"></script>
<script type="text/javascript">jQuery(document).ready(function () {
jQuery('#contactform-verifycode-image').yiiCaptcha({"refreshUrl":"\/index.php?r=site%2Fcaptcha\u0026refresh=1","hashKey":"yiiCaptcha\/site\/captcha"});
jQuery('#contact-form').yiiActiveForm([{"id":"contactform-name","name":"name","container":".field-contactform-name","input":"#contactform-name","error":".help-block.help-block-error","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Name cannot be blank."});}},{"id":"contactform-email","name":"email","container":".field-contactform-email","input":"#contactform-email","error":".help-block.help-block-error","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Email cannot be blank."});yii.validation.email(value, messages, {"pattern":/^[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/,"fullPattern":/^[^@]*<[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?>$/,"allowName":false,"message":"Email is not a valid email address.","enableIDN":false,"skipOnEmpty":1});}},{"id":"contactform-subject","name":"subject","container":".field-contactform-subject","input":"#contactform-subject","error":".help-block.help-block-error","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Subject cannot be blank."});}},{"id":"contactform-body","name":"body","container":".field-contactform-body","input":"#contactform-body","error":".help-block.help-block-error","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Body cannot be blank."});}},{"id":"contactform-verifycode","name":"verifyCode","container":".field-contactform-verifycode","input":"#contactform-verifycode","error":".help-block.help-block-error","validate":function (attribute, value, messages, deferred, $form) {yii.validation.captcha(value, messages, {"hash":642,"hashKey":"yiiCaptcha/site/captcha","caseSensitive":false,"message":"The verification code is incorrect."});}}], []);
jQuery('#w2-success-0').alert();
});</script>
Есть ветка форума на аналогичную тему, но прямого ответа нет
http://www.yiiframework.ru/forum/viewtopic.php?t=17037
2. В подключаемом файле, произвожу добавление произвольного поля к стандартной форме $('#contact-form') и подключаю валидацию - все работает. Но насколько корректно делать это именно таким способом, есть ли автоматизация в шаблоне формы по созданию динамических полей или их прототипов (по типу template)?
Код: Выделить всё
$(window).on('load', function() {
$('<div class="form-group field-contactform-address required"> \
<label for="contactform-name" class="control-label">Address</label> \
<input type="text" name="ContactForm[address]" class="form-control" id="address"> \
<p class="help-block help-block-error"></p> \
</div>').appendTo($('#contact-form'));
$('#contact-form').yiiActiveForm('add', {
'id': 'address',
'name': 'address',
'container': '.field-contactform-address',
'input': '#address',
'error': '.help-block.help-block-error',
'validate': function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages, {"message":"Address cannot be blank."});}
});
});