locked
Help with Validator block RRS feed

  • Question

  • User59134197 posted

    I understand how to do the simple validations when the validation of one property doesn't depend on the values of others. But when it comes to validating property X based on property Y, I'm a little lost. Here's my contrived example:

    public MyEnums.ShapeType Shape { get; set; }  // enum: Circle, Square, Triangle

    public MyEnums.ShapeColor Color { get; set; }  // enum: Red, Green, Blue

    I want to enforce this business rule: Color is a required field only if the Shape is Triangle. Can that be done declaratively? What sort of attributes can be applied to the Color property to enforce my business rule?

    Thanks!

    Thursday, November 26, 2009 11:06 PM

All replies

  • User220959680 posted

    MSDN http://msdn.microsoft.com/en-us/library/dd140088.aspx. I copied the below from MSDN

    You can also group validators together in a rule set. A rule set allows you to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields, properties, and nested objects.

    By using the Validation Application Block, you can perform validation and create rule sets in the following three ways:

    • Using configuration
    • Using attributes
    • Using code
    private void acceptButton_Click(object sender, EventArgs e)
    {
        Customer customer = new Customer
        {
            FirstName = firstNameTextBox.Text,
            LastName = lastNameTextBox.Text,
            SSN = ssnTextBox.Text,
            Address = new Address
            {
                StreetAddress = streetAddressTextBox.Text,
                City = cityTextBox.Text,
                State = stateComboBox.Text,
                ZipCode = zipCodeTextBox.Text
            }
        };
    
        ValidationResults results =
            this.alternativeValidation.Checked
                ? Validation.Validate(customer, "Alternative")
                : Validation.Validate(customer);
        if (!results.IsValid)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("Customer is not valid:");
            foreach (ValidationResult result in results)
            {
                builder.AppendLine(
                    string.Format(
                        CultureInfo.CurrentCulture,
                        "{0}: {1}",
                        result.Key,
                        result.Message));
            }
            MessageBox.Show(
                this,
                builder.ToString(),
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            return;
        }
    
        MessageBox.Show(
            this,
            "Processing customer '" + customer.FirstName + "'",
            "Working",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }


     

    Saturday, November 28, 2009 7:36 AM