简体   繁体   English

表格,表格和验证 - Symfony2,Twig,Doctrine

[英]Tables, forms and validation - Symfony2, Twig, Doctrine

I have some questions all conected, so I will ask all of them here: 我有一些问题都是连接的,所以我会在这里询问所有问题:

  1. [Solved] I have date fields which can be nullable and when I don't fill them they are displayed as 2012-08-09 - today's date, although when I check in the database they are NULL. [已解决]我有可以为空的日期字段,当我不填写它们时,它们显示为2012-08-09 - 今天的日期,但是当我在数据库中签入时它们是NULL。 I have @ORM\\Column(type="date", nullable=true) and @Assert\\Date above both of them. 我有两个@ORM \\ Column(type =“date”,nullable = true)@Assert \\ Date

  2. I have form validation @Assert\\Date and @Assert\\NotBlank() but I violate this constrains nothing happens although I have {{ form_errors(form) }} . 我有表单验证@Assert \\ Date@Assert \\ NotBlank()但我违反了这个约束,虽然我有{{form_errors(form)}}但没有任何反应 How to show what exactly is wrong with the form? 如何显示表单究竟出了什么问题?

  3. [Solved] The last one is that I have NotBlank() constrain above a field called $currency. [已解决]最后一个是我将NotBlank()约束在一个名为$ currency的字段上方。 This is how I add it: 这就是我添加它的方式:

     $builder->add('currency', 'choice', array( 'choices' => array( 'empty_value' => '--- Choose ---', 'USD' => 'USD', 'HKD' => 'HKD'))); 

    but if I don't set anything (it stays --- Choose ---- ) it accepts it, although it's empty. 但如果我没有设置任何东西(它保持---选择----)它接受它,虽然它是空的。 I want the default choice to be --- Choose ---, but it musn't be allowed to leave it this way and the form to be valid in the same time. 我希望默认选择是---选择---,但不允许这样离开它,并且表格在同一时间内有效。

I would appreciate your help! 非常感谢你的帮助!

For the first question, taken from the twig documentation: 对于第一个问题,取自twig文档:

If the value passed to the date filter is null, it will return the current date by default. 如果传递给日期过滤器的值为null,则默认情况下将返回当前日期。 If an empty string is desired instead of the current date, use a ternary operator: 如果需要空字符串而不是当前日期,请使用三元运算符:

{{ post.published_at is empty ? "" : post.published_at|date("m/d/Y") }}

对于第二个问题,这里有信息Symfony2:如何在将请求绑定到表单后获得表单验证错误抱歉没有找到它并质疑已经被质疑的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM