我有一个表单和一个子表单,我想合并定义为默认值的约束值和根表单添加的约束值。
我的子表单:
class DatesPeriodType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('start', DateType::class, [
'constraints' => [
new Date(),
]
])
->add('end', DateType::class, [
'constraints' => [
new Date(),
]
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->setDefault('error_bubbling', false)
->setDefault('constraints', [
new Callback([$this, 'validate']),
])
;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用新的约束选项将表单添加到根目录:
$builder
->add('judgmentPeriod', DatesPeriodType::class, [
'constraints' => [
new Valid(),
new Callback([
'callback' => [$this, 'datesAreEmpty'],
'groups' => ['insertionPeriod'],
]),
new Callback([
'callback' => [$this, 'validDates'], …Run Code Online (Sandbox Code Playgroud) 是否需要创建字段集合并将其存储在一个数据库列类型 json_array 中?
我的实体有 date_data 列,它是 json_array 类型。我想在前端渲染两个字段。
第一个字段 -> 起始日期类型。
第二个字段 -> 截止日期类型。
我使用 jQuery 中继器库,将此字段呈现为前端的中继器字段。并希望将来自转发器的字段数据存储在数据库的 date_data 列中,如下所示。
[{"from": '12/31/2009' , "to": '01/16/2010' }, {"from": '02/10/2011' , "to": '02/16/2011' }]
我正在使用 Symfony 表单组件。我的项目中有很多表格。
要完美地学习表单组件还有很长的路要走,但现在我喜欢它。我也喜欢自动验证等等。
所以。现在我想在我的项目中学习和使用React.js。
但似乎我无法像以前一样在项目中使用验证和表单生成器?我在吗?
我有以下代码:
use Symfony\Component\Validator\Constraints\Positive;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('x', IntegerType::class, [
'mapped' => false,
'required' => false,
'constraints' => [new Positive()]
])
}
Run Code Online (Sandbox Code Playgroud)
树枝形式如下:
{{ form_widget(form.x, { 'attr': {'class': 'form-control'} }) }}
Run Code Online (Sandbox Code Playgroud)
但是,呈现的表单 (HTML) 仍然允许用户输入带减号的值。如何更改它,以便呈现的形式禁止减号并在箭头输入上停止于 1?
我想以这种方式呈现symfony表单:

我的问题涉及单选按钮小部件:我不知道如何单独呈现每个选项,以便在这些单选按钮之间显示其他字段.
有没有办法用symfony形式实现这一目标?
谢谢!
我有一个表单,您可以在其中编辑对象"one"的属性.此对象与另一个对象"很多"具有一对多的关系.我希望用户能够从表单中选择将"many"对象分配给"one".我无法弄明白该怎么做!
马上:
\实体\ One.php
class One
{
...
/*
* @ORM\ManyToOne(targetEntity="many", inversedBy="one")
* @ORM\JoinColumn(name="manyId", referencedColumnName="id")
*/
protected $manyId;
...
}
Run Code Online (Sandbox Code Playgroud)
\控制器\ OneController.php
class OneController extends Controller
{
...
public function editAction($oneId, Request $request)
{
if ($oneId) {
$one = $this->getDoctrine()
->getRepository('One')
->find($oneId);
} else {
$one = new One();
}
$em = $this->getDoctrine()->getEntityManager();
$manyEntity = 'Bundle\Entity\Many';
$manyList = new EntityChoiceList($em, $manyEntity);
$form = $this->createFormBuilder($one)
->add('many', 'choice', array('choice_list' => $manyList))
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$entityManager …Run Code Online (Sandbox Code Playgroud) 使用Symfony中的文本字段类型,有一个修剪选项.我很确定该trim()操作是由Form\Extension\Core\EventListener\TrimListener班级进行的.它是该PRE_BIND事件的监听者并致电:
$event->setData(trim($event->getData()));
Run Code Online (Sandbox Code Playgroud)
我想提供自己的"normalize_spaces"选择:
$builder->add('last_name', 'text', array(
'label' => 'Last name',
'normlize_spaces' => true
));
Run Code Online (Sandbox Code Playgroud)
我怎样才能提供这个选项NormalizeSpacesListener?
class NormalizeSpacesListener implements EventSubscriberInterface
{
public function preBind(FormEvent $event)
{
$data = $event->getData();
if (is_string($data)) {
$event->setData(preg_replace('/[ ]{2,}/', ' ', $data));
}
}
public static function getSubscribedEvents()
{
return array(FormEvents::PRE_BIND => 'preBind');
}
}
Run Code Online (Sandbox Code Playgroud) 我在训练,但我在symfony 3
我有问题我得到这个错误
给出了"string","Test\FrontBundle\Form\Type\SheetType"类型的预期参数
SheetType.php上的代码是
<?php
namespace Test\FrontBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;
class SheetType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name',null,array('label'=>'Titre de l\'album'))
->add('type')
->add('artist')
->add('duration')
->add('released', DateType::class)
;
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的SheetController.php我这样做形成我的控制器我不知道我怎么能解决这一切我尝试别的我得到错误
public function createAction(Request $request)
{
$form = $this->createForm(new SheetType());
$form->handleRequest($request);
if($request->isMethod('post') && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($form->getData());
$em->flush();
return $this->redirect($this->generateUrl('test_front_sheet_list'));
}
return $this->render('TestFrontBundle:Sheet:create.html.twig', array('form' => $form->createView()));
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Symfony 3.3.9,当我尝试渲染表单时,我有以下错误:
在呈现模板期间抛出了异常("无法呈现表单,因为块名称数组包含重复项:"_ fos_user_registration_form_errors","user_errors","user_errors","fos_user_registration_errors","form_errors".").
预先感谢您的帮助 !
编辑17/09/2017 你去:
public function indexAction()
{
/** @var $formFactory FactoryInterface */
$formFactory = $this->get('fos_user.registration.form.factory');
$form = $formFactory->createForm();
return $this->render('AppTMMainBundle:Default:index.html.twig', array(
'form' => $form->createView(),
));
}
Run Code Online (Sandbox Code Playgroud)
我的树枝:
{{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register')}) }}
<div class="card-content">
<h3 class="text-center title" style="color: #3C4858;margin:10px 0;">Inscription</h3>
<div class="social text-center">
<a class="btn btn-just-icon btn-round btn-facebook" href="{{ path('hwi_oauth_service_redirect',{'service': 'facebook'}) }}">
<i class="fa fa-facebook"> </i>
</a>
<a class="btn btn-just-icon btn-round btn-twitter" href="{{ path('hwi_oauth_service_redirect',{'service': 'twitter'}) }}">
<i class="fa fa-twitter"></i>
</a>
<a class="btn btn-just-icon …Run Code Online (Sandbox Code Playgroud) 我的目的是使用Symfony表单允许用户选择多个兴趣并将其与用户相对保存。
我有三个实体来映射和存储此数据:
User
- id
- name
Interest
- id
- name
UserInterest
- id
- user_id (FK ManyToOne user.id)
- interest_id (FK ManyToOne interest.id)
Run Code Online (Sandbox Code Playgroud)
我在努力寻找最有活力的Symfony的方式来处理和保存Interest的A S User的UserInterest实体。
InterestsController.php
public function interests(Request $request)
{
$error = null;
$userInterests = new UserInterest();
$userInterests->setUser($this->getUser());
$form = $this->createForm(UserAccountInterests::class, $userInterests);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->entityManager->persist($userInterests);
$this->entityManager->flush();
} catch (\Exception $e) {
$error = $e->getMessage();
}
}
$parameters = [
'error' => $error,
'user_interests_form' …Run Code Online (Sandbox Code Playgroud) symfony-forms ×10
symfony ×8
symfony4 ×3
php ×2
twig ×2
constraints ×1
reactjs ×1
symfony-1.4 ×1
symfony-3.3 ×1
symfony1 ×1