【发布时间】:2016-07-17 15:55:19
【问题描述】:
我是 Phalcon 框架和他的 Volt 引擎的初学者,但我真的很想学习它。我正在关注他们的文档,我目前正在他们的 INVO 项目中。
他们有一个产品数据库,这些产品有一个类型(5-蔬菜,6-水果)。因此,当我列出所有产品时,它会显示有关该产品的所有数据,并为您提供“编辑”该特定产品数据的链接。因此,当我点击“编辑”时,我想打开一个 html 表单,其中包含已填充的字段以及来自该特定产品的数据。
这是他们用来为产品创建表单的表单类:
<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;
class ProductsForm extends Form
{
/**
* Initialize the products form
*
* @param null $entity
* @param array $options
*/
public function initialize($entity = null, $options = array())
{
if (isset($options['edit'])) {
$element = new Text("id");
$this->add($element->setLabel("Id"));
} else {
$this->add(new Hidden("id"));
}
$name = new Text("name");
$name->setLabel("Name");
$name->setFilters(array('striptags', 'string'));
$name->addValidators(
array(
new PresenceOf(
array(
'message' => 'Name is required'
)
)
)
);
$this->add($name);
$type = new Select(
'profilesId',
ProductTypes::find(),
array(
'using' => array('id', 'name'),
'useEmpty' => true,
'emptyText' => '...',
'emptyValue' => ''
)
);
$this->add($type);
$price = new Text("price");
$price->setLabel("Price");
$price->setFilters(array('float'));
$price->addValidators(
array(
new PresenceOf(
array(
'message' => 'Price is required'
)
),
new Numericality(
array(
'message' => 'Price is required'
)
)
)
);
$this->add($price);
}
}
这是来自控制器的操作:
/**
* Shows the view to "edit" an existing product
*/
public function editAction($id)
{
if (!$this->request->isPost()) {
$product = Products::findFirstById($id);
if (!$product) {
$this->flash->error("Product was not found");
return $this->forward("products/index");
}
$this->view->form = new ProductsForm($product, array('edit' => true));
}
}
这是我的 edit.volt 视图文件:
{{ form("products/whatever") }}
<h2>Search products</h2>
<fieldset>
{% for element in form %}
<div class="control-group">
{{ element.label(['class': 'control-label']) }}
<div class="controls">{{ element }}</div>
</div>
{% endfor %}
<div class="control-group">
{{ submit_button("Edit", "class": "btn btn-primary") }}
</div>
</fieldset>
现在,当我单击特定产品时,我会得到以下信息: Edit screen
如您所见...除了选择标记之外的所有内容都已填充并且工作正常...只是没有被选中的一个选择。 :( 我的意思是我可以点击下拉菜单,它会向我显示数据......但我希望它已经像其他所有内容一样被选中。
所以我的问题是,我是否可以在我的 edit.volt 中添加一个 if 语句(如果可能的话,如何做),就像“如果下一个元素是一个选择元素,只需选择它的值已经在数据库中”,就像在那个屏幕中一样,如果它的甜椒那么 5-Vegetables 应该已经被选中......然后如果你愿意,你可以更改它并保存它......但我只是希望它已经被选中。
我为这篇长文道歉。
【问题讨论】:
-
您的代码看起来不错。您选择输入的名称 (
profilesId),这也是您的product模型中配置文件列的名称吗? -
@Timothy 不,不是。在我的产品模型中,它称为 $product_types_id,这是另一个称为 ProductTypes ($id) 的模型的外键(该模型仅包含类型的 id 和名称:例如 id:5 name:Vegetables)。我使用 Phalcon Dev Tools 来创建我的模型并使用这些名称创建它,因为这就是这些列在数据库中的命名方式(顺便说一句:这是来自 Phalcon 文档教程......我使用他们的东西,因为我只是想学习这个,但显然我太笨了:()。
-
@Timothy,我唯一做的就是编辑.volt,因为他们没有提供该文件的代码,所以我只是从另一个文件中复制了它。我想我可以使用它,因为我只需要一个表单,它会填充给定的数据
-
然后将选择元素的名称从
profilesId更改为product_type_id -
@Timothy 谢谢,它现在可以工作了。现在我想知道为什么他们甚至将其命名为“profilesId”,这没有任何意义:/。哦,好吧,还是谢谢你的回答。