更新:
在2.8.3的新安装中,添加了一组最小的文件(模板,控制器,实体,表单类型),问题仍然存在。 config.php
报告安装没有问题。安装在Windows 10 Pro,32位。文件如下所示。
控制器:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use AppBundle\Form\MemberType;
/**
* @Route("/member")
*/
class MemberController extends Controller
{
/**
* @Route("/edit/{id}", name="member_edit")
* @Template("Member/edit.html.twig")
*/
public function editMemberAction($id)
{
$em = $this->getDoctrine()->getManager();
$member = $em->getRepository('AppBundle:Member')->find($id);
$form = $this->createForm(new MemberType(), $member);
return [
'form' => $form->createView(),
'member' => $member,
];
}
}
表格类型:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class MemberType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fname', TextType::class, array(
'label' => 'First name',
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Member',
'label_attr' => ['style' => 'font-weight: bold;'],
'required' => false,
));
}
public function getName()
{
return 'member';
}
}
实体:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Table(name="member")
* @ORM\Entity
*/
class Member
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @var string
*
* @ORM\Column(name="fname", type="string", length=45, nullable=true)
* @Assert\NotBlank(message = "Member first name may not be blank")
*/
protected $fname;
/**
* Set fname
*
* @param string $fname
* @return Member
*/
public function setFname($fname)
{
$this->fname = $fname;
return $this;
}
/**
* Get fname
*
* @return string
*/
public function getFname()
{
return $this->fname;
}
}
模板:
{{ form_row(form.fname) }}
原始问题:
label_attr
中设置configureOptions
的表单类型会被忽略,但会在字段规范中显示时呈现。 Symfony是2.8.3。奇怪的是,2.8.3中的另一个应用程序不会忽略label_attr
中的configureOptions
。因此,以下代码中的差异不太可能,但这里是:
表格中的字段:
->add('fname', TextType::class, array(
'label' => 'First name:',
))
configureOptions:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Mana\ClientBundle\Entity\Member',
'required' => false,
'label_attr' => ['style' => 'font-weight: bold;'],
));
}
模板提取:
{% for member in form.members %}
{{ form_row(member.fname) }}
...
{% endfor %}
什么不导致这种行为,即执行的测试:
答案 0 :(得分:1)
我认为这是你正在尝试做的事情:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class MemberType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('fname', TextType::class, array(
'label' => 'First name',
'label_attr' => $options['label_attr'],
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Member',
'label_attr' => ['style' => 'font-weight: bold;'],
'required' => false,
));
}
public function getName()
{
return 'member';
}
}
与 T30 的解决方案相比,这只是一个小小的改进。如果你想要一个选项从一个复合形式继承到它的所有孩子,你别无选择,只能告诉孩子它继承它。
Symfony对表单类型的继承不是复合/复合关系,而是将其视为某种类继承。
答案 1 :(得分:0)
您是否尝试在label_attr
方法中移动buildForm
?像这样:
public function buildForm(FormBuilderInterface $builder, array $options){
$builder->add('fname', TextType::class, [
'label' => 'First name',
'label_attr' => ['style' => 'font-weight: bold']
]);
}