Custom validation rule

Custom validation rule

Old forum URL: forums.lhotka.net/forums/t/6487.aspx


Fel posted on Monday, February 23, 2009

I would like to implement field that will validate entered data against code-description table. If entered value is incorrect, error icon should be visible. Otherwise, description for entered value should be displayed in another field.
I tried to implement this scenario by writing custom validation rule. It works fine regarding error.
Server call that validate entered code returns description if value is correct.
Then I have description but I can't find a way to pass this value to class variable because custom method called by business rules is static.

I appreciate any suggestion.

Thanks

Fel

rsbaker0 replied on Monday, February 23, 2009

I'm not sure from your description specifically what you are trying to pass, but if your object is broken in general, then you can get the text description of what is broken using the BrokenRulesCollection property of the object. 

You can get the string equivalent of all the broken rules or interrogate the list for a broken rule for specific property, etc.

Fel replied on Monday, February 23, 2009

When rule is broken, I'm getting correct error message.

This is my custom validation method:
private static void IsValidCode(AsyncValidationRuleContext context)
        {
            CommandCheckCode command = new CommandCheckCode(context.PropertyValues["OrgCode"].ToString());
            DataPortal<CommandCheckCode> dp = new DataPortal<CommandCheckCode>();
            dp.ExecuteCompleted += (o, e) =>
                {
                    if (e.Error != null)
                    {
                        context.OutArgs.Description = "Error during checking for valid Code (" + e.Error.ToString();
                        context.OutArgs.Severity = RuleSeverity.Error;
                        context.OutArgs.Result = false;
                    }
                    else
                    {
                        if (!e.Object.IsValid)
                        {
                            context.OutArgs.Description = "Invalid Code";
                            context.OutArgs.Severity = RuleSeverity.Error;
                            context.OutArgs.Result = false;
                        }
                        else
                        {
                            context.OutArgs.Result = true;
                            // e.Object.CodeName <-- code description
                        }
                    }
                    context.Complete();
                };
            dp.BeginExecute(command);
        }
        
I would like to pass value in e.Object.CodeDesc to variable in hosting BB class.

Copyright (c) Marimer LLC