garbagedigger Posted March 20, 2013 Share Posted March 20, 2013 So I am working on an MVC application where the controller methods instantiate a factory that is suppose to return a model. In this case, the factory methods do some kind of work with a database or API to save/get/ data which then is placed inside a model and returned by the method. So lets say I have a factory method called createUser(email, password); Now I am going to call this method in the controller. I want my controller lean and mean and not fat! The model can be fat but NOT the controller and so my methods for validation at the moment are inside the model. Where do I call this validation? Inside the controller before the factory? Inside the createUser() method in the factory before it does it's database and api work to return a model??? Where?!? Here are two scenarios that come to mind: -Instantiate the User model inside the controller method before the factory and validate the input data. If it returns without any problems, send it to the factory to return a model of the User with the newly created data. -Instantiate the factory, call createUser(input data) and inside the create user method, do the model validation first before getting/saving the data to an API or database? If the validation fails in the createUser() method, return a model with error messages and any of the bad data. If their are no validation problems, return a model. Quote Link to comment https://forums.phpfreaks.com/topic/275891-mvc-using-factory-to-return-models-where-to-call-the-model-validation/ Share on other sites More sharing options...
ignace Posted March 20, 2013 Share Posted March 20, 2013 Validation is context sensitive. Simply having an isValid method does not tell what it is valid for. Read http://martinfowler.com/bliki/ContextualValidation.html You can opt to name your validation methods: validateForPersist instead of isValidForPersist in order to return an array which would be awkward in the latter. Quote Link to comment https://forums.phpfreaks.com/topic/275891-mvc-using-factory-to-return-models-where-to-call-the-model-validation/#findComment-1419893 Share on other sites More sharing options...
Solution garbagedigger Posted March 21, 2013 Author Solution Share Posted March 21, 2013 (edited) Interesting. So from what I gather, it's better to build validation for solving specific situations right? If that is the case, is it almost better to build separate validator classes which represent specific situations and decouple the validation methods from the model? I still don't understand if it's bad practice to do the validation inside the factory methods like factory->getUsers(input data) or factory->updateUsers(input data) or where in the process the validation should really start. I was jut reading one of Martin's articles on mappers. http://martinfowler.com/eaaCatalog/dataMapper.html. He definitely has some good things to say about application architecture. It's a great article for describing ways of decoupling the database from the model. Edited March 21, 2013 by garbagedigger Quote Link to comment https://forums.phpfreaks.com/topic/275891-mvc-using-factory-to-return-models-where-to-call-the-model-validation/#findComment-1420008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.