我是symfony的新手.我为表单提交编写了一个代码.
public function executeEdit(sfWebRequest $request)
{
$userId = $request->getParameter('id');
$this->user = sfGuardUserTable::getUserById($userId);
$this->form = new editOwnerForm($this->user);
$this->paymentForm = new profilePaymentInfoForm($this->user->getPaymentInformation());
if ($request->getMethod() == sfWebRequest::POST) {
$this->form->bind($request->getParameter($this->form->getName()));
if ($this->form->isValid()) {
$this->form->save();
$this->message = 'Successfully updated owner details';
} else {
var_dump($this->form->renderGlobalErrors()); die;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的表格总是无效的.(总是运行其他形式的部分有效)但我找不到原因.var_dump($this->form->renderGlobalErrors());不要发出任何消息.我该如何解决呢?
在Symfony2表单中,当试图获取实体时,Symfony期望接收QueryBuilder对象,但有时没有返回实体.在这种情况下,会出现一条错误消息:
期望类型为"Doctrine\ORM\QueryBuilder"的参数,给出"NULL"
如何使query_builder允许选项没有可用的实体.
$builder
->add('client', 'entity', array(
'class' => 'Faktura\FakturaBundle\Entity\Client',
'query_builder' => function(\Web\MyBundle\Repository\ClientRepository $er) use ($company){
return $er->getClients($company);
))
;
Run Code Online (Sandbox Code Playgroud)
ClientRepository.php
public function getClients($company)
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.company = :company')
->setParameter('company', $company)
->getQuery();
return $qb->getResult();
}
Run Code Online (Sandbox Code Playgroud)
实际上,它只是基本$er->findBy(array('company' => $company))方法,但我使用自定义getClients()方法
我已经创建了精明的联系表单捆绑包,它工作正常,但我想使用knp-pagination捆绑包显示所有联系表数据,但它不起作用所以请帮我解决这个问题.
我收到了这个错误
FileLoaderLoadException: Cannot import resource "C:\wamp\www\Acme\app/config\config.yml" from "C:\wamp\www\Acme\app/config/config_dev.yml". (There is no extension able to load the configuration for "knp_paginator" (in C:\wamp\www\Acme\app/config\config.yml). Looked for namespace "knp_paginator", found "framework", "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "savvy_contact", "web_profiler", "sensio_distribution")
Run Code Online (Sandbox Code Playgroud)
*这是我的config.yml*
imports:
- { resource: parameters.yml }
- { resource: security.yml }
framework:
#esi: ~
translator: { fallback: "%locale%" }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: …Run Code Online (Sandbox Code Playgroud) 我开始学习sf2,非常酷,因为我的问题我有两个表:
媒体
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Website", inversedBy="medias")
* @ORM\JoinTable(name="media_website")
private $websites;
Run Code Online (Sandbox Code Playgroud)
和网站
/**
* @ORM\ManyToMany(targetEntity="Test\SiteBundle\Entity\Media", mappedBy="websites")
private $medias;
Run Code Online (Sandbox Code Playgroud)
在我的MediaType.php中我有这个:
$builder
->add('title')
->add('website', 'entity', array(
'class' => 'TestSiteBundle:Website',
'property' => 'name',
'query_builder' => function(WebsiteRepository $er)use($user_id) {
return $er->getMyWebsites($user_id);
},
'multiple'=>false))
Run Code Online (Sandbox Code Playgroud)
最后,在枝条页面中我有这个:
<div class="form-group">
{{ form_label(form.description, "Description", { 'label_attr': {'class': 'control-label col-md-2'} }) }}
<div class="col-md-5">
{{ form_widget(form.description, { 'attr': {'class': 'form-control'} }) }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
当我尝试添加媒体时出现此错误:
Neither the property "websites" nor one of the methods "setWebsites()", "__set()" or "__call()" exist and …Run Code Online (Sandbox Code Playgroud) 我正在使用引导程序表单主题。这是我的 fields.html.twig 表单模板:
{% block form_row %}
{% spaceless %}
<div class="form-group {% if errors|length > 0 %}has-error{% endif %}">
{{ form_label(form, label|default(null), { 'label_attr': {'class': 'control-label'} }) }}
{{ form_errors(form) }}
{% set class='' %}
{% if attr.class is defined %}
{% set class = attr.class %}
{% endif %}
{{ form_widget(form, { 'attr': {'class': 'form-control ' ~ class} }) }}
</div>
{% endspaceless %}
{% endblock form_row %}
Run Code Online (Sandbox Code Playgroud)
问题是它不尊重复选框。它像普通输入字段一样呈现复选框。知道如何根据上述模板为复选框设置模板吗?
Symfony3表单:我已设法构建并呈现如下所示的表单:
<form action="/member/john/actions" method="post" name="form">
<input type="submit" value="Block John" name="block">
<input type="submit" value="Remove from my friends" name="remove">
<input type="hidden" value="LeiajURspTa9c8JEUYtvepki0b_CdL9dMWqEZxOYvfk" name="form[_token]" id="form__token">
</form>
Run Code Online (Sandbox Code Playgroud)
单击按钮"Block John"或时"Remove from my friends",控制器将其路由到所需位置(member_friend_actions),并且能够在死亡之前显示调试转储值和"Submitted!"文本.
我的路由器"member_friend_actions"的控制器设置如下:
/**
* A common post location to catch all operations like add/remove/cancel/block friends
*
* @Route("/{username}/actions", name="member_friend_actions")
* @Method("POST")
*/
public function allActionsFriendAction(Request $request, User $friend)
{
$form = $this->createAllActionsFriendForm($friend);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$clicked = $form->getData();
$clicked …Run Code Online (Sandbox Code Playgroud) Symfony版本:3.1.3
我有一个像下面的控制器,
public function studentAddClassAction( $id, Request $request )
{
$em = $this->getDoctrine()->getManager();
$student= new Student();
// new class object and create the form
$classes= $em->getRepository('PIE10Bundle:Classes')->findAll();
$form = $this->createForm(ClassType::class, $classes);
$form->handleRequest($request);
if( $form->isSubmitted() && $form->isValid() )
{
$parent = $form['parent']->getData(); // the parentID
$cName = $form['class']->getData();
$classID= $cName->getId(); // the classID
//insert new row to the student table
$student->setParentId( $parent );
$student->setClassId( $classID );
$student->setUserId( $id );
$em->persist($student);
$em->flush();
$this->addFlash('notice',
'Student Updated');
return $this->redirectToRoute('user_students');
}
// some other code
} …Run Code Online (Sandbox Code Playgroud) 我需要在 FormType 中检查哪个字段已更改。有什么方法可以做到吗?我已经搜索了一段时间,然后尝试以几种方式(也使用表单事件)获取编辑过的实体字段以捕获编辑过的字段,但没有简单的结果。
有什么方法可以轻松做到这一点,或者我需要在制作这样的东西时更有创意?最好的办法是,如果我能得到一个实体类型的例子,但任何线索都会很棒。
PS我不能在客户端做 - 由于特殊原因,我必须在服务器端做。
我创建了一个带有选项卡的页面(使用引导程序),并且所有选项卡都有自己的表单。而且它们都工作得很好。我的问题是当我收到表单错误时,我想打开错误所在的选项卡。
我当前的控制器代码:
public function add(Request $request, EntityManagerInterface $em): Response {
$this->denyAccessUnlessGranted('ROLE_USER');
$entityAdd = new \App\Entity\add();
$form = $this->createForm(\App\Form\Add::class, $entityAdd);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
Run Code Online (Sandbox Code Playgroud)
AddFormType.php 中的函数
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add(...)
->add(...)
->add(...)
->getForm()
;
}
Run Code Online (Sandbox Code Playgroud)
我不是自己创建表单,我使用的是 symfony 表单组件,但我不确定如何修改表单方法以将表单哈希(例如 /add#list-tab1)添加到表单方法中。
{{ form_start(AddForm) }}
{{ form_row(AddForm.name1) }}
{{ form_row(AddForm.name2) }}
{{ form_row(AddForm.save) }}
{{ form_end(AddForm) }}
Run Code Online (Sandbox Code Playgroud) 我是 Symfony 的新手,我想使用choiceType.
所以我将这段代码添加到buildForm方法中:
$builder->add('applications', ChoiceType::class, [
'choices' => [
'-- Choisir --' => NULL,
'Admin' => '2',
'Super' => '1',
'visiteur' => '10',
'admin local' => '98',
'partenaire gestionnaire' => '16',
'admin national' => '15',
'soutien' => '11',
'exploitant' => '12',
'moe' => '99',
]
]);
Run Code Online (Sandbox Code Playgroud)
但是当我检查 HTML 代码时,我没有正确的数字:
<select id="user_applications" name="user[applications]" class="form-control">
<option value="0" selected="selected">-- Choisir --</option>
<option value="1">Admin</option>
<option value="2">Super</option>
<option value="3">visiteur</option>
<option value="4">admin local</option>
<option value="5">partenaire gestionnaire</option>
<option value="6">admin national</option>
<option value="7">soutien</option> …Run Code Online (Sandbox Code Playgroud) 我熟悉Symfony Form类API。
我没有得到的一件事是这种方法:
/**
* Returns the extra data.
*
* @return array The submitted data which do not belong to a child
*/
public function getExtraData()
{
return $this->extraData;
}
Run Code Online (Sandbox Code Playgroud)
我无法使用此方法返回任何值。当我尝试发布未映射/无法识别的输入时,它总是返回一个空数组。
请帮助我了解此方法的实际用例。数据何时变为“额外”?
一个代码示例将是理想的。
非常感谢你。
symfony-forms ×11
symfony ×10
php ×6
doctrine-orm ×3
bootstrap-4 ×1
controller ×1
forms ×1
many-to-many ×1
symfony-1.4 ×1
symfony-3.1 ×1
symfony1 ×1
symfony4 ×1