locked
Changing validation rules programmatically RRS feed

  • Question

  • User-853298616 posted

     I need to apply different validation methods/rules depending on some conditions.

    To be more specific, what I need to do is make some fields required, but only in certain cases. I'm looking for a way to check for those cases in the codebehind and somehow "appending" the validator when the page is loading, or something like that.

    Is this possible?

    Monday, October 27, 2008 12:25 PM

Answers

  • User-330204900 posted

    L2S make validation in the Business layer easyer if its possible to do your checks there you may find it easyer here's an example: 

    //Business Logic example:
    public partial class NorthwindDataContext : System.Data.Linq.DataContext
    {
        partial void InsertCustomer(Customer instance)
        {
            var NWDC = new NorthwindDataContext();
            if (NWDC.Customers.SingleOrDefault(c => c.CustomerID == instance.CustomerID) != null)
            {
                throw new ValidationException("Duplicate CustoemrID is not allowed");
            }
            else
            {
                // finally send this to the DB
                this.ExecuteDynamicInsert(instance);
            }
        }
    }

    Obviously you could also check you other conditions in the if statment and the beauty is that any error will be shown on the page in the validation. [:D]

    Hope this helps [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 4:13 PM

All replies

  • User-330204900 posted

    How easy or hard all depends on wether you are using Linq to SQL or Entity Frameork.

    Monday, October 27, 2008 4:03 PM
  • User-853298616 posted
    I'm using LINQ to SQL (when I started the project the Entity Framework wasn't yet working properly with Dynamic Data, unfortunately).
    Monday, October 27, 2008 4:06 PM
  • User-330204900 posted

    L2S make validation in the Business layer easyer if its possible to do your checks there you may find it easyer here's an example: 

    //Business Logic example:
    public partial class NorthwindDataContext : System.Data.Linq.DataContext
    {
        partial void InsertCustomer(Customer instance)
        {
            var NWDC = new NorthwindDataContext();
            if (NWDC.Customers.SingleOrDefault(c => c.CustomerID == instance.CustomerID) != null)
            {
                throw new ValidationException("Duplicate CustoemrID is not allowed");
            }
            else
            {
                // finally send this to the DB
                this.ExecuteDynamicInsert(instance);
            }
        }
    }

    Obviously you could also check you other conditions in the if statment and the beauty is that any error will be shown on the page in the validation. [:D]

    Hope this helps [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 27, 2008 4:13 PM
  • User-1005219520 posted

    In addition to Steve's excellent example,  How to: Customize Data Field Validation in the Data Model  explains how to do programmatic validation.

    Monday, October 27, 2008 8:07 PM
  • User-853298616 posted

     The proposed solutions don't quite seem to address what I need. I'll explain.

     In my application, I can create several "Process" entities of different types. Each of those has a related "Requester". What I do when I need to create a Process is to first create a Requester and then redirecting to the Process insert page.

    The thing is, for a specific type of "Process" I need to make sure that almost all of the Requester fields are filled in (in the other Processes only a few are needed).

    I was thinking that the solution would be something like "injecting" RequiredFieldValidator instances in the Requester insert page fields, if the Process is of that kind. How can I do this?

    Or how can I adapt the proposed solutions to achieve this?

     Thanks.

    Wednesday, October 29, 2008 4:20 PM
  • User-330204900 posted

    Hi Asrfarinha, assume that the process will have the id of the Requester and if it does then you can test each field to see if it is requeired but use a new Attribute. Let me see if I show simple example:

    Back in a bit [:D]

    Wednesday, October 29, 2008 7:43 PM
  • User-330204900 posted

    I was thinking that the solution would be something like "injecting" RequiredFieldValidator instances in the Requester insert page fields, if the Process is of that kind. How can I do this?

    Or how can I adapt the proposed solutions to achieve this?

    Hi, Asrfarinha, had a look at creating something generic that would do what you want and decided I'd leave that till another day as there was a simple solution similar to the one above:

    public partial class NWDataContext
    {
    	partial void InsertOrder(Order instance)
    	{
    		var values = new String[] {"Speedy Express", "Federal Shipping"};
    		if (values.Contains(instance.Shipper.CompanyName))
    		{
    			if (String.IsNullOrEmpty(instance.ShipCountry))
    				throw new ValidationException("Ship Country is a required field");
    		}
    	}
    }

    Here values contains a list (two of the Shippers) and then the relavent field is check to see if it matches, if it does then further check are made (one in my case) to see if the required fields have a value in them.

    There also may be a way of reaching into the FieldTemnplates and turning on the RequiredFieldValidators that are already there, but it's too late tonight to look into it any futher as this requires:

    1. Either fiddling with each fieldTemplate
    2. Or rewriting the Insert/Edit page templates.

    hope this helsp [:D]

    Wednesday, October 29, 2008 8:16 PM