自定义错误symfony [英] Customize errors symfony

查看:85
本文介绍了自定义错误symfony的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Symfony中有一些最佳实践"可以自定义表格错误? 例如,如果我要在需要该字段时显示"Campo obligatorio".

There are some "best practice" in Symfony to customize form errors? For exemple, if i would to show "Campo obligatorio" when the field is required.

1)我该如何做得更好,并且独立于什么形式? 2)如何自定义消息具有相同%namefield"的对象已存在". ?

1)How can i do that better way and independent from what forms call it? 2)How can i customize message 'An object with the same "%namefield" already exist.' ?

谢谢

已更新

对不起,但是如果我尝试无效"您怎么说的话...它会向我显示相同的错误

sorry, but if i try to do 'invalid' how you said me... it print me the same error

$this->setValidator('urlres', new sfValidatorString(array(
                          'min_length' => 6,
                        ), array(
                          'min_length' => 'URL must be longer',
                          'required'   => 'Required field',
                          'invalid' => 'URL exist'
                        )));

将我打印出来: *具有相同"urlres"的对象已经存在.

prints me: * An object with the same "urlres" already exist.

已更新 Felix,您的解决方案很棒,但它向我显示此错误: "urlres:该url已经存在"

updated Felix, your solution is fantastic but it prints me this error: "urlres: that url already exists"

有什么方法可以删除字段:"吗?

Are there some way to delete "field:" ??

谢谢

推荐答案

也许此表单帖子可以帮助您:

输入代码

sfValidatorBase::setDefaultMessage('required', 'Field required');

在应用程序配置apps/youApp/config/yourAppConfiguration.class.php的配置"中.

in the "configure" of you application configuration apps/youApp/config/yourAppConfiguration.class.php.

您应该可以通过这种方式为每种错误消息类型设置默认值.

You should be able to set the default value for every error message type this way.

如果要为某些字段设置某些错误消息,请考虑创建一个定义所有这些内容的表单类,并让所有其他表单都从该表单类继承.
然后,子类仅指定应显示的字段(可能还包括自定义验证逻辑).

If you want to set certain error messages for certain fields, think about to create a form class that defines all this and let all other forms inherit from this one.
The subclasses then only specify which fields should be displayed (and maybe custom validation logic).

您可以在中找到如何执行此操作的示例. symfony图书的管理员生成器"一章.

这是最干净的方法,恕我直言.

This is the cleanest approach IMHO.

如果要将字段留空,则必须添加required => false选项:

If you want leave fields blank, you have to add the required => false option:

  'email'   => new sfValidatorEmail(array('required' => false))

关于错误消息:听起来urlres在数据库表中被标记为唯一,并且该值已经存在.也许您应该检查数据库架构定义.

Regarding the error message: This sounds like the urlres is marked as unique in the database table and the value already exists. Maybe you should check the database schema definition.

要同时测试长度和唯一性,请使用 sfValidatorAnd sfValidatorDoctrineUnique:

To test both, length and uniqueness, you should use sfValidatorAnd and sfValidatorDoctrineUnique:

$this->setValidator('urlres', new sfValidatorAnd(
                    array(
                      new sfValidatorString(
                            array( 'min_length' => 6, ), 
                            array( 'required' => 'Required field',
                                   'min_length' => 'URL must be at least %min_length% chars long.' )
                      ),
                      new sfValidatorDoctrineUnique(
                            array( 'model' => 'yourModel', 
                                   'column' => 'theColumn', 
                                   'primary_key' => 'thePrimaryKeyColumn',
                                   'throw_global_error' => false),
                            array('invalid' => "That URL already exists")
                      )
                    ));

您在字符串验证器中使用invalid错误代码也不正确.您将无效消息设置为 URL exists ,但是字符串验证者怎么知道这一点?它只会检查给定的字符串是否符合min_lengthmax_length条件.

Also your use of the invalid error code in the string validator is not correct. You set the invalid message to URL exists but how can a string validator know this? It only checks whether the given string meets the min_length, max_length criteria or not.

顺便说一句,我假设您使用Doctrine,但我认为Propel可以使用相同的验证器.

Btw I assumed that you use Doctrine but I think the same validators are available for Propel.


设置选项'throw_global_error' => false.但我不确定是否可行.

Edit 3:
Set the option 'throw_global_error' => false. But I am not sure if that works.

您还可以查看源代码(如果有帮助的话).

You can also have a look at the source code if it helps you.

这篇关于自定义错误symfony的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆