Validation scenario

Last post 12-16-2007 2:57 AM by abombss. 5 replies.

Sort Posts:

  • Validation scenario

    12-10-2007, 6:23 PM
    • Loading...
    • CVertex
    • Joined on 09-08-2006, 10:53 PM
    • Posts 88

    How will validation be implemented in the future?

    What happens with the Router arguments fail validation? -- doesn't seem to do anything at the moment

  • Re: Validation scenario

    12-11-2007, 4:03 AM
    • Loading...
    • Haacked
    • Joined on 09-17-2003, 2:43 PM
    • Posts 311
    • AspNetTeam

    What sort of validation are you talking about? Got an example?

    Phil Haack (http://haacked.com/)
    Senior Program Manager, Microsoft

    What wouldn’t you do for a Klondike bar?
  • Re: Validation scenario

    12-15-2007, 11:51 AM
    • Loading...
    • CVertex
    • Joined on 09-08-2006, 10:53 PM
    • Posts 88

     I'm trying to validate inputs into the Controller on form variables with the routing rule:

                RouteTable.Routes.Add(new Route
                {
                    Url = "Clients/Search/[query]",
                    Defaults = new { controller = "Clients", action = "Search" },
                    Validation = new {query=@"\d{2}"},
                    RouteHandler = typeof(MvcRouteHandler)
                });

     So the query param can only be a 2 digit number.

     

    When I visit localhost:8080/Clients/Search/QUERY where QUERY is not a 2 digit number, the query parameter is nullified and passed into the controller. If QUERY is a 2 digit number, it's passed into the controller as is.

    Seems like this validation should be doing more than just nullifying the input.


    I'd prefer it if it actually did something, like pass a validation error into the controllerContext for the action to use. Controller level validation would be good first defense.

    I find Regex a really good way of validating url parameters and i really like the fact MVC will  validate form variables too. It'd be cool if there was a virtual controller method for receiving validation problems. I can imagine creating Action attributes for specifying general bad input behavior.

    Others have implemented validation in model objects ( http://www.dotnetkicks.com/aspnet/How_To_Validation_Using_ASP_NET_MVC) and let the action decide what to do, but I think the Regex's are there for a good reason, and allow for declarable error behavior.


  • Re: Validation scenario

    12-15-2007, 3:34 PM
    Answer
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

    You probably have a rule that is catching the route somewhere else.  The following test shows that no RouteData is found given your scenario.

     

    [Test]
    public virtual void GetRouteData_WithStringForQuery_ReturnsNullRouteData()
    {
    	MockRepository mocks = new MockRepository();
    	RouteCollection routes = new RouteCollection();
    	IHttpContext httpContext = mocks.DynamicMock();
    	IHttpRequest httpRequest = mocks.DynamicMock();
    	routes.Add(new Route
    		{
    			Url = "Clients/Search/[query]",
    			Defaults = new { controller = "Clients", action = "Search" },
    			Validation = new { query = @"\d{2}" } ,
    			RouteHandler = typeof(MvcRouteHandler)
    		});
    	using(mocks.Record())
    	{
    		SetupResult.For(httpContext.Request).Return(httpRequest);
    		SetupResult.For(httpRequest.AppRelativeCurrentExecutionFilePath).Return("~/Clients/Search/Goose");
    	}
    	using(mocks.Record())
    	{
    		Assert.That(routes.GetRouteData(httpContext), Is.Null);
    	}
    }
    
      
    Adam Tybor -- abombss.com
    Filed under: , ,
  • Re: Validation scenario

    12-16-2007, 12:58 AM
    • Loading...
    • CVertex
    • Joined on 09-08-2006, 10:53 PM
    • Posts 88

    Oh, I see. because it fails the validation rules it gets passed onto the next route rule.

    It will finally land on the default /[controller]/[action]/[id] rule which will pass query as null (cos it's not in query string).

    That makes sense.

    I misinterpreted what the validation rules were for.
     


     

     

     

  • Re: Validation scenario

    12-16-2007, 2:57 AM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     Absolutely.   You need to be careful with using rules without validation because more generic rules can unintentionally capture a route that you do not intend to.  Anytime you have a [parameter] without validation the RouteCollection will match anything.  Its almost like a ".*" regex.  Order that rules are added is important.  The first one in is the highest priority.  As I have been playing I try and be as explicit as possible so I do not have overlapping rules and my last rule is usually some type of catch all that I can use to send intelligent 404's and possible 302's.

    Adam Tybor -- abombss.com
    Filed under: , ,
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter