[英]Symfony Form Collection - Error
我有一個Symfony表單集合,該集合本質上是嵌入表單,並且將允許創建多個Specification對象,並且出現了許多錯誤,這些錯誤不應該出現。
筆記:
Symfony版本:2.3.30
實施:我以前使用過這種方法,並且根據Symfony文檔,它已經完美地工作了(我什至嘗試使用該方法從另一個項目中復制relevent實體和表單,這種方法確實適用於該方法,並且仍然出現相同的錯誤!)
碼:
實體:
<?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'
)
))
;
}
解決了。 我有一個名為Collection的學說實體,Product表單用於從中選擇一個項目(在此項目中,Collection本質上是一個Category)。 由於某種原因,這使表單對象進一步混亂,我想使用symfony表單集合。 它全都是命名空間,因此應該不會有類沖突,但是Symfony似乎不喜歡名為Collection的實體。
我將實體重命名為ProductCollection,它解決了嘗試使用嵌入式表單作為集合的問題。
感謝您的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.