porosman
porosman

Reputation: 139

Zend form validation - NotEmpty + StringLength

In form element, I have edited error messages:

$this->addElement('text', 'type', array(
    'label' => 'Type: ',
    'class' => 'form-control',
    'placeholder' => 'type',
    'required' => true,
    'validators' => array(
        array('validator' => 'StringLength', 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.')))
        ,array('validator' => 'NotEmpty', 'options' => array('messages' => array('isEmpty' => 'Type is required.')))
    ),
));

Problem is, if I send empty form, both messages will show up (The type is too short + Type is required.). But in this case, I need to show up only NotEmpty error message and not Stringlength (min length 3) error message. So I need to validate minlength after NotEmpty validation. Thanks

EDIT: i have tried changing diferent positions of calling required and validators and diferent settings of their chains true/false (next code), but it still shows me both error messages if element is empty

$this->addElement('text', 'type', array(
    'label' => 'Type: ',
    'class' => 'form-control',
    'placeholder' => 'type',

    'validators' => array(
        array('validator' => 'NotEmpty',true, 'options' => array('messages' => array('isEmpty' => 'Type is requiredDDDD.'))),
        array('validator' => 'StringLength',false, 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.')))

    ),
    'required' => true,

));

Upvotes: 1

Views: 3163

Answers (2)

porosman
porosman

Reputation: 139

Solution for my last comment, how to add NotEmpty + diferent StringLength validators for more elements in same form:

public function isValid($data) {
    $etat_valid = parent::isValid($data);
    $elements = Array(Array('name', 4, 9), Array('type', 3, 10), Array('type2', 2, 12));

    for ($index = 0; $index < count($elements); $index++) {
        $error = $this->getElement($elements[$index][0])->getMessages();
        if (empty($error)) {
            $validator = new Zend_Validate_StringLength(array(
                'min' => $elements[$index][1],
                'max' => $elements[$index][2],
                    )
            );
            $validator->setMessages(array(
                Zend_Validate_StringLength::TOO_SHORT =>
                'Too short: min '.$elements[$index][1].' chars.',
                Zend_Validate_StringLength::TOO_LONG =>
                'Too long: max '.$elements[$index][2].' chars.'
            ));
            $etat_valid_type = $validator->isValid($data[$elements[$index][0]]);
            if (!$etat_valid_type) {
                $this->getElement($elements[$index][0])->addErrors($validator->getMessages());
            }
        }
    }
    return $etat_valid && $etat_valid_type;
}

So thx for your answers Doydoy

Upvotes: 1

doydoy44
doydoy44

Reputation: 5772

You can try something like this:

  1. Comment the second validator

    $this->addElement('text', 'type', array(
        'label' => 'Type: ',
        'class' => 'form-control',
        'placeholder' => 'type',
        'required' => true,
        'validators' => array(
            //array('validator' => 'StringLength', 'options' => array('min' => 3, 'max' => 10, 'messages' => array('stringLengthTooShort' => 'The type is too short.', 'stringLengthTooLong' => 'The type is too long.'))),
            array('validator' => 'NotEmpty', 'options' => array('messages' => array('isEmpty' => 'Type is required.')))
        ),
    ));
    
  2. and add the validator in the isValid() function:

    public function isValid($data) {
    
        $etat_valid = parent::isValid($data);
    
        $error = $this->getElement('type')->getMessages();
        if (empty($error)){
            $validator = new Zend_Validate_StringLength(array( 
                 'min' => 3, 
                 'max' => 10, 
                 'messages' => array('stringLengthTooShort' => 'The type is too short.', 
                                     'stringLengthTooLong' => 'The type is too long.')
                                                      )
                                               );
    
            $etat_valid_type = $validator->isValid($data['type']);
            if (!$etat_valid_type){
                   $this->getElement('type')->addErrors($validator->getMessages());
            }
        }  
    
        return $etat_valid && $etat_valid_type;
     }
    

Upvotes: 2

Related Questions