2012-10-04 14 views
10

Sto cercando di personalizzare il messaggio di errore predefinito "Value is required and can't be empty" in ZF2ZendFramework 2 inputfilter messaggio di errore Customize Default

Sto usando seguente codice per aggiungere messaggio di errore predefinito Personalizza nella validatori di inputfilter

$inputFilter->add($factory->createInput(array(
       'name' => 'username', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min' => 4, 
          'max' => 20, 
          'messages' => array(
           'isEmpty' => 'Please enter User Name between 4 to 20 character!' 
          ),        
         ), 
        ), 
       ), 
      ))); 

Ma sto ottenendo l'errore seguente.

Zend\Validator\Exception\InvalidArgumentException 

File: 
    /home/website/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php:220 

Message: 
    No message template exists for key 'isEmpty' 

Cosa sto facendo male?

reference

risposta

21

provare questo

$inputFilter->add($factory->createInput(array(
       'name' => 'username', 
       'required' => true, 
       'filters' => array(
        array('name' => 'StripTags'), 
        array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' =>'NotEmpty', 
         'options' => array(
          'messages' => array(
           \Zend\Validator\NotEmpty::IS_EMPTY => 'Please enter User Name!' 
          ), 
         ), 
        ), 
        array(
         'name' => 'StringLength', 
         'options' => array(
          'encoding' => 'UTF-8', 
          'min' => 4, 
          'max' => 20, 
          'messages' => array(
           'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!', 
           'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!' 
          ), 
         ), 
        ), 
       ), 
      ))); 

reference

Other validator set

+0

Ehi, dove sono tje possibili chiavi ("stringLengthTooShort", \ Zend \ Validator \ notempty :: IS_EMPTY, " stringLengthTooLong ") documentato? –

1

Lo StringLength validatore non verifica per l'ingresso sia vuoto o meno. Controlla contro lunghezze. Esistono i seguenti modelli di messaggio per il validatore StringLength:

const INVALID = 'stringLengthInvalid'; 
const TOO_SHORT = 'stringLengthTooShort'; 
const TOO_LONG = 'stringLengthTooLong'; 

/** 
* @var array 
*/ 
protected $messageTemplates = array(
    self::INVALID => "Invalid type given. String expected", 
    self::TOO_SHORT => "The input is less than %min% characters long", 
    self::TOO_LONG => "The input is more than %max% characters long", 
); 

Vedere l'esempio di @Developer per un approccio diretto. Anche se suggerisco di andare con la denominazione CamelCased dei Validator, quindi 'name' => 'NotEmpty' anziché 'name' => 'not_empty'

È possibile verificare quali modelli di messaggio esistono se viene visualizzato il codice per ciascuna classe di convalida. Li troverete sotto ./vendor/zendframework/zendframework/library/Zend/Validator/*

3

È anche possibile impostare il messaggio di errore di inputFilter così:

$form = new ParticipantForm(); 
    $mailInput = new Input('mail'); 
    $mailInput->setRequired(true); 
    $mailInput->setErrorMessage("Empty input"); 
Problemi correlati