简体   繁体   English

symfony日期验证本地化

[英]symfony date validation localization

I have a simple form and I use jquery UI date-picker for birthday input. 我有一个简单的表单,我使用jquery UI date-picker作为生日输入。

and in the server side I use symfony validation with Date validation. 在服务器端,我将symfony验证与Date验证一起使用。

the problem is that the validation accepts only inputs with this format: YYYY-MM-DD 问题是验证仅接受以下格式的输入:YYYY-MM-DD

but in other locals like in UK the format is: DD/MM/YYYY 但在其他本地人(例如在英国)中,格式为:DD / MM / YYYY

and the server gives error on it. 服务器给出错误。

Do you have any suggestions? 你有什么建议吗?

I use the symfony form object with the inside validation, so I would prefer not to force a format change manually. 我在内部验证中使用了symfony表单对象,因此我不希望手动强制更改格式。

As you can read from the documentation " date ", the constraint dates performs validation only format "YYYY-MM-DD". 正如您可以从文档“ date ”中看到的那样,约束日期仅执行验证格式“ YYYY-MM-DD”。

So you have two choices: 因此,您有两种选择:

one (the simplest and most logical) is to set the format manually in the form field " format ": 一种(最简单,最合理的方法)是在“ 格式 ”字段中手动设置格式

$builder->add('date', 'date', array(
    'widget' => 'single_text',
    'format' => 'dd-MM-yyyy',
));

in this way will be the form itself to reverse the date and pass the correct format to the validator (and the database). 这样,表单本身就可以逆转日期并将正确的格式传递给验证器(和数据库)。

The other is to create a constraint callback " callback ", and creating your own logic for validation. 另一种是创建约束回调“ callback ”,并创建自己的验证逻辑。

There would also be another way, even if it is inserted in the bonds of strings, and it is the constraint regex " regex ". 即使将其插入字符串的键中,也将存在另一种方式,它是约束正则表达式“ regex ”。

In this way you will need to create a regular expression very precise (and complex). 这样,您将需要创建一个非常精确(复杂)的正则表达式。

For a problem as simple as the first solution is the most suitable! 对于第一个解决方案这样简单的问题,最合适!

EDIT: 编辑:

jquery ui datepicker locks to an input type text, you do not need to create it manually. jQuery ui datepicker锁定为输入类型的文本,您无需手动创建它。

As I wrote in the example, if you use the 'widget' => 'single_text' will produce an input type text! 如我在示例中所写,如果您使用'widget' => 'single_text'将生成输入类型的文本!

You can set up a class to the date field in order to hook into that with jquery. 您可以在date字段中设置一个类,以便通过jquery插入该类。

A short example: 一个简短的例子:

form: 形成:

$builder->add('date', 'date', array(
    'widget' => 'single_text',
    'format' => 'dd-MM-yyyy',
    'attr'   =>  array(
            'class'   => 'your_class'),
));

javascript: JavaScript的:

$(function() {
    $( ".your_class" ).datepicker();
});

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

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