【发布时间】:2014-04-21 00:45:51
【问题描述】:
我正在使用带有 zend 2 的引导程序。 modal 的内容是通过 ajax 调用来获取的。 但是验证根本不起作用,它在不检查任何字段的情况下提交:
这里是模态加载的地方:
<div class="modal fade" id="themesModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
这里是模态内容:
<div class="modal-body">
<div id="themesbox">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">title</div>
</div>
<div style="padding-top: 30px" class="panel-body">
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('user/default', array('controller' => 'users', 'action' => 'create')));
$form->prepare();
echo $this->form()->openTag($form);
?>
<div id="mydiv"
style="display: none" class="alert alert-danger">
<p>Error:</p>
<span></span>
</div>
<div class="form-group">
<label for="name" class="col-md-3 control-label">user</label>
<div class="col-md-9">
<?php echo $this->formElement($form->get('name'));?>
</div>
</div>
<div class="form-group">
<label for="description" class="col-md-3 control-label">password</label>
<div class="col-md-9">
<?php echo $this->formElement($form->get('password'));?>
</div>
</div>
<div style="margin-top: 10px" class="form-group">
<!-- Button -->
<div class="col-sm-12 controls">
<?php echo $this->formSubmit($form->get('submit'));?>
<button id="cancel" class="btn btn-success" data-dismiss="modal">Cancel</button>
</div>
</div>
<?php echo $this->form()->closeTag();?>
</div>
<!-- /.panel-body -->
</div>
<!-- /. panel panel-info -->
</div>
</div>
这里是jquery验证代码:
$('.form-validation').each(function () {
$(this).validate({
errorElement: "span",
errorClass: "help-block",
highlight: function(element) {
$(element).closest('.control-group').removeClass('success').addClass('error');
$(element).attr('style', "border-radius: 5px; border:#FF0000 1px solid;");
},
unhighlight: function(element) {
$(element).addClass('valid').closest('.control-group').removeClass('error').addClass('success');
$(element).attr('style', "border-radius: 4px; border:1px solid #ccc;");
},
errorPlacement: function (error, element) {
$(error).attr('style', "color: #FF0000;");
if ( element.prop('type') === 'checkbox') {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
});
这是表单代码:
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'id' => 'username',
'class' => 'form-control',
'required' => 'true',
'minlength' => '8',
'maxlength' => '20',
),
'options' => array(
'label' => 'name',
),
));
$this->add(array(
'name' => 'description',
'attributes' => array(
'type' => 'password',
'id' => 'password',
'class' => 'form-control',
'required' => 'true',
'minlength' => '8',
'maxlength' => '20',
),
'options' => array(
'label' => 'Password',
),
));
【问题讨论】:
-
不显示 Zend 代码,而是显示浏览器看到的相关 HTML 标记。
-
@Sparky 我可以将包含上述 js 代码的 js 脚本包含在 html 中
-
显示相关的 HTML “证明”该标记适用于 JavaScript。我们还应该如何确定您的 jQuery Validate 脚本不工作的原因?
-
@Sparky 请看我的回答
标签: zend-framework2 jquery-validate twitter-bootstrap-3 bootstrap-modal