Re: Proof of concept: Custom validation using beforeValidate

2006-06-13 Thread gwoo

you can do most of this without any other helpers or changes to the  
core code.

function beforeValidate() {

 $this-invalidate('username_taken');
 return false ;
}

the in your view
$html-tagErrorMsg('User/username_taken', 'This username is already  
taken. Please choose another');

Then you can use validates and save as normal.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



RE: Proof of concept: Custom validation using beforeValidate

2006-06-13 Thread Ryan Ginstrom

 [mailto:[EMAIL PROTECTED] On Behalf Of RosSoft
 invalidate() sets a row in an array with a value of 1. Change
 invalidate() to set the string as value (then the errors will be
 available in your controller and helpers)

Nice. So I could override invalidate in app_model:

/*  +++  */
function invalidate( $field, $errMsg = null ) 
{
   if (!is_array($this-validationErrors)) 
   {
   $this-validationErrors = array();
   }
   if ( $errMsg == null )
   {
  $bits = explode('_', $field) ;
  $nameBits = array() ;
  foreach( $bits as $bit )
  {
 $nameBits[] = ucfirst( $bit ) ;
  }
  $name = join( ' ', $nameBits ) ;
  $msg = Invalid $name value ;
   }
   else
   {
  $msg = $errMsg ;
   }

   $this-validationErrors[$field] = $msg ;
}
/*  +++  */

Then in my beforeValidate():

/*  +++  */
 function beforeValidate()
 {
   $this-invalidate( 'username', 'User name already taken' ) ;
   return false ;
 }
/*  +++  */

Then, using ErrorHelper::tagErrorMsg as a drop-in replacement for
HtmlHelper::tagErrorMsg :

/*  +++  */
class ErrorHelper extends Helper 
{
   var $helpers = array( 'Html' ) ;
   
   function tagErrorMsg( $field ) 
   {
  $this-Html-setFormTag($field) ;
  $msg = $this-Html-tagIsInvalid($this-Html-model,
$this-Html-field) ;
  if ( !$msg )
  {
 return null ;
  }
  return sprintf('div class=error_message%s/div', $msg ) ;
   }
}
/*  +++  */

// 

How does that look?

--
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
-~--~~~~--~~--~--~---



RE: Proof of concept: Custom validation using beforeValidate

2006-06-13 Thread Ryan Ginstrom

 [mailto:[EMAIL PROTECTED] On Behalf Of gwoo
 you can do most of this without any other helpers or changes to the  
 core code.
 
 function beforeValidate() {
 
  $this-invalidate('username_taken');
  return false ;
 }
 
 the in your view
 $html-tagErrorMsg('User/username_taken', 'This username is already  
 taken. Please choose another');

I had thought about this approach, but I saw a couple problems. First, your
views need to know about your business logic so they know the types of errors
that can occur. And every time your business logic changes, you need to go
mucking about with your views to change the possible error messages. Having
this all in the model allows you to encapsulate error handling.

Also, when you localize your site, I think it will be easier to have all your
error-message logic in one place, so you can more easily retrieve and set
localized error messages from the database.

--
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
-~--~~~~--~~--~--~---



Re: Proof of concept: Custom validation using beforeValidate

2006-06-13 Thread gwoo

I prefer to have my messages in the view.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---