[英]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([ ....])
只有第一个验证器在
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);
..........
Phalcon version 2.0.4 on windows. Windows上的Phalcon版本2.0.4。
Any proposition, or suggestions ? 有什么建议或建议吗?
Thanks a lot. 非常感谢。
The best way to get around this problem is to use flash messages, with the class Phalcon\\Flash\\Direct
. 解决此问题的最佳方法是使用Flash消息(类为
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. 我建议您查看phalcon vokuro项目使用此策略。
The key of this solution is the message()
function inside your form class. 该解决方案的关键是表单类内部的
message()
函数。
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
查看如果您遵循建议的架构,则应位于
/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
. 如果您使用多种形式,则向
DI
注册Flash服务非常有用。 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: 定义服务时(可以在根文件夹中的
index.php
或/app/config/
文件夹中的services.php
中),您可以定义Flash服务:
<?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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.