简体   繁体   中英

Phalcon - validation in form

I created a form with validation rules. Everything 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.

Using Phalcon\\Flash\\Direct

The best way to get around this problem is to use flash messages, with the class Phalcon\\Flash\\Direct . You can find the documentation here .

This strategy is used by the phalcon vokuro project which I suggest you to look at.

The key of this solution is the message() function inside your form class.

Form class

<?php
namespace Your\App\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Message;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;
use Your\App\Validation\MYArticleAddCheckTitleValidator;

class YourForm extends Form {

    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);
    }

    /**
     * Prints messages for a specific element. Call it in the view
     */
    public function messages($name)
    {
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }
}

Controller

<?php
namespace Your\App;

use Your\App\YourForm;

class YourController extends ControllerBase
{

    public function indexAction()
    {
       $form = new YourForm();
       $this->view->form = $form;

        if($this->request->hasQuery('title')){
            if ($form->isValid($this->request->getQuery()) != false) {
                // Code when form is valid
            }
        }
    }
}

View If you follow the suggested schema should be located in /app/views/your/index.html

<form method="GET" action="">
    <?= $form->label('title') ?>
    <?= $form->render('title')?>
    <?= $form->messages('title') //show messages here ?>
</form>

If you have more than one form, it is useful to register the flash service with the DI . When you define your services (could be in the index.php in the root folder or services.php in the /app/config/ folder) you define your flash service:

<?php 
use Phalcon\DI\FactoryDefault;

$di = new FactoryDefault();

// Register the flash service with custom CSS classes
$di->set('flash', function () {
    $flash = new Phalcon\Flash\Direct(
        array(
            'error'   => 'your-error-class',
            'success' => 'your-success-class',
            'notice'  => 'your-notice-class',
            'warning' => 'your-warning-class'
        )
    );

    return $flash;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM