1
votes

Form code :

  $this->add(array(
     'name' => 'username',
     'type' => 'Text',
     'options' => array(
         'label' => 'Username',
     ),
 ));

model :

    $inputFilter->add(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 => 'Username required',
        ),
    ),
            ),                    
            array(
                'name' => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                   'min' => 5,
                    'max' => 69,
                ),
            ),             
            ),                
    ));

View :

$form->setAttribute('action', $this->url('signup', array('action' => 'signup')));
 $form->prepare();

 echo $this->form()->openTag($form);
 echo $this->formRow($form->get('username'));

then I get output like following :

enter image description here

It seems every individual part of ‘Zend\Validator’ works alone and throws error message individually. I want to display the required message only if the field is empty, not the stringlength. How can I do that ?

-Thanks.

Update :

I did the following in the 'view' page :

 echo $this->formRow($form->get('username'));
 foreach($form->getMessages() as $key=>$value){
     if($key=="username"){
             if(isset($value['isEmpty'])){
                 echo $value['isEmpty'];
             }else if(isset($value['stringLengthTooShort'])){
                echo $value['stringLengthTooShort'];
             }else if(isset($value['stringLengthTooLong'])){
                echo $value['stringLengthTooLong'];
             }
     }
 }

then got the output :

enter image description here

now there are two error messages in unordered list still being there. how can I remove those messages and keep my formatted message only ?

Solved :

I had to do the following :

View :

$errmsg = $form->getMessages();
echo $this->formLabel($form->get('username'));
echo $this->formInput($form->get('username'));
if ($errmsg) {
    if (isset($errmsg['username'])) {
        foreach ($errmsg['username'] as $key => $value) {
            ?>
            <span class="formerror">
                <?php
                if ($key == "isEmpty") {
                    echo $value;
                    break;
                } else if ($key == "stringLengthTooShort") {
                    echo $value;
                    break;
                } else if ($key == "stringLengthTooLong") {
                    echo $value;
                    break;
                }
                ?>
            </span>

To format the error messages I can’t use ‘formelementerrors’ because it returns string (ref: http://framework.zend.com/manual/2.0/en/modules/zend.form.view.helpers.html#formelementerrors), not array. so its easy to identify individual errors by keys if I use ‘getMessages()’.

1

1 Answers

1
votes

In your controller you can use this method for retrieve the errors messages :

$form->getMessages();

Is an array and you can use a foreach to retrieve the key 'isEmpty' and display this.