locked
OnValidate How to display multiple error lines RRS feed

  • Question

  • User1719580512 posted

    I want to validate all data at once and displayed all the errors message.

    First I was using partial class and partial method OnSomethingChanging(string value), but this one display error one by one, such as

    public partial class Employer
        {
           
            partial void  OnAddressChanging(string value)
            {
                if (value == null)
                {
                    throw new ValidationException("Address is required!");
                }
            }
            partial void OnZipCodeChanging(string value)
            {
                string x = @"^\d{5}(\-\d{4})?$";
                if (value == null)
                {
                    throw new ValidationException("Zipcode is required!");
                }
                else
                {
                    if (!Regex.IsMatch(value, x))
                    {
                        throw new ValidationException("zipcode must be in format of 11111 or 1111-1234");
                    }
                }
            }

     

    I then attempted to use the OnValidate, I was able to display multiple errors but could not arrange the error line by line.  It just displayed in the same line.  Here is the code and I appreciate very much for your help.  This is a great website and very helpful

             partial void OnValidate(System.Data.Linq.ChangeAction action)
            {
                Boolean displayError = false;
                string errorMessage = null;

                if ((action == System.Data.Linq.ChangeAction.Insert) ||
                   (action == System.Data.Linq.ChangeAction.Update))
                {
                    if (Address == null)
                    {
                        displayError = true;
                        errorMessage = "Address is required <br/>";
                    }
                    if (City == null)
                    {
                        displayError = true;
                        errorMessage = errorMessage + "City is required <br/>";
                    }
                    if (ZipCode == null)
                    {
                        displayError = true;
                        errorMessage = errorMessage + "Zipcode is required <br/>";
                    }
                    else
                    {
                        if (!Regex.IsMatch(ZipCode, @"^\d{5}(\-\d{4})?$"))
                        {
                            displayError = true;
                            errorMessage = errorMessage + "zipcode must be in format of 11111 or 1111-1234";
                        }

                    }

                    if (displayError)
                    {
                        throw new ValidationException(errorMessage);
                    }
                }
            }
        }

     

     

    Friday, September 12, 2008 12:28 PM

All replies

  • User388626632 posted

     You can use

    errorMessage = errorMessage + "\n" + "error";
      

     

    Friday, September 12, 2008 12:59 PM
  • User-284360669 posted

     should be able to concatenate System.Enviroment.NewLine or \n

    Friday, September 12, 2008 1:00 PM
  • User1719580512 posted

     I tried both approach "\n"  and System.Environment.NewLine but it still display like this:

     

    List of validation errors


    Address is required City is required Zipcode is required

     

    Here is my codes:

     partial void OnValidate(System.Data.Linq.ChangeAction action)
            {
                Boolean displayError = false;
                string errorMessage = null;

                if ((action == System.Data.Linq.ChangeAction.Insert) ||
                   (action == System.Data.Linq.ChangeAction.Update))
                {
                    if (Address == null)
                    {
                        displayError = true;
                        errorMessage = "Address is required";
                    }
                    if (City == null)
                    {
                        displayError = true;
                        errorMessage = errorMessage + System.Environment.NewLine + "City is required ";
                    }
                    if (ZipCode == null)
                    {
                        displayError = true;
                        errorMessage = errorMessage + "\n" + "Zipcode is required ";
                    }
                    else
                    {
                        if (!Regex.IsMatch(ZipCode, @"^\d{5}(\-\d{4})?$"))
                        {
                            displayError = true;
                            errorMessage = errorMessage + "\n" + "zipcode must be in format of 11111 or 1111-1234";
                        }

                    }

                    if (displayError)
                    {
                        throw new ValidationException(errorMessage);
                    }
                }
            }
        }

    Friday, September 12, 2008 1:09 PM
  • User-1005219520 posted

    HTML eats extra white space - including new lines. You will have to take that into account. I'll see if I can come up with a way to handle this problem.

    Friday, September 12, 2008 1:39 PM
  • User-284360669 posted

    try the approach of using StrinBuilder

     
     partial void OnValidate(System.Data.Linq.ChangeAction action)
    {
    Boolean displayError = false;
    string errorMessage = null;
    StringBuilder sb = new StringBuilder();

    if ((action == System.Data.Linq.ChangeAction.Insert) ||
    (action == System.Data.Linq.ChangeAction.Update))
    {
    if (Address == null)
    {
    displayError = true;
    sb.Append("Address is required");
    }
    if (City == null)
    {
    displayError = true;
    sb.Append("<br />");
    sb.Append("City is required";
    }
    if (ZipCode == null)
    {
    displayError = true;
    sb.Append("<br />");
    sb.Append("Zipcode is required");
    }
    else
    {
    if (!Regex.IsMatch(ZipCode, @"^\d{5}(\-\d{4})?$"))
    {
    displayError = true;
    sb.Append("<br />");
    sb.Append("
    zipcode must be in format of 11111 or 1111-1234");
    }

    }

    if (displayError)
    {
    throw new ValidationException(sb.ToString());
    }
    }
    }
    }

     
    Friday, September 12, 2008 2:02 PM
  • User-1005219520 posted

    That just appends the literal characters <br /> to the single line.

    Friday, September 12, 2008 2:48 PM
  • User-284360669 posted

     Try replacing all the "<br />" with System.Enviroment.NewLine

    i tried it with your code and it seemed to work for me.

    Friday, September 12, 2008 3:05 PM
  • User-1005219520 posted

     is the same as "\n" - but I tried it any way and still get one line . (I'm using AdventureWorksLT address table and L2S).

    List of validation errors

    • Length < 2 _AddressLine1 String. Length < 2 _City String. Length < 2 _CountryRegion String. zipcode must be in format of 11111 or 1111-1234
    Friday, September 12, 2008 3:30 PM
  • User-284360669 posted

     im not sure what to tell you then. i tried it and it threw the exception with new lines for each null value

    Friday, September 12, 2008 3:36 PM
  • User-330204900 posted

    As far as I know you can get the multi line effect with say the required field, range and RegEx attributes.

    Friday, September 12, 2008 6:46 PM
  • User1719580512 posted

     It's still not working with all of the suggestion from the above.  It keeps displaying all in one line.

    Monday, September 15, 2008 6:54 PM
  • User-1005219520 posted

    Hi PhuongHa ,

    Yes, we are investigating an approach for multiple lines. Unless you need to validate in groups, you can apply validation per property (or per property changing method) and automatically get the nice formatting for each error. See How to: Customize Data Field Validation in the Data Model

    Sometimes you need to validate fields in a group (for example checking the before and after dates are consistent), then you need to use one method to validate multiple data.

    Monday, September 15, 2008 7:10 PM
  • User1719580512 posted

    Ricka6, thank you for your advice and your effort in help me with this. 

    From my codes you can see that I knew how to throw out error once by once.  But sometime validate all data at once is more efficient, instead of go back and forth one by one error, we can display all error at once for user.  The problem is I don't know how to display them in separate lines.   Again, many thanks for all of people's efforts in this forum.  Any suggestion or solution is greatly appreciated. 

    Monday, September 15, 2008 7:23 PM
  • User660823006 posted

    Note that the main issue that you are having is that the ValidationSummary control on the page defaults to putting a bullet next to each error message. This means even if you append multiple error messages together you will not get the behavior you want. However you can set the ValidationSummary control to display as a paragraph instead via the following property:

    DisplayMode="SingleParagraph"

    Then some of the earlier code show that sets the error message with <br> between them will do what you want.

    Tuesday, September 16, 2008 2:41 AM
  • User1719580512 posted

     I tried this way too, but it also displayed in the same line.

    Tuesday, September 16, 2008 5:38 PM
  • User-1005219520 posted

    I'll try to get you complete instructions tomorrow.

    Tuesday, September 16, 2008 5:42 PM
  • User1719580512 posted

     I'm still looking for help on this.  If anyone has any suggestion, please share with me.  Many thanks

    Monday, September 29, 2008 12:46 PM
  • User-1005219520 posted

    Hi PhuongHa ,

    I've opened a bug on this. It appears you cannot get multiple lines from OnValidate. I'll reiterate my previous suggestion, use validation per property (which takes place first), then apply the OnValidate. Sometimes you need to validate in groups, and if you have multiple errors there is no good solution.

    Monday, September 29, 2008 3:06 PM