Tôi có một lớp khách hàng có cả thuộc tính PhoneNumber và Email. Sử dụng DataAnnotations tôi có thể trang trí các thuộc tính với các thuộc tính xác nhận DataType, nhưng tôi không thể thấy những gì đang nhận được tôi.Có một tham chiếu tốt cho chú thích dữ liệu liên quan đến cách DataType hoạt động không?
Ví dụ:
[DataType(DataType.PhoneNumber)]
public string PhoneNumber {get; set;}
Tôi có một bài kiểm tra đơn vị được phân công "1515999A" vào thuộc tính này. Khi tôi bước qua nhân tố xác thực, giá trị được coi là hợp lệ cho một số điện thoại. Tôi đã nghĩ rằng điều này sẽ không hợp lệ.
Tôi google'd xung quanh một số nhưng không thể tìm thấy một lời giải thích phong nha về những gì các DataTypes liệt kê khác nhau thực sự bắt. Có một tài liệu tham khảo đáng giá ở đâu đó không?
Edit:
Dưới đây là ruột của những gì tôi đang sử dụng cho một Á hậu xác nhận ...
public virtual XLValidationIssues ValidateAttributes<TEntity>(TEntity entity)
{
var validationIssues = new XLValidationIssues();
// Get list of properties from validationModel
var props = entity.GetType().GetProperties();
// Perform validation on each property
foreach (var prop in props)
ValidateProperty(validationIssues, entity, prop);
// Return the list
return validationIssues;
}
protected virtual void ValidateProperty<TEntity>(XLValidationIssues validationIssues, TEntity entity, PropertyInfo property)
{
// Get list of validator attributes
var validators = property.GetCustomAttributes(typeof(ValidationAttribute), true);
foreach (ValidationAttribute validator in validators)
ValidateValidator(validationIssues, entity, property, validator);
}
protected virtual void ValidateValidator<TEntity>(XLValidationIssues validationIssues, TEntity entity, PropertyInfo property, ValidationAttribute validator)
{
var value = property.GetValue(entity, null);
if (!validator.IsValid(value))
validationIssues.Add(new XLValidationIssue(property.Name, value, validator.FormatErrorMessage(property.Name, value)));
}
Đây có phải là trong khung 3.5 hoặc 4.0 không? Bạn đang sử dụng trình duyệt xác thực nào? –
Nhận xét hay. Tôi đang sử dụng 3,5 và tôi đang sử dụng một Á hậu xác nhận tùy chỉnh khá đơn giản (đăng mã ở trên). –