녹아웃 유효성 검사 플러그인을 사용하여 매우 간단한 유효성 검사를 수행하려고합니다. 하나 이상의 텍스트 필드에 텍스트가 있고 하나 이상의 확인란이 선택되어 있는지 확인하고 싶습니다. 모든 바인딩이 올바르게 작동하고 녹아웃 자체가 지금까지 최고입니다. 네이티브 유효성 검사 규칙을 테스트했으며 메시징 작업을합니다. 나는이 2 가지 규칙에 대한 유효성 검사를받을 수 없다.녹아웃 유효성 검사 - 적어도 하나의 필드에 값이 있고 적어도 하나의 확인란이 선택되어 있음
jQuery를 사용하면 빈 값을 쉽게 확인할 수 있지만 실제로 녹아웃을 활용하고 싶습니다. (검증없이 나는 아직 작동 아무것도 발견하지 않았기 때문에)
모델 : 여기
var SearchForm = function(collections) {
// main search fields
this.fullRecord = ko.observable();
this.title = ko.observable();
this.author = ko.observable();
// collections to search
var sources = [];
$.each(collections, function(index,collection) {
sources.push(new Source(collection));
});
this.sources = ko.observableArray(sources);
// Error handling vars
this.errors = ko.validation.group(this);
};
var Source = function(collection) {
$.extend(this,collection);
this.id = "collection-"+this.code;
this.selected = ko.observable(true);
};
난 그냥 서버에서 제공 수집 데이터의 소스 오브젝트의 목록을 만드는거야. 그 데이터는 관측 가능한 'selected'속성에만 관심이 있기 때문에 관련이 없습니다.
마크 업 : 유효성 검사 기능에서
<div id="advanced-controls" class="row">
<div class="col-sm-8">
<fieldset id="search-fields">
<div class="form-group">
<label for="fullrecord" class="control-label">Keywords:</label>
<input type="text" id="fullrecord" class="form-control" name="fullrecord" placeholder="Full Record Search" data-bind="value:fullRecord" />
</div>
<div class="form-group">
<label for="title" class="control-label">Title:</label>
<input type="text" id="title" name="title" class="form-control" data-bind="value:title"/>
</div>
<div class="form-group">
<label for="author" class="control-label">Author:</label>
<input type="text" id="author" name="author" class="form-control" data-bind="value:author"/>
</div>
<div class="form-group">
<button id="advanced-search-submit" class="btn btn-primary" data-bind="click:search">Search</button>
<button id="advanced-search-reset" class="btn" data-bind="click: clear">Clear All</button>
</div>
</fieldset>
</div>
<div class="col-sm-4">
<fieldset data-bind="foreach: sources">
<div class="form-group">
<input type="checkbox" name="collections" data-bind="attr:{ id:id, value:code }, checked:selected, click: $parent.clearRequiredSourceError ">
<label data-bind="attr:{ for:id }, text: name"></label>
</div>
</fieldset>
</div>
</div>
제출하기 전에 :
this.sources = ko.observableArray(sources).extend({
validation: {
validator : function (sources) {
var anySelected = false;
$(sources).each(function(){
anySelected = this.selected();
});
return anySelected;
},
message: 'At least one source is required to search.'
}
});
:이 같은 소스의 관찰 배열에 지정 유효성 검사 확장을 설정하려고했습니다
// If there's any knockout validation errors
if (model.errors().length > 0) {
model.errors.showAllMessages();
isValid = false;
}
그러나 배열이 c 인 경우에만 확인란을 클릭하면 실행되지 않습니다. 교수형 ~ 푸시, 팝, 등등. 그래, 난 올바르게 설정을 설정했습니다 :
ko.validation.configure({
grouping: {
deep: true,
observable: true
}
});
이것은 매우 간단해야 달성 같아요. 어쩌면 내 두뇌가 이번 주 전체 녹아웃 세계로 뛰어 드는 것에서 튀겨 졌을 수도 있습니다. 어떤 제안이라도 대단히 감사하겠습니다. 미리 감사드립니다!
네 말이 맞아. 이것은 혼자 녹아웃을 사용하여 훨씬 쉬웠다. 필자는 필요한 검증 로직을 사용하여 두 개의 계산 된 부울 관측 값을 만들 수있었습니다. 관측 값은 고유 한 오류 메시지 범위에 눈에 띄게 바인딩됩니다. 팁을 주셔서 감사합니다. – headz68
입니다 !! 특수 연산자 또는 그 이중 부정입니까? 여기서 어떻게 작동합니까? –
그것은 아무것도를 부울로 변환합니다. 기본적으로 null, undefined, ''또는 false가 아닌 모든 것은 true를 반환합니다. –