Breeze如何保存空字符串 [英] how does Breeze saves empty strings
问题描述
我正在从数据库中检索具有某些属性的实体。
I am retrieving an entity from the database with some proprieties.
其中一个属性不允许在数据库中为非null字符串。即使不为null,它也可以是空字符串
。
One of the proprieties is not allowed to be not null string in the database. Even though is not null it can be an empty string ""
.
在EF映射中,正确性的验证方式如下:
In the EF mappings the propriety is validated like:
this.Property(t => t.ColumnName)
.IsRequired()
.HasMaxLength(50);
问题是,当我尝试使用Breeze保存更改时,它会返回元素错误 ColumnName是必需的
)。
The problem is that when I am trying to save the changes with Breeze it returns an error for the elements that have the propriety ColumnName
equal to an empty string.(saying "ColumnName is required"
).
这是公认的行为吗?仅当 ColumnName
为 null
或 undefined <时才引发错误/ code>?
Is this accepted behaviour? Shouldn't the error be thrown only if the the ColumnName
would be null
or undefined
?
Breezejs引发的错误:
The error Breezejs throws:
valError: Error
entityErrors: Array[5]
0: Object
entity: Object
ColumnName: function dependentObservable() {
__ko_proto__: function (evaluatorFunctionOrOptions, evaluatorFunctionTarget, options) {
_latestValue: ""
_subscriptions: Object
...
...
entityAspect: ctor
__proto__: Object
errorMessage: "'ColumnName' is required"
errorName: "required"
isServerError: false
propertyName: "ColumnName"
__proto__: Object
推荐答案
Breeze如何保存数据。
This has nothing to do with how Breeze saves data.
您遇到的是Breeze的验证逻辑。 Breeze默认将空字符串视为空,以验证必填字段。您可以通过替换Breeze的必填验证器来更改此设置,以简单地将必填视为非空。
What you are encountering is Breeze's validation logic. Breeze defaults to treating empty strings as nulls for the purposes of validating "required" fields. You can change this by replacing Breeze's required validator to simply treat required as meaning "not null".
Validator.required = function (context) {
var valFn = function (v, ctx) {
return v != null;
}
return new Validator("required", valFn, context);
};
// register the new validator so that metadata can find it.
Validator.registerFactory(Validator.required, "required");
这篇关于Breeze如何保存空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!