我有一个带有字段的经典表单:'username','password'和'birthday'.
以下是我的验证断言(在我的用户实体中):
.....
/**
* @var string
* @Assert\NotBlank(message="username.error.blank")
* @Assert\MinLength(limit="2", message="username.error.short")
*/
protected $username;
/**
* @var string
* @Assert\NotBlank(message="password.error.blank")
* @Assert\MinLength(limit="4", message="password.error.short")
*/
protected $password;
/**
* @Assert\True(message="password.error.different")
*/
public function isPasswordLegal()
{
return ($this->username != $this->password);
}
Run Code Online (Sandbox Code Playgroud)
问题是当我提交表格时它完全是空的:
那么,2个问题:
谢谢你的帮助 :-)
斯坦
我正在使用 Goutte,https://github.com/fabpot/goutte,并有以下代码,
$client = new Client();
$crawler = $client->request('GET', \Config::get('Eload2::url'));
$form = $crawler->selectButton('Submit')->form();
// add extra fields here
$client->submit($form);
Run Code Online (Sandbox Code Playgroud)
如何在提交表单之前将隐藏的输入字段添加到表单中?
我尝试了以下代码,
$domdocument = new \DOMDocument;
$formfield = new InputFormField ($domdocument->createElement('__EVENTTARGET', 'ctl00$ContentPlaceHolder1$DDLTelco'));
$formfield2 = new InputFormField ($domdocument->createElement('__EVENTARGUMENT',''));
$form->set($formfield);
$form->set($formfield2);
Run Code Online (Sandbox Code Playgroud)
但返回以下错误消息,
InputFormField 只能从输入或按钮标签(给定的 __EVENTTARGET)创建。
在Symfony 2.8上遇到一个小问题。我有几个数据库字段,一个是整数,一个是十进制。构建表单时,这些字段是下拉列表,因此我使用的是ChoiceType而不是IntegerType或NumberType。
表单实际上可以正常工作,两者之间的内部差异显然不会引起问题,我可以选择一个值并将其正确保存到数据库中。
现在,问题出在侦听器中。更改某些字段后,我需要启动一个额外的过程,因此我有一个事件侦听器,并且正在使用getEntityChangeSet()命令。
我要注意的是它会将这些字段报告为已更改,因为它识别出Vardump输出中可以看到的1000与“ 1000”之间的差异:
"baths" => array:2 [?
0 => 1.5
1 => "1.5"
]
Run Code Online (Sandbox Code Playgroud)
这导致监听器始终触发我的钩子,即使该值确实没有更改。如果我将表单类型更改为Integer,那只是一个文本输入,而我的下拉菜单也会丢失。您如何强制下拉ChoiceType将数字视为数字?
在我的实体中,这是正确定义的:
/**
* @var float
*
* @ORM\Column(name="baths", type="decimal", precision=10, scale=1, nullable=true)
*/
private $baths;
Run Code Online (Sandbox Code Playgroud)
以我的常规形式:
->add('baths', BathsType::class)
Run Code Online (Sandbox Code Playgroud)
引入:
class BathsType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => array_combine(range(1,10,0.5),range(1,10,0.5)),
'label' => 'Bathrooms:',
'required' => false,
'placeholder' => 'N/A',
'choices_as_values' => true,
]);
}
public function getParent()
{
return 'Symfony\Component\Form\Extension\Core\Type\ChoiceType';
}
}
Run Code Online (Sandbox Code Playgroud) 我在 Symfony3 应用程序中使用新的内置引导程序样式。
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
form_themes:
- bootstrap_3_horizontal_layout.html.twig
Run Code Online (Sandbox Code Playgroud)
我正在使用它来通过它的组件(标签和小部件)呈现表单。
所有标签都有一个宽度为 col-xs-2 的类,宽度的小部件类为 col-xs-10。
<div class="form-group">
{{ form_label(form.name) }}
<div class="col-md-9">
{{ form_widget(form.name) }}
</div>
<div class="col-md-offset-3 col-md-9">
{{ form_errors(form.name) }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我如何设置(在所有表单上全局设置,或在 form_row 上,或在每个组件上,例如 form_label/form_widget)要使用的类。我曾尝试添加类,但这只是添加到现有类而不是替换它。
{{ form_label(form.name, 'gfdg', {'label_attr': {'class': 'col-md-3'} }) }}
Run Code Online (Sandbox Code Playgroud)
哪个渲染 class="col-xs-2 col-md-3"
我正在使用symfony中的嵌入式表单,并且无法从嵌入式表单获取数据。
这是我的专利表格
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use AppBundle\Form\SubscriberAddressType;
class SubscriberDetailsType extends AbstractType {
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('firstname', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => [
'placeholder' => 'First Name',
'class' => 'form-control'
]])
->add('lastname', TextType::class, [
'label' => false,
'required' => true,
'error_bubbling' => true,
'attr' => …Run Code Online (Sandbox Code Playgroud) 我正在使用 Symfony3 形式。CountryType我的 Symfony 表单生成器中有一个。它工作正常。但假设用户属于一家位于西班牙的公司。对于该用户,我想将默认国家/地区设置为西班牙,然后显示其余国家/地区。我怎样才能在 Symfony3 中做到这一点。
我试过这个,但它不工作。
$builder->add("country", CountryType::class, array(
"label" => "Country",
"required" => false,
"preferred_choices" => array(
"ES" => "Spain",
),
));
Run Code Online (Sandbox Code Playgroud)
感谢您的时间。
我正在 Symfony 上重写旧的已弃用的 REST API,问题是如何在 Symfony 表单上映射和验证具有不同名称的字段。
一个例子,我有 Symfony 表单,其中包含字段:
->add('receiverCity', TextType::class, ['constraints' => new NotBlank()])
->add('receiverCountry', TextType::class, ['constraints' => new NotBlank()])
->add('receiverPostCode', TextType::class, ['constraints' => new NotBlank()])
Run Code Online (Sandbox Code Playgroud)
在请求的控制器中,我得到具有不同名称的相同字段,例如:
$data = ['city' => 'My city', 'country' => 'My country', 'postal' => 'My post code'];
Run Code Online (Sandbox Code Playgroud)
然后我手动提交表单$form->submit($data)。问题是在表单中映射和验证该字段的最佳方法是什么?我应该使用表单事件还是有更简单的方法来做到这一点?
我使用 symfony 表单,并且有一系列具有相同名称 + 索引号的字段,如下所示:in0、in1、in2、in3...
是否可以在 TWIG 中循环名称,例如:
{% for i in 0..10 %}
{{ form_row(propForm."in"~i) }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
谢谢!
在生成基于学说的表格时:
php symfony doctrine:build-form
Run Code Online (Sandbox Code Playgroud)
Symfony会覆盖我之前生成和编辑的表单,这是错误的!:)
有人知道如何生成单一表格吗?它应该是像这样的东西:
php symfony doctrine:build-form MODEL_NAME
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的Symfony2表单实现自定义字段类型,但无论出于何种原因,我都会遇到致命错误,如本问题标题中所述.
我已经从界面复制了原始声明,但无济于事:-(
<?php
namespace Yanic\HomeBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormValidatorInterface;
class ShowOnlyType extends AbstractType
{
/**
* {@inheritdoc}
*/
function buildView(FormViewInterface $view, FormInterface $form, array $options)
{
$view->addVars(array(
'value' => date( 'd/m/Y H:i', $options['value'] )
));
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DateTime'
));
}
public function getParent()
{
return 'form';
}
public function getName()
{
return 'showOnly';
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助
symfony-forms ×10
symfony ×9
php ×3
forms ×2
css ×1
doctrine ×1
doctrine-orm ×1
domcrawler ×1
loops ×1
symfony-1.4 ×1
symfony1 ×1
twig ×1