我有一个具有addmore
功能的表单。单击 addmore 时,一组 html 输入字段会附加到表单中,如果用户想要删除添加的行,他/她也可以这样做。一切正常,只是面临删除附加 html 后,附加字段的验证仍然有效的问题。
我正在使用带有引导程序的ValidaionsJs进行验证。
下面是我得到的错误
类型错误:$message 未定义
$allErrors = $message.find('.' + this.options.err.clazz.split(' ').join('.') +...
var $parent = $field.closest(row),
$message = $field.data(ns + '.messages'),
$allErrors = $message.find('.' + this.options.err.clazz.split(' ').join('.') + '[data-' + ns + '-validator][data-' + ns + '-for="' + field + '"]'),
$errors = validatorName ? $allErrors.filter('[data-' + ns + '-validator="' + validatorName + '"]') : $allErrors,
$icon = $field.data(ns + '.icon'),
// Support backward
container = ('function' === typeof (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container))
? (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container).call(this, $field, this)
: (this.options.fields[field].container || this.options.fields[field].err || this.options.err.container),
isValidField = null;
这是我的html
<fieldset class="col-md-6">
<a href="javascript:void(0)" class="remove-addmore high-education fa fa-trash-o" alt="Remove" title="Remove" onclick="removeAddmore(this)"></a>
<div class="ph10"><legend>Higher Education</legend> </div>
<div class="form-group ph10 mt10">
<label for="">Title</label>
<?php echo $this->Form->input('DoctorEducation.' . $count . '.title', array('placeholder' => 'ex. DM or Diploma', 'label' => false, 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'))); ?>
</div>
<?php echo $this->Form->input('DoctorEducation.' . $count . '.id');
echo $this->Form->hidden('DoctorEducation.' . $count . '.user_id', array('value' => $userID));
?>
<div class="form-group ph10 mt10">
<?php
$data = array(3);
$GraduationDegries = array();
foreach ($data as $idValue) {
$GraduationDegries[$idValue] = $degreeTypes[$idValue];
}
$degreeTypes = $GraduationDegries;
echo $this->Form->input('DoctorEducation.' . $count . '.degree_type_id', array('empty' => 'Select Course', 'label' => 'Course', 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'options' => $degreeTypes));
?>
</div>
<div class="form-group ph10 mt10">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.college_hospital', array('label' => 'College / Hospital Name', 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'))); ?>
</div>
<div class="form-group">
<label for="" class="ph10">Duration</label>
<div class="clearfix">
<div class="col-lg-6">
<div class="input-group year">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.start_date', array('placeholder' => 'Start Year', 'type' => 'text', 'label' => false, 'div' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'div' => false)); ?>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="col-lg-6">
<div class="input-group year">
<?php echo $this->Form->input('DoctorEducation.' . $count . '.end_date', array('placeholder' => 'End Year', 'div' => false, 'type' => 'text', 'label' => false, 'class' => 'form-control', 'data-fv-notempty-message' => __('notEmpty'), 'div' => false)); ?>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</fieldset>
这是我用来删除 HTML 的 JS
function removeAddmore(_this) {
// $(_this).closest('fieldset').remove();
var classname = $(_this).attr('class');
if (classname.indexOf("post-education") >= 0) {
console.log($("#higher_edu_count").val());
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
$(_this).closest('fieldset').remove();
} else if (classname.indexOf("past-exprience") >= 0) {
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
$(_this).closest('fieldset').remove();
} else if (classname.indexOf("high-education") >= 0) {
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
$(_this).closest('fieldset').remove();
}
}
删除 HTML 后有什么方法可以停止验证。
请让我知道有什么我必须提及的以进行更多说明
有一些有用的链接。
http://formvalidation.io/examples/switching-validators-same-field/
http://formvalidation.io/examples/adding-dynamic-field/
http://formvalidation.io/examples/ignoring-validation/更新
尝试以下代码但没有成功
function removeAddmore(_this) {
// $(_this).closest('fieldset').remove();
var classname = $(_this).attr('class');
if (classname.indexOf("post-education") >= 0) {
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
// $(_this).closest('fieldset').remove();
$('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][title]', false);
$('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][degree_type_id]', false);
$('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][college_hospital]', false);
$('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][start_date]', false);
$('#education').formValidation('enableFieldValidators', 'data[DoctorEducation][1][end_date]', false);
} else if (classname.indexOf("past-exprience") >= 0) {
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
$(_this).closest('fieldset').remove();
} else if (classname.indexOf("high-education") >= 0) {
var val = $("#higher_edu_count").val();
$("#higher_edu_count").val(val - 1);
$(_this).closest('fieldset').remove();
}
}