问题
I am retrieving an entity from the database with some proprieties.
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 ""
.
In the EF mappings the propriety is validated like:
this.Property(t => t.ColumnName)
.IsRequired()
.HasMaxLength(50);
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"
).
Is this accepted behaviour? Shouldn't the error be thrown only if the the ColumnName
would be null
or undefined
?
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
回答1:
This has nothing to do with how Breeze saves data.
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");
来源:https://stackoverflow.com/questions/19658297/how-does-breeze-saves-empty-strings