gpt4 book ai didi

forms - Phalcon 使用 Phalcon\Tag 或 Phalcon\Forms 创建表单

转载 作者:行者123 更新时间:2023-12-02 00:38:35 28 4
gpt4 key购买 nike

我上下搜索,但找不到 Phalcon\Tag 或 Phalcon\Forms 哪一个更适合创建表单。

这两个类都具有创建表单元素的功能。但我发现 Phalcon\Tag 中有一些方便的标签,例如 Phalcon\Tag::emailField() 或 Phalcon\Tag::dateField(array())

Phalcon 文档说:

"Phalcon\Forms is a component that aid the developer in the creation and maintenance of forms in web applications."

"Phalcon\Tag is designed to simplify building of HTML tags. It provides a set of helpers to generate HTML in a dynamic way."

任何人都可以帮助我了解使用这两种方法的优缺点吗?

谢谢

最佳答案

简单来说,Phalcon\Tag 仅用于设计 html(用户 View )。但为了验证&&向表单添加规则,您需要使用 phalcon\forms 我将在下面向您展示 phalcon\forums 的示例

新表格类:

use Phalcon\Forms\Form,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Email as Emailfield,
Phalcon\Forms\Element\Check,
Phalcon\Forms\Element\Hidden,
Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\Identical,
Phalcon\Validation\Validator\Email;

class LoginForm extends Form
{
public function initialize()
{
$email = new Emailfield('email', array(
'placeholder' => 'Type your Email'
));
$email->setLabel('E-Mail');
$email->setFilters('email');
$email->addValidators(array(
new PresenceOf(array(
'message' => 'E-mail is required'
)),
new Email(array(
'message' => 'E-mail is not valid'
))
));

$this->add($email);

$password = new Password('password', array(
'placeholder' => 'Type your Password'
));
$password->setLabel('Password');
$password->setFilters(array('striptags', 'string'));
$password->addValidators(array(
new PresenceOf(array(
'message' => 'Password required'
))
));
$this->add($password);

//Remember
$long_login = new Check('long_login', array(
'value' => 'yes'
));
$long_login->setLabel('Keep me logged in');
$this->add($long_login);

// CSRF
$csrf = new Hidden('csrf');
$csrf->addValidator(new Identical(array(
'value' => $this->security->getSessionToken(),
'message' => 'CSRF validation failed'
)));
// $this->add($csrf);

}
}

在 Controller 中:

$form = new LoginForm();
if (!empty($_POST)) {
if (!$form->isValid($_POST)) {
$errors = array();
foreach ($form->getMessages() as $message) {
$errors[] = $message;
}
if (!empty($errors))
$this->flash->error(join('<br/>', $errors));
} else {
//Login Continues
}
}


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

要将此表单转换为 html,代码如下:

    <div class="form-group">
{{ form.label('email',['class': 'control-label']) }}
{{ form.render('email', ['class': 'form-control input-md']) }}
</div>
<div class="form-group">
{{ form.label('password',['class': 'control-label']) }}
{{ form.render('password', ['class': 'form-control input-md']) }}
</div>
<div class="checkbox">
{{ form.render('long_login') }}
{{ form.label('long_login') }}
</div>

关于forms - Phalcon 使用 Phalcon\Tag 或 Phalcon\Forms 创建表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com