Zend Framework 3表单字段集 [英] Zend Framework 3 Form Fieldset
问题描述
我真的是Zend-Framework 3的新手,我正在练习OOP,找不到关于使用字段集和图例制作Zend Form的简单解释/教程.基本上,我试图用HTML创建它:
Hy, I'm really new to Zend-Framework 3 and I'm practicing OOP, I can't find a simple explanation/tutorial on making a Zend Form with a fieldset and legend. Basically I'm trying to create this in HTML:
<form name="my_name">
<fieldset>
<legend>My legend value</legend>
<input type="checkbox" name="name_1" value="value_1">Value 1</input>
<input type="checkbox" name="name_2" value="value_2">Value_2</input>
<input type="checkbox" name="name_3" value="value_3">Value_3</input>
</fieldset>
<input type="button" value="Get values" id="btn"/>
</form>
我查看了有关Zend Forms,Collections和Fieldsets的官方文档,但这确实让我感到困惑.任何帮助将不胜感激.
I checked the official documentation about Zend Forms and Collections and Fieldsets, but it's really confusing me. Any help would be greatly appreciated.
推荐答案
首先,对不起,因为它会有点长.但这将描述实际的形式.所以请耐心等待!
First, I am sorry as it is going to be a bit long one. But this would describe the form in action. So be patient please!
假设您以ZF3默认的Application
模块而闻名.在Application
模块中创建了一些文件夹,用于分隔每个元素.您需要按如下方式创建它们.
Assuming you are known to ZF3 default Application
module. Some folders are created in the Application
module for separation of each element. You need to create them as follows.
让我们先创建您的字段集开始. Zend\Form\Fieldset
组件表示元素的可重用集合,并且依赖于Zend\From\Form
组件.这意味着您需要将其附加到Zend\Form\Form
.
Let's get started by creating your fieldsets first. Zend\Form\Fieldset
component represents a reusable set of elements and is dependent on Zend\From\Form
component. This means you need to attach this to Zend\Form\Form
.
模块/应用程序/src/表单/字段集/YourFieldset.php
<?php
namespace Application\Form\Fieldset;
use Zend\Form\Element;
use Zend\Form\Fieldset;
class YourFieldset extends Fieldset
{
public function __construct($name = null)
{
parent::__construct('your-fieldset');
$this->add([
'name' => 'name_1',
'type' => Element\Checkbox::class,
'options' => [
'label' => 'Value 1',
'use_hidden_element' => true,
'checked_value' => 'yes',
'unchecked_value' => 'no',
],
'attributes' => [
'value' => 'no',
],
]);
// Creates others as your needs
}
}
现在,我们将使用Zend\From\Form
附加从Zend\From\Fieldset
创建的字段集来创建表单.
Now we would create the form using Zend\From\Form
attaching the fieldset created from Zend\From\Fieldset
.
模块/应用程序/src/Form/YourForm.php
<?php
namespace Application\Form;
use Application\Form\Fieldset\YourFieldset;
use Zend\Form\Form;
class YourForm extends Form
{
public function __construct($name = null)
{
parent::__construct('your-form');
$this->add([
// This name will be used to fetch each checkbox from
// the CheckboxFieldset::class in the view template.
'name' => 'fieldsets',
'type' => YourFieldset::class
]);
$this->add([
'name' => 'submit',
'attributes' => [
'type' => 'submit',
'value' => 'Get Values',
'class' => 'btn btn-primary'
],
]);
}
}
我们的资料快要准备好了.如果要在控制器的任何操作中使用它,则需要使其可维护.因此,让我们做到这一点.
Our from is almost ready. We need to make it serviceable if we want it to be used in any action of a controller. So let's do that.
更新您的模块配置文件.如果service_manager
键不存在,则添加以下代码段,否则,仅更新factories
和aliases
键,如下所示.
Update your module config file. If service_manager
key does not exist then add the following snippet of code, otherwise, update only factories
and aliases
key as the following.
修复模块配置文件中的命名空间.
Fix namespaces in module config file.
module/Application/config/module.config.php
'service_manager' => [
'factories' => [
// Form service
Form\YourForm::class => Zend\ServiceManager\Factory\InvokableFactory::class,
// Other services
],
'aliases' => [
// Make an alias for the form service
'YourForm' => Form\YourForm::class,
],
],
现在可以使用表格了.这需要注入到我们的控制器中.当我在Application
模块上工作时,我会将表单注入到IndexController::class
的构造函数中.然后我们将在IndexController::fieldsetAction()
方法中使用该表格.
Now the form is ready to be used. This needs to be injected into our controller. As I am working on Application
module, I would inject the form into the IndexController::class
's constructor. Then we would be using that form inside IndexController::fieldsetAction()
method.
模块/应用程序/src/Controller/IndexController.php
<?php
namespace Application\Controller;
use Zend\Form\FormInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
protected $YourForm;
public function __construct(FormInterface $YourForm)
{
$this->YourForm = $YourForm;
}
public function fieldsetAction()
{
$request = $this->getRequest();
$viewModel = new ViewModel(['form' => $this->YourForm]);
if (! $request->isPost()) {
return $viewModel;
}
$this->YourForm->setData($request->getPost());
if (! $this->YourForm->isValid()) {
return $viewModel;
}
$data = $this->YourForm->getData()['fieldsets'];
echo '<pre>';
print_r($data);
echo '</pre>';
return $viewModel;
}
}
此控制器在其构造函数中接受参数时,我们需要为此控制器创建一个工厂(工厂将创建其他对象).
As this controller is taking argument in its constructor, we need to create a factory for this controller (a factory creates other objects).
模块/应用程序/src/工厂/控制器/IndexControllerFactory.php
<?php
namespace Application\Factory\Controller;
use Application\Controller\IndexController;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
// We get form service via service manager here
// and then inject controller's constructor
$YourForm = $container->get('YourForm');
return new IndexController($YourForm);
}
}
再次,我们需要更新模块配置文件.这次我们将在controllers
键下添加工厂,如下所示:
Once again, we need to update the module config file. We would add this time the factory under controllers
key as follows
'controllers' => [
'factories' => [
Controller\IndexController::class => Factory\Controller\IndexControllerFactory::class,
],
],
最后,在视图模板中回显表单,如下所示:
At the end, echo the form in the view template as follows:
模块/应用程序/视图/应用程序/索引/fieldset.phtml
<h1>Checkbox Form</h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url());
// Here is the catch, remember this name from the CheckboxForm::class
$fieldset = $form->get('fieldsets');
$name_1 = $fieldset->get('name_1');
$name_2 = $fieldset->get('name_2');
$name_3 = $fieldset->get('name_3');
$submit = $form->get('submit');
$submit->setAttribute('class', 'btn btn-primary');
$form->prepare();
echo $this->form()->openTag($form);
?>
<fieldset>
<legend>My legend value</legend>
<?= $this->formElement($name_1) ?>
<?= $this->formLabel($name_1) ?>
<?= $this->formElement($name_2) ?>
<?= $this->formLabel($name_2) ?>
<?= $this->formElement($name_3) ?>
<?= $this->formLabel($name_3) ?>
<?= $this->formSubmit($submit) ?>
</fieldset>
<?php
echo $this->form()->closeTag();
希望这对您有帮助!
这篇关于Zend Framework 3表单字段集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!