在symfony 2.0中,如何使用表单中的一对一关联创建下拉列表?你们能举好榜样吗?
访问我的路线/message/new我将显示一个表单,用于向一个或多个客户发送新消息.表单模型包含(以及其他)Customer实体集合:
class MyFormModel
{
/**
* @var ArrayCollection
*/
public $customers;
}
Run Code Online (Sandbox Code Playgroud)
我想使用GET参数实现自动客户选择customers,如下所示:
message/new?customers=2,55,543
Run Code Online (Sandbox Code Playgroud)
现在只需拆分,并进行查询即可获得客户:
public function newAction(Request $request)
{
$formModel = new MyFormModel();
// GET "customers" parameter
$customersIds = explode($request->get('customers'), ',');
// If something was found in "customers" parameter then get entities
if(!empty($customersIds)) :
$repo = $this->getDoctrine()->getRepository('AcmeHelloBundle:Customer');
$found = $repo->findAllByIdsArray($customersIds);
// Assign found Customer entities
$formModel->customers = $found;
endif;
// Go on showing the form
} …Run Code Online (Sandbox Code Playgroud) <?php
namespace Tabl\VenueBundle\Tests\Form;
use Symfony\Component\Form\Test\TypeTestCase;
use Tabl\VenueBundle\Entity\Venue;
use Tabl\VenueBundle\Form\VenueType;
class VenueTypeTest extends TypeTestCase
{
public function testSubmitValidData() {
$formData = array(
'title' => 'Hello World',
);
$type = new VenueType();
$form = $this->factory->create($type);
$object = new Venue();
$object->setTitle('Hello World');
// submit the data to the form directly
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertEquals($object, $form->getData());
$view = $form->createView();
$children = $view->children;
foreach (array_keys($formData) as $key) {
$this->assertArrayHasKey($key, $children);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我一直收到此错误消息:
Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "places_autocomplete"
怎么解决这个问题?如何加载此类型以便我可以在表单上执行功能测试?
places_autocomplete是Ivory GMaps Bundle …
我想在Symfony中自定义表单呈现.我查看了文档,发现可以使用{{ form_theme }}标记设置渲染主题.如果当时只有一个表单存在,但如果在同一模板中有多个表单则不起作用,这种方法非常有效.
我的index.html.twig中的一个简单示例
{% extends 'base.html.twig' %}
{% block body %}
<h1>Hello Form 1</h1>
{% form_theme form1 'bootstrap_3_layout.html.twig' %}
{{ form_start( form1 ) }}
{{ form_end( form1 ) }}
<h1>Hello Form 2</h1>
{% form_theme form2 'bootstrap_3_horizontal_layout.html.twig' %}
{{ form_start( form2 ) }}
{{ form_end( form2 ) }}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
正如你可以看到有两个表单变量,我想form1与渲染'bootstrap_3_layout.html.twig',并form2用'bootstrap_3_horizontal_layout.html.twig'.我不知道Twig内部,但我认为主题覆盖了他们的块定义.
结果看起来像这样

所以我的问题是如何渲染具有给定主题的表单而不会相互干扰.有没有办法在单独的干净枝条过程中渲染表单?
我尝试使用自定义函数的Twig扩展,但似乎该函数使用相同的Twig_Environment.我还尝试使用{%render}%的子请求,但这也不起作用.
我正在使用symfony2,我想知道是否可以重命名表单中的字段.
我的意思是......假设我有一个实体
class MyEntity{
private $name
//more code
}
Run Code Online (Sandbox Code Playgroud)
我为这个实体创建了一个类型:
class MyEntityType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\MyEntity'
));
}
public function getName()
{
return 'entity';
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法重命名name窗体中的字段,但映射name属性工作.就像是:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('MySuperName', null, array("mapping" => "name"))
;
}
Run Code Online (Sandbox Code Playgroud)
那么表单参数名称变为entity[MySuperName]而不是entity[name]填充name实体上的属性?
我希望你能帮助我.我正在使用Symfony 2.x和Doctrine 2.x,我想创建一个由两个实体组成的表单.通过填写这一表格,我想将数据保存到两个学说实体.
为简单起见,我举了一个例子.多语种网上商店需要有英文和法文的名称和产品描述.我想用一个表单来创建一个新产品.此创建表单将包含Product实体(id; productTranslations; price,productTranslations)以及ProductTranslation实体(id; name; description; language,product)中的数据.生成的创建产品表单包含以下字段(名称;描述;语言(EN/FR);价格).
Product和ProductTranslation实体通过双向一对多关系相互关联.关系的拥有站点是ProductTranslation.
提交表单后,我希望将数据持久保存到两个实体(Product和ProductTranslation).这是出错的地方.我不能坚持这些数据.
因此,我尝试了以下内容:
产品实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Product
*
* @ORM\Table(name="product")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
*/
class Product
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="price", type="decimal", precision=10, scale=0)
*/
private $price;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\ProductTranslation", mappedBy="product")
*/
private $productTranslations; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一组带有额外文本输入的选项创建一个表单字段,如果您选择"其他",则需要填写这些输入:
How often do you exercise?
(*) I do not exercise at the moment
( ) Once a month
( ) Once a week
( ) Once a day
( ) Other, please specify: [ ]
Run Code Online (Sandbox Code Playgroud)
目前,我正在使用ChoiceType我设置的地方choices:
$form->add('exercise', Type\ChoiceType::class, array(
'label' => 'How often do you exercise?',
'choices' => [ 'I do not excerise at the moment' => 'not', ... ],
'expanded' => true,
'multiple' => false,
'required' => true,
'constraints' => [ new Assert\NotBlank() ], …Run Code Online (Sandbox Code Playgroud) 我有一个带有assotation字段类型的表单(相关实体列表).
我一直试图实现的是在"newAction"表单上过滤此列表(创建新实体).
例如,以下屏幕:
您可以看到两个可用的调查,但我只想显示第一个,因为其User字段值与当前用户相同.
这很令人困惑,因为我在调试时无法找到传递给Survey字段的值.
entity symfony-forms symfony doctrine-orm symfony2-easyadmin
[更新]:2019/06/24-23; 28
上载带有表单的文件时,遇到以下错误:
此值应为字符串类型
表单构建器设置FileType为:
FormType
class DocumentType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
/** @var Document $salle */
$document=$options['data']; //Unused for now
$dataRoute=$options['data_route']; //Unused for now
$builder->add('nom')
->add('description')
->add('fichier', FileType::class, array(
//'data_class' is not the problem, tested without it.
//see comments if you don't know what it does.
'data_class'=>null,
'required'=>true,
))
->add('isActif', null, array('required'=>false));
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'data_class'=>Document::class,
'data_route'=>null,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
而且我的getter和setter没有类型提示以确保UploadedFile::__toString()不会被调用:
Entity
class Document { …Run Code Online (Sandbox Code Playgroud) 我有一个名为 DynamicForm 的实体,它看起来像这样:
<?php
namespace App\Entity\Product\DynamicForm;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
/**
* Class DynamicForm
* @package App\Entity\Product
*
* @Entity
* @ORM\Table(name="product_dynamic_form")
*/
class DynamicForm
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(name="id", type="integer", unique=true, nullable=false)
*/
private ?int $id = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Product\DynamicForm\Component\Text", mappedBy="dynamicForm")
* @ORM\JoinColumn(name="component_text_id", referencedColumnName="id")
*/
private ?Collection $textComponents;
/**
* @return Collection|null
*/
public function getTextComponents(): ?Collection
{
return $this->textComponents;
}
/**
* @param Collection|null $textComponents
*
* @return DynamicForm
*/ …Run Code Online (Sandbox Code Playgroud) symfony ×10
symfony-forms ×10
doctrine-orm ×4
php ×4
doctrine ×2
entity ×1
forms ×1
one-to-one ×1
phpunit ×1
symfony4 ×1
twig ×1