none
Unable to pass Parameter Value in CustomValidation Attribute RRS feed

  • Question

  • Unable to pass Parameter Value in CustomValidation Attribute

    public string Test { get; set; } = "ValidateSpecialCharacters";
        
              [CustomValidation(typeof(ValidationTextBox), Test)]
             public string GeneralDescription
             {
                 get => _generalDescription;
                 set
                 {
                     SetValue(ref _generalDescription, value);
                     TestBorderBrush = ValidationDictionaryCollection["GeneralDescription"] != string.Empty
                         ? new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x00, 0x00))
                         : new SolidColorBrush(Color.FromArgb(102, 0, 0, 0));
                 }
             }
        
             public static ValidationResult ValidateSpecialCharacters(string zip)
             {
                 var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
        
                 return regexItem.IsMatch(zip)
                     ? ValidationResult.Success
                     : new ValidationResult("Cannot have Special characters");
             }
    

    I dont want Property to be Constant

    Sunday, June 21, 2020 7:13 AM

Answers

  • Consider this approach too:

    public static Func<string, ValidationResult> Validator = ValidateSpecialCharacters;

    [CustomValidation( typeof( ValidationTextBox ), "MyValidation" )]

    public string GeneralDescription

    {

           . . .

    }

    public static ValidationResult MyValidation( string zip )

    {

           return Validator?.Invoke( zip );

    }

    public static ValidationResult ValidateSpecialCharacters( string zip )

    {

           . . .

    }

    • Proposed as answer by CoolDadTxModerator Friday, July 10, 2020 2:04 PM
    • Marked as answer by ID GO Sunday, July 12, 2020 3:29 AM
    Sunday, June 21, 2020 10:41 AM

All replies

  • I dont want Property to be Constant

    It is not feasible. The compiler cannot pass a non-constant to the constructor of the attribute because this value needs to be know at compile-time, but your property doesn't have a value at this time.

    You will have to resort to some other trick. For example, you can pass a constant that is the name of a fixed method, and then, within that method, you user Reflection to invoke the method whose name is written in your Test property.

    Sunday, June 21, 2020 8:20 AM
    Moderator
  • Consider this approach too:

    public static Func<string, ValidationResult> Validator = ValidateSpecialCharacters;

    [CustomValidation( typeof( ValidationTextBox ), "MyValidation" )]

    public string GeneralDescription

    {

           . . .

    }

    public static ValidationResult MyValidation( string zip )

    {

           return Validator?.Invoke( zip );

    }

    public static ValidationResult ValidateSpecialCharacters( string zip )

    {

           . . .

    }

    • Proposed as answer by CoolDadTxModerator Friday, July 10, 2020 2:04 PM
    • Marked as answer by ID GO Sunday, July 12, 2020 3:29 AM
    Sunday, June 21, 2020 10:41 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 10, 2020 8:37 AM