model validation seems to have a serious logic flaw

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

model validation seems to have a serious logic flaw

ianmac-2

It seems to me that the model validation is somewhat flawed - let me
explain:

if I have a model with say three fields and I set up
var $validate = array (
                              'field1' => VAILID_NOT_EMPTY,
                              'field2' => VAILID_NOT_EMPTY,
                              'field3' => VAILID_NOT_EMPTY);

Validation works as I would expect ONLY if the data I am trying to save
has all three fields. i.e. if the data I am saving looks like :
$data = array
('field1'=>'somedata','field2'=>'somedata','field3'=>'somedata');

However if I try saving to the model with the data looking like say:
$data = array('field1'=>'somedata');
IT WORKS JUST FINE - EVEN THOUGH THERE IS NO DATA FOR field2 and field3
which is clearly not the intention of the business logic encapsulated
in $validate.

I checked through the code in model_php5.php the function invalidFields
the code that checks the fields is:

if (isset($data[$field_name]) && !preg_match($validator,
$data[$field_name])) {
                                $this->invalidate($field_name);
                        }

I.E. It only checks if data is valid IF the data field was submitted.
Is this the intention? If it is then the validation rules cannot be
considered business logic rules for the MODEL as they are easily
violated by simply submitting data with only some of the data fields
present.

Maybe something more like:
if (!isset($data[$field_name]) || (isset($data[$field_name]) &&
!preg_match($validator, $data[$field_name]))) {

                                $this->invalidate($field_name);
                        }

This would invalidate a field if there was a valiadition rule for the
field_name even if $data[$field_name] did not exists - this would seem
more like the intended behaviour.

I am reasonably new to cake - about 6 weeks and  I think it is a great
framework - this is my first post to this group so hope it makes sense.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: model validation seems to have a serious logic flaw

marc.konings@gmail.com

Although I completely agree with you and I have wondered about the
design logic behind this myself, I think changing this behavior will
have a big impact on existing applications.

Having said that, I wouldn't mind refactoring my code to support proper
Model validation.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: model validation seems to have a serious logic flaw

Nate Abele

It is written that way to allow saveField and other features to work
properly.  Cake 1.2 will feature functionality that allows you to check
if a field is set, but in the meantime you are more than welcome to use
a callback like beforeValidate, to make sure all fields are set.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---