tags:

views:

22

answers:

1

I have a class in my SL4 app that represents a single entity the user is inputing data about. I am doing data validation as follows:

    private double cost;
    public string Cost
    {
        get
        {
            return String.Format("{0}{1}", DOLLAR_SYMBOL, cost);
        }
        set
        {
            string price = getPriceFromCost(value);
            if (!double.TryParse(price, out cost))
            {
                throw new ArgumentException("Please enter a number.");
            }

            OnPropertyChanged("Cost");
        }
    }

This works great. However, if the user enters a valid value, then an invalid one, ignores the validation error, and hits submit, the entity will be created with the old valid value. I would rather force the user to correct the error. How can I disable the "Add" button?

Also, I would like to do some other forms of validation when the user clicks the add button, but I still want the nice effect of the text input box being highlighted in red, with the message that pops out. How can I do this without throwing ArgumentExceptions?

A: 

You could toggle a bool along with throwing you exception and bind the IsEnabled of your Button to that bool.

You can do almost any validation task with the WCF RIA Services - I suggest you begin with Jeff Handleys latest blog posts and take it from there. http://jeffhandley.com/archive/2010/09/22/RiaServicesStandardValidators.aspx

ChristianL
Right, but wouldn't I need a bool for every validated field? `IsValidName`, `IsValidDate`, etc. If the user fails to set the input for `Date`, I don't want to enable the button again until valid input is presented for that field.
Rosarch
@ChristianL that is a very helpful blog post. I've added `[Required]` to several the properties in my data entity class. However, nothing happens in the front end. How can I make this validation occur?
Rosarch