我正在尝试实现可靠的表单,我跟着this并且它似乎只是找不到字段。
查看:
<form action="{{ path('select_users') }}" method="POST" novalidate>
{{ form_row(form.company) }}
{{ form_row(form.user) }}
{{ form_rest(form) }}
<button type="submit" class="btn btn-success">
Next
</button>
</form>
错误:
Method "company" for object "Symfony\Component\Form\FormView" does not exist in EngMgmtBundle:Engineering:update.html.twig at line 110
注意:我没有使用示例中使用的“Location”模型,因为我不需要额外的字段或其他实体。 (至少我认为我没有)
我正在使用2.3版
我想要完成的任务:用户选择公司,用户字段应仅填写属于所选公司的用户。
形式:
class UsersForCompanyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$factory = $builder->getFormFactory();
$companySubscriber = new AddCompanyFieldSubscriber($factory);
$builder->addEventSubscriber($companySubscriber);
$userSubscriber = new AddUserFieldSubscriber($factory);
$builder->addEventSubscriber($userSubscriber);
$builder->add('comments', 'textarea', array (
'required' => true,
'label' => 'Comments:'
));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Bundle\UserBundle\Entity\Company'
));
}
/**
* @return string
*/
public function getName()
{
return 'users_for_company';
}
}
公司订阅者:
class AddCompanyFieldSubscriber implements EventSubscriberInterface
{
private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_BIND => 'preBind'
);
}
private function addCompanyForm($form, $company)
{
$form->add('company', 'entity', array(
'class' => 'UserBundle:Company',
'mapped' => false,
'data' => $company,
'empty_value' => 'Company',
'attr' => array(
'class' => 'company_selector',
),
'query_builder' => function (EntityRepository $repository) {
$qb = $repository->createQueryBuilder('company');
return $qb;
}
));
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$accessor = PropertyAccess::getPropertyAccessor();
$user = $accessor->getValue($data, 'user');
$company = ($data->getUser()) ? $data->getUser()->getCompany() : null ;
$this->addCompanyForm($form, $company);
}
public function preBind(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$company = array_key_exists('company', $data) ? $data['company'] : null;
$this->addCompanyForm($form, $company);
}
用户订阅者: class AddUserFieldSubscriber实现EventSubscriberInterface { 私人$ factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}
public static function getSubscribedEvents()
{
return array(
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_BIND => 'preBind'
);
}
private function addUserForm($form, $user, $company)
{
$form->add($this->factory->createNamed('user','entity', null, array(
'class' => 'UserBundle:User',
'empty_value' => 'Select User',
'data' => $user,
'auto_initialize' => false,
'attr' => array(
'class' => 'user_selector',
),
'query_builder' => function (EntityRepository $repository) use ($company) {
$qb = $repository->createQueryBuilder('user')
->innerJoin('user.company', 'company');
if ($company instanceof Company) {
$qb->where('user.company = :company')
->setParameter('company', $company);
} elseif (is_numeric($company)) {
$qb->where('company.id = :company')
->setParameter('company', $company);
} else {
$qb->where('company.name = :company')
->setParameter('company', null);
}
return $qb;
}
)));
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$accessor = PropertyAccess::getPropertyAccessor();
$user = $accessor->getValue($data, 'user');
$company = ($data->getUser()) ? $data->getUser()->getCompany() : null ;
$this->addUserForm($form, $user, $company);
}
public function preBind(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$company = array_key_exists('company', $data) ? $data['company'] : null;
$user = array_key_exists('user', $data) ? $data['user'] : null;
$this->addUserForm($form, $user, $company);
}
}
我在这里缺少什么?
答案 0 :(得分:0)
只是一个想法,但您是否在twig-template中尝试{{dump()}}以查看可用的变量?
这只是一个很长的镜头,但是在preBind()函数中,你正在检查$ data中是否存在'user'和'company'键,所以我的猜测是,在你的twig-template中你应该使用
{{ form_row(company) }} {# instead of {{ form_row(form.company) }} #}
{{ form_row(user) }} {# instead of {{ form_row(form.user) }} #}