【发布时间】:2017-11-24 13:38:51
【问题描述】:
我正在按照 Sylius 文档上的教程来自定义表单。
这是我所拥有的:
src\AppBundle\Form\Extension\CustomerProfileTypeExtension.php:
<?php
namespace AppBundle\Form\Extension;
use Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
final class CustomerProfileTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
// Adding new fields works just like in the parent form type.
$builder->add('contactHours', TextType::class, [
'required' => false,
'label' => 'app.form.customer.contact_hours',
]);
// To remove a field from a form simply call ->remove(`fieldName`).
// $builder->remove('gender');
// You can change the label by adding again the same field with a changed `label` parameter.
$builder->add('lastName', TextType::class, [
'label' => 'app.form.customer.surname',
]);
}
/**
* {@inheritdoc}
*/
public function getExtendedType(): string
{
return CustomerProfileType::class;
}
}
src\AppBundle\Entity\Customer.php:
<?php
namespace AppBundle\Entity;
use Sylius\Component\Core\Model\Customer as BaseCustomer;
class Customer extends BaseCustomer
{
/**
* @var string|null
*/
private $contactHours;
/**
* @return string
*/
public function getContactHours(): ?string
{
return $this->contactHours;
}
/**
* @param string $contactHours
*/
public function setContactHours(?string $contactHours): void
{
$this->contactHours = $contactHours;
}
}
AppBundle/Resources/config/services.yml : 服务: …… app.form.extension.type.customer_profile: 类:AppBundle\Form\Extension\CustomerProfileTypeExtension 标签: - {名称:form.type_extension,extended_type:Sylius\Bundle\CustomerBundle\Form\Type\CustomerProfileType }
app\Resources\SyliusShopBundle\views\Account\profileUpdate.html.twig
<div class="two fields">
<div class="field">{{ form_row(form.birthday) }}</div>
<div class="field">{{ form_row(form.contactHours) }}</div>
</div>
Sylius 1.0.4.
我在姓氏的标签中得到了预期的“app.form.customer.surname”。 但是我的字段“contactHours”没有出现......
有什么想法吗? 我的“app\Resources\SyliusShopBundle\views\Account\profileUpdate.html.twig”可以吗?
【问题讨论】: