0

我对 Symfony2 Forms + Entities + Validation 过程有一些疑问。以这段代码为例(/src/Common/CommonBundle/Resources/config/validation.yml):

Common\CommonBundle\Entity\AddressExtraInfo:
    properties:
        town:
            - NotBlank: 
                message: "This value should not be blank"
            - Length:
                min: 3
                max: 50
                minMessage: "This value should be {{ limit }} or more"
                maxMessage: "This value should be {{ limit }} or less"
            - Regex:
                pattern: "/^[\w\sÑñÁÉÍÓÚáéíóú]+$/"
                match: false
                message: "This value should be of type {{ alfanumérico }}"

现在,

  1. 此验证适用于:FormType 和 Entity 还是其中之一?在第二种情况下是哪一种?
  2. 我在我的应用程序中使用 i18n,如果我使用来自/vendor/symfony/symfony/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf然后有“fr”的翻译,消息将是法语还是会以英语显示?

这就是实体(只是相关代码)的样子:

<?php

namespace Common\CommonBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
use Common\CommonBundle\Model\BaseEntityTrait;
use Common\CommonBundle\Model\IdentifiedAutogeneratedEntityTrait;

/**
 * @ORM\Entity
 * @ORM\Table(name="address_extra_info")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 */
class AddressExtraInfo
{

    use IdentifiedAutogeneratedEntityTrait;
    use BaseEntityTrait;

    /**
     * Municipio
     * 
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank(message="Este valor no debería estar vacío.")
     */
    protected $town;

    ....

    public function setTown($town)
    {
        $this->town = strip_tags($town);
    }

    public function getTown()
    {
        return $this->town;
    }

    ...

}

这个实体有一个额外的疑问:每个 set 方法中都需要 strip_tags 吗?或者 Doctrine 或 Symfony 会处理这个问题?

AddressExtraInfoType.php

<?php

namespace Common\CommonBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints as Assert;

class AddressExtraInfoType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'town', 'text', array(
                'required' => true,
                'attr' => array(
                    'class' => 'address-input wv-tooltip',
                    'style' => 'width:272px;',
                    'placeholder' => 'Municipio *',
                    'tt-placement' => 'right',
                    'validated' => 'validated',
                    'onkeypress' => 'return isAlphaNumeric(event)',
                    'ng-minlength' => '3',
                    'maxlength' => '50',
                    'ng-pattern' => '/^[\w\sÑñÁÉÍÓÚáéíóú]+$/',
                    'wv-err' => 'Este valor debería ser de tipo alfanumérico',
                    'wv-cur' => '',
                    'wv-req' => 'Este valor no debería estar vacío.',
                    'wv-min' => 'Este valor debería ser de 3 ó más'
            )));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Common\CommonBundle\Entity\AddressExtraInfo'
            )
        );
    }

    public function getName()
    {
        return 'address_extra_info';
    }

}
4

1 回答 1

0

1) 在 symfony 2 验证文档的最顶部:

“验证是 Web 应用程序中非常常见的任务。在表单中输入的数据需要进行验证。数据在写入数据库或传递到 Web 服务之前也需要进行验证。Symfony2 附带了一个验证器组件来完成这项任务简单而透明。”

http://symfony.com/doc/current/book/validation.html

2)取决于您是否正确设置了所有内容:http: //symfony.com/doc/current/book/translation.html#translating-constraint-messages

3) 有条形标签并没有什么坏处。Symfony 表单只会传回用户提供的信息,但用户可以输入标签,所以如果这是一个问题,请留下 strip_tags。

于 2014-05-07T15:44:35.820 回答