Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validator in my form.php

Hi.

i created form with validation rules. All is fine, form is visible and works. Problem is with validators. Only first validator works in addValidators([ ....])

My form class source code:

public function initialize()
{

    $title = new Text('title');
    $title->setLabel('Title of article');
    $title->setFilters([
        'striptags', 'trim'
    ]);

    $title->addValidators([
        new PresenceOf([
            'message' => 'Title can not be empty'
        ]),
        new StringLength([
            'min' => 5,
            'messageMinimum' => 'Title is too short. Should has more than 5 letters'
        ]),
        new MYArticleAddCheckTitleValidator([
            'message' => 'aaaaaaaaaaaaaaa'
        ])
    ]);

    $this->add($title);

    ..........
  • Validator PresenceOf works fine. validation flash message is visible.
  • Validator StringLength does not work. It looks like form doesn't know about it
  • Validator MYArticleAddCheckTitleValidator (my own validator class) - the same as StringLength.

Phalcon version 2.0.4 on windows.

Any proposition, or suggestions ?

Thanks a lot.



4.0k
Accepted
answer

I tested on OSX, with latest Phalcon version and all works fine.

<?php
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Form;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;

class TestForm extends Form
{
    public function initialize()
    {
        // Name
        $name = new Text('name');
        $name->setLabel('Your Full Name');
        $name->setFilters(array(
            'striptags', 'trim'
        ));
        $name->addValidators(array(
            new PresenceOf(array(
                'message' => 'Title can not be empty'
            )),
            new StringLength(array(
                'min'            => 5,
                'messageMinimum' => 'Title is too short. Should has more than 5 letters'
            )),
        ));
        $this->add($name);
    }
}
<?php

class AboutController extends ControllerBase
{
    public function indexAction()
    {

        $form = new TestForm();

        if($this->request->isPost() && !$form->isValid($this->request->getPost())) {
            $this->view->disable();
            /** @var \Phalcon\Validation\Message $message */
            $format = 'Post value: %s<br />Field: %s<br />Type: %s<br />Message: %s<hr/>';
            foreach($form->getMessages() as $message) {
                echo sprintf($format, var_export($this->request->getPost($message->getField()), true), $message->getField(), $message->getType(), $message->getMessage());
            }
        }

        $this->view->setVar('form', $form);
    }
}

Test results:

Post value: ''
Field: name
Type: PresenceOf
Message: Title can not be empty
--------------
Post value: ''
Field: name
Type: TooShort
Message: Title is too short. Should has more than 5 letters
Post value: 'bla'
Field: name
Type: TooShort
Message: Title is too short. Should has more than 5 letters


1.1k

Thanks...

I did't use $form->isValid in my controller. I used $model->getMessages() instead of $form->getMessages()

Beer for you ;)