我应该如何read-only使用Symfony表单组件渲染字段?
这就是我试图这样做无济于事的方式:
Symfony 2
$builder
->add('descripcion', 'text', array(
'read_only' =>'true'
));
}
Run Code Online (Sandbox Code Playgroud)
Symfony 3
$builder
->add('descripcion', TextType::class, array(
'read_only' => 'true'
));
}
Run Code Online (Sandbox Code Playgroud) 我想分离表单验证逻辑:
public function contactAction()
{
$form = $this->createForm(new ContactType());
$request = $this->get('request');
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->isValid()) {
$mailer = $this->get('mailer');
// .. setup a message and send it
return $this->redirect($this->generateUrl('_demo'));
}
}
return array('form' => $form->createView());
}
Run Code Online (Sandbox Code Playgroud)
我想翻译成两个单独的行动:
public function contactAction()
{
$form = $this->createForm(new ContactType());
return array('form' => $form->createView());
}
public function contactSendAction()
{
$form = $this->createForm(new ContactType());
$request = $this->get('request');
if ($request->isMethod('POST')) {
$form->submit($request);
if ($form->isValid()) {
$mailer = $this->get('mailer');
// .. setup a message and …Run Code Online (Sandbox Code Playgroud) 我需要设置symfony2表单元素的值.我在Controllers Action中使用了一个doctrine2实体,一个Symfony\Component\Form\AbstractType和createForm()方法.
$saleDataForm = $this->createForm(new SaleType(), $sale);
Run Code Online (Sandbox Code Playgroud)
现在,我如何从该表单中获取元素,以及如何设置它的值?我想做这样的事情,但它不起作用:
$saleDataForm->get('image')->setValue('someimapge.jpg');
Run Code Online (Sandbox Code Playgroud)
FYI:我需要这样做,以正确渲染场(使用这种方法,我像场总是空的,我需要将其设置为内容的ImagePath呈现上传图像的预览)
我有一个表单,其中包含数据库中实体的选择字段:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categories', 'document', array(
'class' => 'Acme\DemoBundle\Document\Category',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'empty_value' => false
));
}
Run Code Online (Sandbox Code Playgroud)
此表单将生成复选框列表,并将呈现为:
[ ] Category 1
[ ] Category 2
[ ] Category 3
Run Code Online (Sandbox Code Playgroud)
我想在此列表中按值禁用某些项目但我不知道在哪里拦截选择字段项目来执行此操作.
有人知道解决方案吗?
我创建了一个Symfony2表单并将其绑定到Request.在继续表单的其余部分之前,我需要明确确保CSRF令牌是否有效/无效.
$form['_token']->isValid()抛出OutOfBoundsException消息"Child _token不存在".
我仍然可以验证渲染的表单是否包含_token字段.如果CSRF值无效,则$form->isValid()返回false.
我在这里错过了什么?
更新1:
控制器(部分):
private function buildTestForm() {
$form = $this->createFormBuilder()
->add('name','text')
->getForm();
return $form;
}
/**
* @Route("/test/show_form", name="test.form.show")
* @Method("GET")
*/
public function showFormTest()
{
$form = $this->buildTestForm();
return $this->render('TestBundle::form_test.html.twig', array('form' => $form->createView()));
}
/**
* @Route("/test/submit_form", name="test.form.submit")
* @Method("POST")
*/
public function formTest()
{
$form = $this->buildTestForm();
$form->bind($this->getRequest());
if ($form['_token']->isValid()) {
return new Response('_token is valid');
} else {
return new Response('_token is invalid');
}
}
Run Code Online (Sandbox Code Playgroud)
模板 …
我的表单看起来像这样:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$factory = $builder->getFormFactory();
$builder->add('name');
$builder->add('description');
$builder->add('manufacturers', null, array(
'required' => false
));
$builder->add('departments', 'collection', array(
'type' => new Department
));
}
Run Code Online (Sandbox Code Playgroud)
我在表单所代表的实体上有一个类验证器,它调用:
if (!$valid) {
$this->context->addViolationAtSubPath('departments', $constraint->message);
}
Run Code Online (Sandbox Code Playgroud)
这只会在表单中添加"全局"错误,而不会在子路径中添加错误.我假设这是因为departments是嵌入另一个FormType的集合.
如果我改为departments其他字段之一,它可以正常工作.
如何才能将此错误显示在正确的位置?我假设如果我的错误是在集合中的单个实体上,并因此以子表单形式呈现,它将正常工作,但我的标准是如果集合中没有任何实体被标记为活动,则会发生违规,因此它需要在父母一级.
FormType:
class BranchFormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('name');
}
public function getName() {
return 'branch';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\MainBundle\Entity\Branch',
));
}
}
Run Code Online (Sandbox Code Playgroud)
在实体定义中:
<field name="name" column="name" type="string" length="255"/>
Run Code Online (Sandbox Code Playgroud)
没有nullable = true,并且字段在呈现时具有必需属性.
Validation.yml:
My\MainBundle\Entity\Branch:
properties:
name:
- NotBlank: ~
Run Code Online (Sandbox Code Playgroud)
symfony是自动查找此文件还是必须包含在某处?doc只是声明表单自动使用验证服务.
控制器:
$branch = new Branch();
$form = $this->createForm(new BranchFormType(), $branch);
if ($request->isMethod('POST')) {
$form->bindRequest($request);
if ($form->isValid()) {
$em->persist($branch);
$em->flush();
return $this->redirect($this->generateUrl('view_branch'));
}
}
return $this->render('MyMainBundle:Branch:create.html.twig', array(
'form' => $form->createView() …Run Code Online (Sandbox Code Playgroud) 我遇到令牌和文件字段表单的问题.
表单的验证如下:
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'fields' => array(
'file' => new File(
array(
'maxSize' => '2M',
'mimeTypes' => array(
'application/pdf', 'application/x-pdf',
'image/png', 'image/jpg', 'image/jpeg', 'image/gif',
),
)
),
)
));
return array(
'validation_constraint' => $collectionConstraint
}
Run Code Online (Sandbox Code Playgroud)
当我上传一个无效的大小文件(~5MB)时,我得到了这个错误,这是我希望的:
The file is too large. Allowed maximum size is 2M bytes
Run Code Online (Sandbox Code Playgroud)
但是当我上传一个太大的文件(~30MB)时,错误会发生变化:
The CSRF token is invalid. Please try to resubmit the form
The uploaded file was too large. Please try to upload a smaller file
Run Code Online (Sandbox Code Playgroud)
问题是错误令牌.我的格式是{{form_rest(form)}}代码.我认为错误更改是因为: …
我一直在使用Symfony Framework一段时间了,我一直想知道这些问题.
为什么表单元素在Symfony Framework中称为类型?
非常感谢向我解释.
我有一个Symfony的实体表格:
class MyType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
...
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'LogicielBundle\Entity\FichierGroup',
'intention' => $this->getName() . '_token'
));
}
Run Code Online (Sandbox Code Playgroud)
但是在POST_SUBMIT事件中,我想返回null(没有实体).我测试了这个,但没有工作:
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
.... my condition ...
$event->setData(null);
});
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗 ?谢谢 :)
symfony-forms ×10
symfony ×9
symfony-2.1 ×3
csrf ×1
doctrine-orm ×1
forms ×1
image-upload ×1
php ×1
validation ×1