我是Zend框架的新手,正在尝试在表单中的字段集中填充选择字段。我跟踪了各种链接,但似乎都没有成功。
我有两个表Item&Category,类别ID作为外键存在于item表中。我正在尝试加载可用类别,以便在“编辑”或“添加”模式下进行选择。
我以这些为基础
How to trigger fieldset factory in ZF3
ZF3 - Populate Select from Database
字段集工厂
class ItemFieldsetFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$category = $container->get(CategoryRepository::class);
$categories = $category->findAllCategoriesAsArray();
$fieldset = $container->get('FormElementManager')->get(\Form\ItemFieldset::class);
$fieldset->setCategories($categories);
return $fieldset;
}
}
字段集
namespace Item\Form;
use Zend\Form\Fieldset;
use Zend\Form\Element\Select;
use Item\Model\Item;
use Zend\Hydrator\ReflectionHydrator as ReflectionHydrator;
class ItemFieldset extends Fieldset
{
private $categories = [];
public function setCategories(array $categories) {
$this->categories = $categories;
}
public function init()
{
$this->setHydrator(new ReflectionHydrator());
$this->setObject(new Item('', '', '', ''));
$this->add([
'type' => 'hidden',
'name' => 'itemid',
]);
$this->add([
'type' => Select::class,
'name' => 'categoryid',
'options' => [
'label' => 'Category',
'value_options' => $this->categories
]
]);
$this->add([
'type' => 'text',
'name' => 'description',
'options' => [
'label' => 'Description',
],
]);
$this->add([
'type' => 'text',
'name' => 'size',
'options' => [
'label' => 'Size',
],
]);
}
}
控制器工厂
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$formManager = $container->get('FormElementManager');
return new WriteController(
$container->get(ItemCommandInterface::class),
$formManager->get(ItemForm::class),
$formManager->get(ItemFieldset::class),
$container->get(ItemRepositoryInterface::class)
);
}
}
控制器
{
private $command;
private $form;
private $fieldset;
private $repository;
/**
* @param ItemCommandInterface $command
* @param ItemForm $form
*/
public function __construct(
ItemCommandInterface $command,
ItemForm $form,
ItemFieldset $fieldset,
ItemRepositoryInterface $repository
) {
$this->command = $command;
$this->form = $form;
$this->fieldset = $fieldset;
$this->repository = $repository;
}
public function addAction()
{
$req = $this->getRequest();
$viewModel = new ViewModel(['form' => $this->form]);
if (! $req->isPost()) {
return $viewModel;
}
$this->form->setData($req->getPost());
if (! $this->form->isValid()) {
return $viewModel;
}
$item = $this->form->getData();
try {
$item = $this->command->insertItem($item);
} catch (\Exception $ex) {
// An exception occurred; we may want to log this later and/or
// report it to the user. For now, we'll just re-throw.
throw $ex;
}
return $this->redirect()->toRoute(
'item/detail',
['itemid' => $item->getItemId()]
);
}
配置
namespace Item;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\Form;
use Zend\Fieldset;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'service_manager' => [
..
],
'controllers' => [
..
],
'router' => [
..
],
'form_elements' => [
'factories' => [
Form\ItemFieldset::class => Factory\ItemFieldsetFactory::class
]
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
我希望选择字段将包含所有可用类别(在编辑模式下选中当前类别)。
我只得到一个空白框
我认为我的ItemFieldsetFactory没有触发,但无法理解原因。我这样做的原因是,我现在试图通过调用不存在的方法以及通过vardump&exit来停止。
任何建议/指导都非常感谢。