Welcome Guest, Not a member yet? Register   Sign In
Data Validation in Model
#1

[eluser]Teks[/eluser]
Apologies in advance for my newbie question - totally new to CodeIgniter.

In most Object-Oriented frameworks that use the MVC pattern, data validation is handled by the MODEL classes. That seems to be the logical division, as the model should be aware of what is, and isn't, permissible in its variables. A view, or a controller, would only be able to do very basic validation - ie., "is this an email address", "is this a number", etc. - but by the very definition on OO, it will be the model class that will be responsible for doing the ultimate check on the data, and making sure that it can be stored in the database. Even if we consider database operations alone - ie., referential integrity checks, etc. - there is a lot of validation that can only (and should only) be performed by the model. If the model *will* have to perform *some* data validation, then it does not seem to make much sense to put any data validation in a control class, and even less in a view class, as these then would have to be aware of the internal functioning and requirements of the model - and that breaks encapsulation, and makes it meaningless to have an OO framework in the first place.

Indeed, so far, every OO framework I've used instructs programmers to place data validation functions within the model classes.

It seems to me, however, from the information in the User Guide, and from several postings in this forum, that CodeIgniter assumes that data validation for my models should be performed by my CONTROLLER classes - using the Form Validation class. Am I wrong?

Is there an easy way to implement the data validation functions - already available in the Form Validation class - in my models?
#2

[eluser]dmyers[/eluser]
[quote author="Teks" date="1227591911"]Apologies in advance for my newbie question - totally new to CodeIgniter.

In most Object-Oriented frameworks that use the MVC pattern, data validation is handled by the MODEL classes. That seems to be the logical division, as the model should be aware of what is, and isn't, permissible in its variables. A view, or a controller, would only be able to do very basic validation - ie., "is this an email address", "is this a number", etc. - but by the very definition on OO, it will be the model class that will be responsible for doing the ultimate check on the data, and making sure that it can be stored in the database. Even if we consider database operations alone - ie., referential integrity checks, etc. - there is a lot of validation that can only (and should only) be performed by the model. If the model *will* have to perform *some* data validation, then it does not seem to make much sense to put any data validation in a control class, and even less in a view class, as these then would have to be aware of the internal functioning and requirements of the model - and that breaks encapsulation, and makes it meaningless to have an OO framework in the first place.

Indeed, so far, every OO framework I've used instructs programmers to place data validation functions within the model classes.

It seems to me, however, from the information in the User Guide, and from several postings in this forum, that CodeIgniter assumes that data validation for my models should be performed by my CONTROLLER classes - using the Form Validation class. Am I wrong?

Is there an easy way to implement the data validation functions - already available in the Form Validation class - in my models?[/quote]

Take a look at CodeIgniter's DataMapper at CI DataMapper
#3

[eluser]m4rw3r[/eluser]
IgnitedRecord has also integrated validation (and form generation) in the form of a behaviour.
I haven't written any manual yet, but check out the screencast in my signature (it works almost like CI's).




Theme © iAndrew 2016 - Forum software by © MyBB