【发布时间】:2018-11-20 13:59:26
【问题描述】:
我真的对我的表单过滤器感到困惑。
我的测试项目包含 2 个模型。
class Category extends AbstractEntity
{
use Nameable; // just property name and getter and setter
/**
* @var boolean
* @ORM\Column(name="issue", type="boolean")
*/
private $issue;
/**
* @var Collection|ArrayCollection|Entry[]
*
* @ORM\OneToMany(targetEntity="CashJournal\Model\Entry", mappedBy="category", fetch="EAGER", orphanRemoval=true, cascade={"persist", "remove"})
*/
private $entries;
}
条目
class Entry extends AbstractEntity
{
use Nameable;
/**
* @var null|float
*
* @ORM\Column(name="amount", type="decimal")
*/
private $amount;
/**
* @var null|Category
*
* @ORM\ManyToOne(targetEntity="CashJournal\Model\Category", inversedBy="entries", fetch="EAGER")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id", nullable=false)
*/
protected $category;
/**
* @var null|DateTime
*
* @ORM\Column(name="date_of_entry", type="datetime")
*/
private $dateOfEntry;
}
如果有人需要 AbstractEntity
abstract class AbstractEntity implements EntityInterface
{
/**
* @var int
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
}
每个类别可以有多个条目。我正在使用Doctrine 来处理这种关系。这很好用。
我有一个基于这个 FieldSet 的表单:
$this->add([
'name' => 'id',
'type' => Hidden::class
]);
$this->add([
'name' => 'name',
'type' => Text::class,
'options' => [
'label' => 'Name'
]
]);
$this->add([
'name' => 'amount',
'type' => Number::class,
'options' => [
'label' => 'Summe'
]
]);
$this->add([
'name' => 'date_of_entry',
'type' => Date::class,
'options' => [
'label' => 'Datum'
]
]);
$this->add([
'name' => 'category',
'type' => ObjectSelect::class,
'options' => [
'target_class' => Category::class,
]
]);
所以我的表单会显示一个包含我的类别的下拉列表。好的。
要为我的条目实体加载类别,我使用过滤器。
$this->add([
'name' => 'category',
'required' => true,
'filters' => [
[
'name' => Callback::class,
'options' => [
'callback' => [$this, 'loadCategory']
]
]
]
]);
还有回调:
public function loadCategory(string $categoryId)
{
return $this->mapper->find($categoryId);
}
映射器可以很好地加载类别。伟大的。但表格无效,因为:
CashJournal\Model\Category 类的对象无法转换为 int
好的,所以我要删除过滤器,但现在它无法将属性设置为条目实体,因为设置器需要Category。表单错误说:
输入的步骤无效
在 Symfony 中,我可以创建一个ParamConverter,它将category_id 转换为一个有效的Category 实体。
问题 我如何将过滤器用作我的 ParamConver?
更新 此外,当我将 category_id 转换为 int 时,我会从表单中得到错误。
更新 2 我将我的 FieldSet 更改为:
class EntryFieldSet extends Fieldset implements ObjectManagerAwareInterface
{
use ObjectManagerTrait;
/**
* {@inheritDoc}
*/
public function init()
{
$this->add([
'name' => 'id',
'type' => Hidden::class
]);
$this->add([
'name' => 'name',
'type' => Text::class,
'options' => [
'label' => 'Name'
]
]);
$this->add([
'name' => 'amount',
'type' => Number::class,
'options' => [
'label' => 'Summe'
]
]);
$this->add([
'name' => 'date_of_entry',
'type' => Date::class,
'options' => [
'label' => 'Datum'
]
]);
$this->add([
'name' => 'category',
'required' => false,
'type' => ObjectSelect::class,
'options' => [
'target_class' => Category::class,
'object_manager' => $this->getObjectManager(),
'property' => 'id',
'display_empty_item' => true,
'empty_item_label' => '---',
'label_generator' => function ($targetEntity) {
return $targetEntity->getName();
},
]
]);
parent::init();
}
}
但这将退出并显示错误消息:
Entry::setDateOfEntry() 必须是 DateTime 的实例,给定字符串
【问题讨论】:
标签: php zend-framework zend-form zend-framework3