Php 渲染a<;的VOLT宏是什么;表格>;标签?
在Phalcon中,可以创建扩展Php 渲染a<;的VOLT宏是什么;表格>;标签?,php,forms,macros,phalcon,volt,Php,Forms,Macros,Phalcon,Volt,在Phalcon中,可以创建扩展Phalcon\form\form的自定义表单。这个类有一个名为setAction的方法,但是我找不到任何关于如何使用我指定的操作自动呈现标记的信息 我的表单名为RegisterForm,我将其传递给VOLT view,如下所示: $this->view->registerForm = new RegisterForm(new UserModel()); 在我的VOLT模板中,我可以使用registerForm.render('username')
Phalcon\form\form
的自定义表单。这个类有一个名为setAction
的方法,但是我找不到任何关于如何使用我指定的操作自动呈现
标记的信息
我的表单名为RegisterForm
,我将其传递给VOLT view,如下所示:
$this->view->registerForm = new RegisterForm(new UserModel());
在我的VOLT模板中,我可以使用registerForm.render('username')
宏自动呈现在我的表单中注册的输入字段
是否有任何宏将创建以下内容
<form action="/register" method="post">
在表单定义中。以下是如何以伏特表示表单:
{{ form('products/save', 'method': 'post') }}
<label for="name">Name</label>
{{ text_field("name", "size": 32) }}
<label for="type">Type</label>
{{ select("type", productTypes, 'using': ['id', 'name']) }}
{{ submit_button('Send') }}
{{ end_form() }}
{{form('products/save','method':'post')}
名称
{{text_字段(“名称”,“大小”:32)}
类型
{{select(“type”,productTypes,'using':['id','name'])}
{{提交按钮('Send')}
{{end_form()}}
更多信息请访问
您还可以查看Phalcon的项目以获取更多示例
更新:Lukasz要求提供一种解决方案,用自定义表单元素的属性呈现表单标记是我能找到的最接近问题的解决方案。经过一天的研究,在Phalcon的Slack频道聊天后,我意识到,做我想做的事情没有内在的方式 最简单的解决方案是创建一个扩展
Phalcon\Forms\Form
的BaseForm
类。以下是一个例子:
<?php
namespace MyNamespace\Common\Form;
use Phalcon\Forms\Form;
class BaseForm extends Form {
/**
* Creates <form> tag in VOLT view.
*/
public function startForm($method = 'post') {
return '<form action="'.$this->getAction().'" method="'.$method.'">';
}
/**
* Creates </form> tag in VOLT view.
*/
public function endForm() {
return '</form>';
}
/**
* Pushes all errors to flash container.
*/
public function error($name) {
if($this->hasMessagesFor($name)) {
foreach($this->getMessagesFor($name) as $message) {
$this->flash->error($message);
}
return true;
}
return false;
}
}
但是这个解决方案不使用扩展的
Phalcon\Form\Form
元素。这只是简单地使用助手。嘿,再一次,似乎在这一点上没有内置的方法。用我能找到的最接近的解决方案更新了我的答案。这些天我会做更多的研究,如果我发现了什么,会告诉你的。祝你好运!谢谢我回家后会试试这个。在我看来,这会损害前端代码的可读性。当您想要上传带有该表单的文件,或者想要向表单标签添加类或id,或者任何其他相关属性时,该怎么办。。。您将得到一个充满标记属性的数组!卢卡斯,这个主意看起来很棒。您甚至可以使用默认选项将该方法放在基窗体类中,并使用自定义选项(如class、fileupload e.t.c)在自定义类中进行扩展,如Timothy提到的@Timothy他正在使用扩展基窗体的类,以便他可以完成类中的所有工作,并使模板文件不受损坏。@Timothy的事情是,所有属性都应该添加到表单定义中,或者作为呈现
元素的函数的另一个参数。我在这里提供的类只是一个示例,在代码中,它比这个更高级。
<?php
namespace MyNamespace\Common\Form;
use Phalcon\Forms\Form;
class BaseForm extends Form {
/**
* Creates <form> tag in VOLT view.
*/
public function startForm($method = 'post') {
return '<form action="'.$this->getAction().'" method="'.$method.'">';
}
/**
* Creates </form> tag in VOLT view.
*/
public function endForm() {
return '</form>';
}
/**
* Pushes all errors to flash container.
*/
public function error($name) {
if($this->hasMessagesFor($name)) {
foreach($this->getMessagesFor($name) as $message) {
$this->flash->error($message);
}
return true;
}
return false;
}
}
# IndexController.php
public function index() {
$this->view->registerForm = new RegisterForm();
}
# index.volt
{{ registerForm.startForm() }}
{{ registerForm.endForm() }}
{{ registerForm.error('username') }}