【发布时间】:2015-09-02 11:14:44
【问题描述】:
我有一个 Symfony 表单集合,它本质上是嵌入一个表单,并允许创建多个规范对象,但我遇到了许多不应该出现的错误。
注意事项:
Symfony 版本:2.3.30
实现:我以前使用过这种方法,并且根据 Symfony 文档它工作得很好(我什至尝试使用这种方法从另一个项目中复制相关实体和表单,该方法确实适用于这个方法,但仍然遇到相同的错误! )
代码:
实体:
<?php
/**
* product
* @package AppBundle\Entity
* @author Pete Robinson
**/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Utils\Traits\Doctrine\Timestampable;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="product")
*/
class Product
{
~snip snip snip - ID and other text fields~
/**
* specifications
* @ORM\OneToMany(targetEntity="Specification", mappedBy="product")
* @var object ArrayCollection
**/
private $specifications;
子实体:
<?php
/**
* product
* @package AppBundle\Entity
**/
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="specification")
*/
class Specification
{
/**
* product
* @ORM\ManyToOne(targetEntity="Product", inversedBy="specifications")
* @var object Product
**/
private $product;
形式:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
class ProductType extends AbstractType
{
/**
* set default options
* @return void
**/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'data_class' => 'AppBundle\Entity\Product'
));
}
/**
* build form
* @param FormBuilderInterface $buidler
* @param array $options
* @return void
**/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('specifications', 'collection', array(
'type' => new SpecificationType(),
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'cascade_validation' => true
))
;
错误:
The options "allow_add", "allow_delete", "type" do not exist. Known options
are: "action", "attr", "auto_initialize", "block_name", "by_reference",
"cascade_validation", "compound", "constraints", "csrf_field_name",
"csrf_message", "csrf_protection", "csrf_provider", "data", "data_class",
"disabled", "empty_data", "error_bubbling", "error_mapping",
"extra_fields_message", "inherit_data", "intention", "invalid_message",
"invalid_message_parameters", "label", "label_attr", "mapped", "max_length",
"method", "pattern", "post_max_size_message", "property_path", "read_only",
"required", "translation_domain", "trim", "validation_groups", "virtual"
据我所见,我已经正确地遵循了有关嵌入式表单的 Symfony 文档,就像我一直做的那样——但我遇到了这个问题。有人见过这个吗?
最终仅供参考:Composer.json
{
"name": "project",
"license": "proprietary",
"type": "project",
"autoload": {
"psr-0": {
"": "src/"
}
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.3.*",
"doctrine/orm": "~2.2,>=2.2.3,<2.5",
"doctrine/dbal": "<2.5",
"doctrine/doctrine-bundle": "~1.2",
"twig/extensions": "1.0.*",
"symfony/assetic-bundle": "~2.3",
"symfony/swiftmailer-bundle": "~2.3",
"symfony/monolog-bundle": "~2.4",
"sensio/distribution-bundle": "~2.3",
"sensio/framework-extra-bundle": "~3.0,>=3.0.2",
"sensio/generator-bundle": "~2.3",
"incenteev/composer-parameter-handler": "~2.0",
"friendsofsymfony/user-bundle": "~2.0@dev",
"doctrine/doctrine-fixtures-bundle": "2.2.*@dev",
"gregwar/image-bundle": "dev-master",
"gedmo/doctrine-extensions": "dev-master"
},
"scripts": {
"post-install-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
],
"post-update-cmd": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
]
},
"config": {
"bin-dir": "bin"
},
"minimum-stability": "stable",
"extra": {
"symfony-app-dir": "app",
"symfony-web-dir": "web",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
}
}
}
编辑:下面的 SpecificationType - 在与 ProductType 相同命名空间的同一目录中
<?php
/**
* specification form
**/
namespace AdminBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
class SpecificationType extends AbstractType
{
/**
* set default options
* @return void
**/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'csrf_protection' => false,
'data_class' => 'AppBundle\Entity\Specification'
));
}
/**
* build form
* @param FormBuilderInterface $buidler
* @param array $options
* @return void
**/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value', 'text', array(
'label' => 'Value *',
'label_attr' => array(
'class' => 'col-sm-2 control-label'
),
'attr' => array(
'class' => 'form-control'
),
'constraints' => array(
new NotBlank(array(
'message' => 'Specification value is required'
))
)
))
->add('remove', 'button', array(
'attr' => array(
'class' => 'btn btn-danger right padded remove_item'
)
))
;
}
【问题讨论】:
-
D4VID - 是的 - 同一个目录。抱歉,错过了 SpecificationType。已添加。
-
您是否用自己的类型覆盖了内置的
collection类型?这可以解释没有这些选项的原因...... -
您好 Jovan,感谢您的回复。“覆盖”是什么意思?
-
我曾多次看到人们用
email、text等命名自己的表单类型,有效地覆盖 内置Symfony类型。你碰巧有collection这样的行为吗? -
Jovan - 啊,我明白你的意思了。不,我还没有实现。没有对 Collection 类型进行任何修改,全部由 Symfony formbuilder 包提供。
标签: php forms symfony collections