Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-11-2008, 02:23 PM
greboguru's Avatar
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 16
Question Zend Form - Custom Errors ... how?

Hi,

I'm trying to add a custom error message to my form defintion, but all I'm getting are the default messages.

My definition ..

Code:
 
   var $form = array(
       'action'   => '/myswr/site/admin/login',
       'method'   => 'post',
       'elements' => array(
           'username' => array('text', array(
               'validators' => array(
                   array('alnum', array('message' => '%s must be alphanumeric!')),
                   array('regex', false, array('/^[a-z]/i')),
                   array('stringLength', false, array(3, 20,
                                          'message' => array(
                                          	Zend_Validate_StringLength::TOO_SHORT => '%s must be at least %min% characters.',
                                          	Zend_Validate_StringLength::TOO_LONG => '%s must be shorter than %max% characters.'))
                   )
               ),
               'required' => true,
               'filters'  => array('StringToLower')
           )),
           'password' => array('password', array(
               'validators' => array(
                   array('stringLength', false, array(3))
               ),
               'required' => true,
           )),
           'submit' => 'submit',
       ),
   );
Where am I going wrong? I've seen examples on how do this 'long hand' but I can't seem to get the above to work ?

Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-12-2008, 10:12 AM
greboguru's Avatar
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 16
Post

[Yet again I seem to be responding too my own posts .. is there anybody out there?]

One solution to the above is ...

Code:
 
class LoginForm extends Zend_Form
{
   var $form = array(
       'action'   => 'login',
       'method'   => 'post',
       'elements' => array(
           'username' => array('text', array(
               'validators' => array(
                   array('alnum', false),
                   array('regex', false, array('/^[a-z]/i')),
                   array('stringLength', false, array(6, 20))
               ),
               'required' => true,
               'filters'  => array('StringToLower')
        ...
      );

  function __construct() 
  {
      parent::__construct($this->form);
      
      $el = $this->getElement('username');
      
      $val = $el->getValidator('alnum');
      $val->setMessage("custom message for alnum", Zend_Validate_Alnum::NOT_ALNUM);
      
      $val = $el->getValidator('stringLength');
      $val->setMessage("The user name is too short", Zend_Validate_StringLength::TOO_SHORT);
      $val->setMessage("The user name is too long", Zend_Validate_StringLength::TOO_LONG);
      ...
  } 
}

Is there a better way?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-12-2008, 10:19 AM
Cristian's Avatar
Administrator
 
Join Date: Feb 2007
Location: Sibiu, Romania
Posts: 99
Default

Actually i am using combined config files and translation files.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-18-2008, 07:20 AM
greboguru's Avatar
Junior Member
 
Join Date: Feb 2008
Location: UK
Posts: 16
Default

Quote:
Originally Posted by Cristian View Post
Actually i am using combined config files and translation files.
Do you have any examples you could post?

Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 11:43 AM
Junior Member
 
Join Date: Apr 2008
Posts: 1
Default

Quote:
Originally Posted by greboguru View Post
Code:
 
   var $form = array(
       'action'   => '/myswr/site/admin/login',
       'method'   => 'post',
       'elements' => array(
           'username' => array('text', array(
               'validators' => array(
                   array('alnum', array('message' => '%s must be alphanumeric!')),
                   array('regex', false, array('/^[a-z]/i')),
                   array('stringLength', false, array(3, 20,
                                          'message' => array(
                                          	Zend_Validate_StringLength::TOO_SHORT => '%s must be at least %min% characters.',
                                          	Zend_Validate_StringLength::TOO_LONG => '%s must be shorter than %max% characters.'))
                   )
               ),
               'required' => true,
               'filters'  => array('StringToLower')
           )),
           'password' => array('password', array(
               'validators' => array(
                   array('stringLength', false, array(3))
               ),
               'required' => true,
           )),
           'submit' => 'submit',
       ),
   );
I think the array key for custom validation messages is not 'message' but rather 'messages'
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:14 PM.