Welcome Guest, Not a member yet? Register   Sign In
Validation again - a big conceptual problem...
#1

[eluser]Multisnet[/eluser]
I think that I read all posts about validation in the forum, however I would like to know more opinions. My doubt is where to validate (model vs controller). I know that the opinions will diverge a lot, some of you prefer to validate the data in the model, others in the controller. Some people create a specif layer between model and controller to deal with validation. So I realize that there are a lot of approaches.

I'm working in some front-end forms, but in 1 month I'll be working in application's backend. If I validate the data in the controllers, I'll need to repeat all the validation in the backend forms. So, I'm thinking if it would be better to validate in the model.

My "conceptual" opinion is that some validation should remain in the business logic layer (e.g. username max. char. 12) and other in the data layer (e.g. in User constructor the username should be a required field, as we can't create a User without a username). My opinion is that some validation is more related with the logic of our application, other with the data itself (the basic validation for data). This approach would be a big nightmare to implement and maintain, it's only a conceptual idea.

I would like to know your opinions.
#2

[eluser]umefarooq[/eluser]
try to validate you data on controller level, when you got the correct data then pass that data to your model. in that way your data layer will clean, no need to re validate the data again.
#3

[eluser]Multisnet[/eluser]
Thank you for your opinion.

But how should I deal with the problem that I explained? FrondEnd <> BackEnd validation?

Simple example:
I implement the validation on the user_register_controller (in FrontEnd) and then I need the same validation in admin_users_controller (in backEnd). The rules will be the same, should I duplicate my code? or in your opinion I should create another controller to deal with validation?
#4

[eluser]xwero[/eluser]
You can only have the same validation if the forms are the same. If that is the case you better create a controller method for both front- and backend form and use url redirecting.

Even if there is a slight difference in fields the form processing should be split, you never know how the forms going to change in the future.




Theme © iAndrew 2016 - Forum software by © MyBB