You have to setup the error messages as an array with all of the possible error values. Using your example:

$validators = array('fname' => array('Alpha',
                   'presence' => 'required',
                   'messages' => array(
                        Zend_Validate_Alpha::NOT_ALPHA => 'Custom not alpha 
error message.',
                        Zend_Validate_Alpha::STRING_EMPTY => 'Custom empty 
string error message.'
));

Hope that helps.

Arthur


Joshua Ross wrote:
I have a question about Zend_Filter_Input. I have read and reread the documentation and I am confused about the what can be done with the messages parameter when defining a validator. The problem is that if I have a value string that is empty it seems I cannot override the error message that is returned. Maybe I am just not understanding the documentation fully? Here is the code(generecized):

$validators = array('fname' => array('Alpha',
                                  'presence' => 'required',
                                  'messages' => 'Invalid First Name'));
$filters = array();
$options = array('missingMessage' => 'test message 2',
                          'notEmptyMessage' => 'test message 3');
$input = Zend_Filter_Input($filters, $validators, $this->getRequest()->getPost(), $options);

if ($input->hasMissing() || $input->hasInvalid()) {
    $msg ='';
    foreach($input->getMessages() as $error) {
        $msg .= implode("\n", $error);
    }
    throw new My_Exception($msg);
}

I expect to receive one of my three test messages for any error from this validator and I have tried both $input->getMessages() and $input->getErrors()... however when the string is empty I always receive the following message.

'' is an empty string


When I dump the $input after it processes I see:
...
...
...
  ["_validatorRules:protected"] => array(8) {
    ["fname"] => array(9) {
      [0] => string(5) "Alpha"
      ["presence"] => string(8) "required"
      ["messages"] => array(1) {
        [0] => string(19) "Invalid First Name."
      }
      ["rule"] => string(5) "fname"
      ["fields"] => string(5) "fname"
      ["breakChainOnFailure"] => bool(false)
      ["allowEmpty"] => bool(false)
      ["validatorChain"] => object(Zend_Validate)#187 (3) {
        ["_validators:protected"] => array(1) {
          [0] => array(2) {
            ["instance"] => object(Zend_Validate_Alpha)#188 (6) {
              ["allowWhiteSpace"] => bool(false)
              ["_messageTemplates:protected"] => array(2) {
                ["notAlpha"] => string(19) "Invalid First Name."
                ["stringEmpty"] => string(28) "'%value%' is an empty string"
              }
              ["_value:protected"] => string(0) ""
              ["_messageVariables:protected"] => array(0) {
              }
              ["_messages:protected"] => array(1) {
                [0] => string(21) "'' is an empty string"
              }
              ["_errors:protected"] => array(1) {
                [0] => string(11) "stringEmpty"
              }
            }
            ["breakChainOnFailure"] => bool(false)
          }
        }
        ["_messages:protected"] => array(1) {
          [0] => string(21) "'' is an empty string"
        }
        ["_errors:protected"] => array(1) {
          [0] => string(11) "stringEmpty"
        }
      }
      ["validatorChainCount"] => int(1)
    }
...
...
...
 ["_invalidMessages:protected"] => array(1) {
    ["fname"] => array(1) {
      [0] => string(21) "'' is an empty string"
    }
  }
  ["_invalidErrors:protected"] => array(1) {
    ["fname"] => array(1) {
      [0] => string(11) "stringEmpty"
    }
  }


Any help here understanding my mistake or what I'm doing wrong would be appreciated. Thanks Josh

Reply via email to