객체를 관측 가능 객체에 추가하기 전에 사용자 입력의 유효성을 검사하고 싶습니다. 예를 들어 observable에 객체를 추가하기 전에 Quantity와 Price라는 두 개의 필드가있는 경우 사용자 입력의 유효성을 검사하려고합니다. 이 동작을 어떻게 수행 할 수 있습니까? 지금까지이입력 유효성 검사 후 KnockoutJS에 객체 추가
것은 그래서,이 오류 메시지가 있어야한다 :
self.productPriceAdd = function() {
var newPrice = {
Quantity: self.newProductPriceEntry.Quantity(),
Price: self.newProductPriceEntry.Price(),
ProductBarcode: self.productPrices().Barcode
}
self.productPrices().ProductSalePrices().push(newPrice);
self.productPrices().ProductSalePrices(self.productPrices().ProductSalePrices());
self.newProductPriceEntry.Quantity(null);
self.newProductPriceEntry.Price(null);
}
사용자 인터페이스는 다음과 같이 somethig에 보이는 빈 필드마다 하나씩 표시됩니다.
내 HTML 코드 :
<!-- ko if: productPrices() -->
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title"><b data-bind="text: productPrices().Name"></b></h2>
</div>
<table class="table">
<tr>
<th>
@Html.DisplayName("Quantity")
</th>
<th>
@Html.DisplayName("Price")
</th>
<th></th>
</tr>
<tbody data-bind="foreach: productPrices().ProductSalePrices()">
<tr>
<td>
<b data-bind="text: Quantity"></b>
</td>
<td>
<b data-bind="text: Price"></b>
</td>
<td>
<a href="#" data-bind="click: $parent.productPriceRemove" class="btn btn-defaul">Remove</a>
</td>
</tr>
</tbody>
<tbody data-bind="with: newProductPriceEntry">
<tr>
<td>
<input type="number" class="form-control" data-bind="value: Quantity " placeholder="Quantity">
</td>
<td>
<input type="number" step="0.01" class="form-control" data-bind="value: Price " placeholder="Price">
</td>
<td>
<a href="#" data-bind="click: $parent.productPriceAdd" class="btn btn-defaul">Add</a>
</td>
</tr>
</tbody>
</table>
</div>
<a href="#" data-bind="click: productPriceSave" class="btn btn-defaul">Save</a>
나는 Knockout-Validation을 사용했고 적절한 오류 메시지를 표시하고 있지만 여전히 관찰 할 수있는 변수에 잘못된 개체를 추가 할 수 있습니다 (예 : 빈 필드). 유효성 검사 오류가 있는지 확인하고 관찰 대상에 잘못된 개체를 추가하지 못하도록하는 방법이 있습니까? – sixfeet