Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not too sure you'll be able to pass runtime parameters into your attribute.</p> <p>You could use <code>DependencyResolver.Current.GetService&lt;DataAccessHelper&gt;()</code> to resolve your dal (given that you've registered DataAccessHelper)</p> <p>You're probably more likely to have registered DataAccessHelper as IDataAccessHelper or something? in which case you'd call <code>GetService&lt;IDataAccessHelper&gt;</code></p> <pre><code>public class EmailIsUnique : ValidationAttribute { private string _errorMessage = "An account with this {0} already exists"; protected override ValidationResult IsValid(object value, ValidationContext validationContext) { IDataAccessHelper Dal = DependencyResolver.Current.GetService&lt;IDataAccessHelper&gt;(); // too slow bool isValid = true; if(value == null) { isValid = false; _errorMessage = "{0} Cannot be empty"; } else { string email = value.ToString(); if (Dal.User.FindByEmail(email) != null) { isValid = false; } } if (isValid) return ValidationResult.Success; else return new ValidationResult(String.Format(_errorMessage, validationContext.DisplayName)); } } </code></pre> <p>or</p> <pre><code>public class EmailIsUnique : ValidationAttribute { [Dependency] public IDataAccessHelper DataAccess {get;set;} private string _errorMessage = "An account with this {0} already exists"; protected override ValidationResult IsValid(object value, ValidationContext validationContext) { bool isValid = true; if(value == null) { isValid = false; _errorMessage = "{0} Cannot be empty"; } else { string email = value.ToString(); if (DataAccess.User.FindByEmail(email) != null) { isValid = false; } } if (isValid) return ValidationResult.Success; else return new ValidationResult(String.Format(_errorMessage, validationContext.DisplayName)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload