views:

53

answers:

4

A name field needs to be verified as starting with a capital letter and not being allowed to take a number. I'll do this with regular expressions. Implementing the MVC pattern.

Should I have the controller send the value that was input to a checking class and send corresponding error msg's back to the UI and after checking then call the class that writes it to the DB
OR
should I have the controller send the 'value input' to the class that writes it to the DB and this method then calls the validation method?

A: 

The first approach seems more correct because the db access logic shouldn't be mixed with validation.

Darin Dimitrov
@Darin Dimitrov Whats the "standard" way to do it? I mean under best practices and all?
hi tech credo
The controller calls the validation logic layer before calling the data access layer.
Darin Dimitrov
@Darin Dimitrov Should the controller itself call it or should there be a trigger in the UI? (sorry I'm a bit of an amateur.)
hi tech credo
+1  A: 

you can use something like this (i've used for email validation)

    [Required(ErrorMessageResourceType = typeof(CCSModelResources), ErrorMessageResourceName = "ANTCommonTextRequiredMessage")]
    [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$",
        ErrorMessageResourceType = typeof(CCSModelResources), ErrorMessageResourceName = "ANTCommonTextRegularExpressionMessage")]
    public new string EmailAddress 
    {
        get { return base.EmailAddress; }
        set { base.EmailAddress = value; } 
    }

and you controller code like

    [Authorize]        
    [HttpPost]
    public ActionResult UpdatePersonalDetails(FormCollection form)
    {
        regUserWizard.PersonalDetails = new MVCPersonalDetails();

        if (!TryUpdateModel<MVCPersonalDetails>(regUserWizard.PersonalDetails, form.ToValueProvider()))
        {
            return View("UpdateUser", regUserWizard);
        }
else
        {
            //you code
        }

        return RedirectToAction("Index", "Home");
    }

you view code like

< %= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %> <% Html.EnableClientValidation(); using (Html.BeginForm()) { %> //you code

Hasu
The code isn't the problem. I'm wondering at what level of the MVC it should be implemented at. A trigger in the UI code, the controller calls it or the DB writing class calls it. How is validation error supposed to be implemented under "best practices"
hi tech credo
RIght so the controller does it. Gotcha.
hi tech credo
if you add these lines in your view then it will start validation on UI aswell using JS. <%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %> <% Html.EnableClientValidation(); using (Html.BeginForm()) { %>
Hasu
A: 

THe purpose of the controller is to validate input and provide valid input to models. Your model should not be concerned with what inputs is provided by the views. It should concentrate on Business logic only. You can add some validation code on the client side for ease-of-use as well but it needs to be present on server side too for security purpose.

Your 1st approach is correct...go ahead with it...

Mulki
@Mulki thankyou
hi tech credo
I thought validation was put on the model, as a meta class. so the model will validate itself to be used by the business logic?
jimplode
Depends on what kind of validation you are talking about...I am talking about user input validation.eg:checking if the user has entered a Email Id...is the input a number etc.
Mulki
A: 

In my opinion, it all depends on what kind of validation you want to perform:
1. If you don't want a field to be empty, I will do that check on the view layer. This is where most regex could be applied.
2. If I want to ensure that a user input(, say a username) is unique or not , I will do that validation on the controller side and pass any feeback back to the view. In the latter, the controller might have a dependency on an abstraction of a data access layer or service layer to do the actual checking.

walters