2013-07-04 17 views
8

Hiện nay tôi có một thuộc tính xác nhận tùy chỉnh được gọi ExistingFileName (dưới đây) nhưng tôi đã cho nó thông báo lỗi để hiển thịLàm cách nào để tùy chỉnh thông báo lỗi thuộc tính xác thực?

protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext) 
    { 
     if (value!=null) 
     { 
      string fileName = value.ToString(); 
      if (FileExists(fileName)) 
      { 
       return new ValidationResult("Sorry but there is already an image with this name please rename your image"); 
      } 
      else 
      { 
       return ValidationResult.Success; 
      } 
     } 
     else 
     { 
      return new ValidationResult("Please enter a name for your image"); 
     } 
    } 

Tôi đã thực hiện nó như vậy:

[ExistingFileName] 
public string NameOfImage { get; set; } 

Tôi chắc chắn có cách xác định thông báo lỗi khi đặt thuộc tính như sau:

[ExistingFileName(errormessage="Blah blah blah")] 
public string NameOfImage { get; set; } 

Nhưng tôi không chắc chắn như thế nào? Bất kỳ trợ giúp nào được đánh giá cao

Trả lời

13

Thay vì trả lại ValidationResult bằng chuỗi được xác định trước, hãy thử sử dụng thuộc tính ErrorMessage hoặc bất kỳ thuộc tính tùy chỉnh nào khác. Ví dụ:

private const string DefaultFileNotFoundMessage = 
    "Sorry but there is already an image with this name please rename your image"; 

private const string DefaultErrorMessage = 
    "Please enter a name for your image"; 

public string FileNotFoundMessage { get; set; } 

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{ 
    if (value!=null) 
    { 
     string fileName = value.ToString(); 
     if (FileExists(fileName)) 
     { 
      return new ValidationResult(FileNotFoundMessage ?? 
             DefaultFileNotFoundMessage); 
     } 
     else 
     { 
      return ValidationResult.Success; 
     } 
    } 
    else 
    { 
     return new ValidationResult(ErrorMessage ?? 
            DefaultErrorMessage); 
    } 
} 

Và trong chú thích của mình:

[ExistingFileName(FileNotFoundMessage = "Uh oh! Not Found!")] 
public string NameOfImage { get; set; } 

Nếu bạn không explicitely thiết lập một thông báo tùy chỉnh, nó sẽ dự phòng để hằng số được xác định trước trong thuộc tính tùy chỉnh của bạn.

3

Bạn đã được kế thừa từ ValidationAttribute?

thì bạn không cần giữ nó trong một biến riêng biệt. Tất cả mã thông báo lỗi đều có sẵn khi bạn kế thừa từ lớp ValidationAttribute.

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
public class ExistingFileNameAttribute : ValidationAttribute 
{ 
    public string FileFoundMessage = "Sorry but there is already an image with this name please rename your image"; 
    public ExistingFileNameAttribute() 
     : base("Please enter a name for your image") 
    {    
    } 

    public override ValidationResult IsValid(object value) 
    { 
     if (value!=null) 
     { 
      string fileName = value.ToString(); 
      if (FileExists(fileName)) 
      { 
       return new ValidationResult(FileFoundMessage); 
      } 
      else 
      { 
       return ValidationResult.Success; 
      } 
     } 
     else 
     { 
      return new ValidationResult(ErrorMessage); 
     } 
    } 
} 

Bây giờ bạn có thể sử dụng để xác nhận các lĩnh vực của bạn/tài sản

[ExistingFileName(ErrorMessage="Blah blah blah", FileFoundMessage = "Blah Bla")] 
public string NameOfImage { get; set; } 

và nếu bạn sử dụng nó như dưới đây.

[ExistingFileName] 
public string NameOfImage { get; set; } 

sau đó, nó sẽ sử dụng các thông báo lỗi mặc định trong các nhà xây dựng của ExistingFileName thuộc tính

Hy vọng rằng sẽ giúp.

+0

Rất nhiều, cảm ơn bạn vì điều đó. –

+0

'IsValid' của bạn là nghĩa vụ phải trả về một bool, nhưng bạn đang trả về một' ValidationResult'. Điều này có đúng không? Tôi không thể làm điều đó để làm việc, và tôi không thể ghi đè 'IsValid' để trả về' ValidationResult'. – muttley91

+0

Cảm ơn bạn đã chỉ ra, loại trả về phải là ValidationResult – Amila