Phalcon
save
功能正在询问必填字段,即使它可以从 post
获得。之前使用的是 tag
,一切都工作正常,并且能够完成完整的 CRUD
功能。现在我想实现验证并将代码从tag
升级到form
;更改后我无法执行save
或update
。对于视图,我使用 .volt
语法来渲染 form
。
始终收到错误消息,如 “名称为必填项”,即使其 硬编码。
可能会出现什么问题?
型号:
<?php
class Invoicestatus extends \Phalcon\Mvc\Model
{
protected $id;
protected $name;
protected $description;
public function setId($id)
{
$this->id = $id;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function setDescription($description)
{
$this->description = $description;
return $this;
}
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function getDescription()
{
return $this->description;
}
public function initialize()
{
$this->setSchema("invoice");
$this->setSource("invoicestatus");
$this->hasMany(
'Id',
'Invoice',
'InvoiceStatusId',
[
'alias' => 'Invoice',
'foreignKey' => [
'message' => 'The invoice status cannot be deleted because other invoices are using it',
]
]
);
}
public function getSource()
{
return 'invoicestatus';
}
public static function find($parameters = null)
{
return parent::find($parameters);
}
public static function findFirst($parameters = null)
{
return parent::findFirst($parameters);
}
}
?>
控制器:
<?php
$form = new InvoicestatusForm();
$invoicestatus = new Invoicestatus();
$data = $this->request->getPost();
if (!$form->isValid($data, $invoicestatus)) {
$messages = $form->getMessages();
foreach ($messages as $message) {
$this->flash->error($message);
}
return $this->dispatcher->forward(
[
"action" => "new",
]
);
}
//$invoicestatus->name = $this->request->getPost('name', 'string');
//$invoicestatus->description = $this->request->getPost('description', 'string');
//$success = $invoicestatus->save();
$success = $invoicestatus->save($data, array('name', 'description'));
if($success)
{
$form->clear();
$this->flash->success("Invoice Status successfully saved!");
$this->dispatcher->forward(['action' => 'index']);
}
else
{
$this->flash->error("Following Errors occured:");
foreach($invoicestatus->getMessages() as $message)
{
$this->flash->error($message);
}
$this->dispatcher->forward(['action' => 'new']);
}
?>
形式:
<?php
class InvoicestatusForm extends Form
{
public function initialize($entity = null, $options = null)
{
if (isset($options['edit']) && $options['edit']) {
$id = new Hidden('id');
} else {
$id = new Text('id');
}
$this->add($id);
$name = new Text('name', ['placeholder' => 'Name']);
$name->setFilters(["striptags","string",]);
$name->addValidators([new PresenceOf(["message" => "Name is required...",])]);
$this->add($name);
$description = new Text('description', ['placeholder' => 'Description']);
$this->add($description);
}
}
?>
这不是在验证之前和保存时发生的 notNullValidations。您可以在数据库中设置列类型 NULL 或通过以下方式禁用 notNullValidations:
Model::setup(
[
'notNullValidations' => false,
]
);
在我的例子中,我创建了一个非空布尔列 (
TINYINT(1)
),默认为 false (0
)。 在我将列设置为空后,带有隐藏错误消息“whatever_field is required”的静默故障消失了。
根据我的问题,
Null
从未被分配,并且被保存的对象被证明分配了非空值;事实上,仅当尝试用 false
!覆盖检索到的
true
时,才会发生错误
此外,调试日志表明在无提示故障之前可能没有尝试与数据库进行交互。 显然,数据库的类型也很重要,因为该问题最初未被注意到,因为该列是在使用 MariaDB 的系统上添加的,而存在问题的第二个系统正在使用 MySQL。