Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd validation rule to model property dynamically in asp.net mvc2
    primarykey
    data
    text
    <p>one model property validation info/rules are available in the other properties of same model object. i have to validate the property values using other properties values.</p> <p><strong>Model class:-</strong></p> <pre><code> [FormatValidtion("Value", "Format", "MinLength", "MaxLength", ErrorMessage = "Please enter correct format")] public class Sample { #region ModelProperties public string Id { get; set; } [Required(ErrorMessage = "Please Enter Value")] public string Value { get; set; } public string Format { get; set; } public string MinLength { get; set; } public string MaxLength { get; set; } #endregion } </code></pre> <p>Model object looks like above.Here i have to validate <strong>Value</strong> property using the </p> <ul> <li><p><strong>Format</strong> ( Fomart Validation like email,phone &amp; Date)</p></li> <li><p><strong>MinLength,MaxLength</strong> (Range validation) properties.</p></li> </ul> <p>I know we can do this using custom validations like </p> <ol> <li>Create Custom class keeping <strong>ValidationAttribute</strong> as base</li> <li>Pass all these properties( Value ,Format,MinLength &amp; MaxLength)</li> <li>Write switch case with Format property.</li> <li>Validate Format using Regex and do manual range validation or with dynamic Regex validation.</li> </ol> <p><strong>EDIT :- Added Custom Validation Class Code Here</strong></p> <p><strong>Custom Validation Class:</strong></p> <pre><code>[AttributeUsage(AttributeTargets.Class)] public class FormatValidtion : ValidationAttribute { public String Value { get; set; } public String Format { get; set; } public String MinLength { get; set; } public String MaxLength { get; set; } public FormatValidtion(String value, String format, String minLength, String maxLength) { Value = value; Format = format; MinLength = minLength; MaxLength = maxLength; } public override Boolean IsValid(Object value) { Type objectType = value.GetType(); PropertyInfo[] neededProperties = objectType.GetProperties() .Where(p =&gt; p.Name == Value || p.Name == Format || p.Name == MinLength || p.Name == MaxLength) .ToArray(); if (neededProperties.Count() != 4) { throw new ApplicationException("PropertiesMatchAttribute error on " + objectType.Name); } Boolean isMatched = true; switch (Convert.ToString(neededProperties[1].GetValue(value, null))) { case "Alpha": Regex regExAlpha = new Regex(@"/^[A-Za-z]+$/"); if (!regExAlpha.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = false; } break; case "Number": Regex regExNumber = new Regex(@"/^[0-9]+$/"); if (!regExNumber.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = false; } break; case "AlphaNum": Regex regExAlphaNum = new Regex(@"/^[a-zA-Z][a-zA-Z0-9]+$/"); if (!regExAlphaNum.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = false; } break; default: isMatched = false; break; } return isMatched; } } </code></pre> <p>This is working correctly at class level and showing validation messages through validation summary. But I want to modify the above code to do the following things.</p> <ol> <li>Validation should happen at property level or some how need to show error message at property level( using validationMessage helper class).</li> <li>Need to provide the specific error messages for each validation other than common error message.</li> <li>Range validation should happen.</li> </ol> <p>Can anybody provide some thoughts around these?</p>
    singulars
    1. This table or related slice is empty.
    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. 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