Forms and Validation

Last post 01-11-2008 5:12 AM by andyfreestyle. 6 replies.

Sort Posts:

  • Forms and Validation

    01-10-2008, 8:20 AM
    • Member
      98 point Member
    • random0xff
    • Member since 02-11-2007, 2:34 PM
    • Posts 208
    1. Are you going to reuse the existing client side validation scripts (output from the regular validators), or are you going to write new ones?
    2. Will it be possible to define validation (or rules) once (preferably in the controller that handles the form data).

    Just curious, because I saw a blog post from Phil Haack that used TempData to send error messages and the original field values to the view if there was an error, but I think that's too much work (plumbing) . Perhaps a controller action can have attributes on it to define the rules for its parameters. The helper method that creates the form in the view could maybe inspect those rules and generate an onsubmit handler and some script for the responding fields in the form.

    Just some ideas, thanks for listening! 

  • Re: Forms and Validation

    01-10-2008, 8:46 AM

    Seems to me that the Dynamic Data aspects are taking a lead on how validation attributes could be added to data classes (as opposed to the Controller as you mention), with some of this work finding it's way into the MVCToolkit. Finger's crossed the next wave of previews will clarify the direction on this, so that people could begin writing their own Html Helpers to generate javascript in their favourite javascript library (mine being ExtJS).

  • Re: Forms and Validation

    01-10-2008, 5:38 PM
    • Member
      98 point Member
    • random0xff
    • Member since 02-11-2007, 2:34 PM
    • Posts 208

    Yeah on the Model is the best place... if you have a model. What if you have a contact form? Where do you define it, there's no model? That's why I thought the action that the form points to could declare it, thought how the view would get that I have no idea, since its rendering is the result of a another action. 

  • Re: Forms and Validation

    01-10-2008, 6:45 PM
    • Participant
      983 point Participant
    • ChadThiele
    • Member since 04-27-2003, 2:43 AM
    • Japan
    • Posts 274

    random0xff:
    Yeah on the Model is the best place... if you have a model. What if you have a contact form? Where do you define it, there's no model?

    There's still a model. What happens when the user presses the submit button? That should be handled by the model (but "controlled" by the controller Stick out tongue ). You'd run your validation from the models too... What I've done is just write a service class, that I named SecurityGuard, to perform my security and validation calls. I named it SecurityGuard because I like to name my classes based on real world jobs.

    My SecurityGuard class will let my controller know there was a problem, and that the user should be redirected to an error message. That's how it works right now, but I think attribute based validation and security could be very useful. Hopefully we'll have that option in the future.

    Did I answer your question(s)? Phweew...
  • Re: Forms and Validation

    01-11-2008, 5:02 AM
    • Contributor
      4,372 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    Be careful here. You said "a service class", do you mean an actual service like WCF etc? if so I wouldn't remove the validation from the service because you don't want to have to repeat yourself when you create other clients. The validation should stay with the service.

    However, exposing the validation logic (via attributes or whatever) and then improving the UI with client side validation is a win-win. The main problem at the moment is there's no easy way to hook all this up dynamically.

    I've seen the dynamic data controls and i'm very impressed, however, none of that works in MVC.

  • Re: Forms and Validation

    01-11-2008, 5:07 AM
    • Contributor
      4,372 point Contributor
    • tgmdbm
    • Member since 12-17-2007, 2:08 PM
    • Posts 883
    • ASPInsiders
      TrustedFriends-MVPs

    random0xff:
    What if you have a contact form? Where do you define it, there's no model?
     

    Create one!

    Create a ContactForm class, add the validation code to a method called Validate(). Then "adding" a contact form will create a ContactForm object, perform Validate(), and send the email.

  • Re: Forms and Validation

    01-11-2008, 5:12 AM

    tgmdbm:
    I've seen the dynamic data controls and i'm very impressed, however, none of that works in MVC.

    But surely there is the sample project (Blog, plus MVCDynamicDataFiles) in the MVCToolkit that shows using Dynamic Data with MVC. Looks a great start.

Page 1 of 1 (7 items)