I really like the idea of custom validation error messages. For
example, for a password field you could check that the password was
entered the same way twice, contains at least two numbers, no illegal
characters, etc., and give a different error message for each.

I think that validation should live in the model, so error messages
should as well. Thus, I put together a quick proof of concept using
beforeValidate(). Comments/critiques appreciated.

First, in the model, define a beforeValidate function:

function beforeValidate()
{
   $this->valMessages = array() ;
   $this->valMessages['modelValidationErrors'] = array(
      'username' => 'This username is already taken. Please choose
another' ) ;
   return false ;
}

Then, in app_controller, the following function:

function validatingSave( &$model )
{
   if ( $model->save( $this->data ) ) return true ;

   $this->Session->setFlash( '<p class="error">Sorry, but we cannot
process your form. Please make the changes indicated below, and try
again.</p>', '' );
   $this->data += $model->valMessages ;
   return false ;
}

Next, in the controller's edit function, change the save line to this:

if ( $this->validatingSave( $this->Attachment ) )
{
        $this->Session->setFlash( '<p class="success">Your profile has been
updated.</p>', '' );
        $this->redirect( "/users" );
}

Now in your ErrorHelper, you should have access to
$this->data['[modelValidationErrors]']
If it's set and the field is set, you can show the custom error message
for that field, or if not, the fallback validation error message from
HtmlHelper.

Does this sound like a good direction to take my validation? Thanks for
any feedback.

--
Regards,
Ryan Ginstrom


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

Reply via email to