问题描述:

I have some doubts around Symfony2 Forms + Entities + Validation process. Take this code as example (/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 }}"

Now,

  1. This validation applies to: FormType and Entity or just one of them? In the second case which one?
  2. I'm using i18n on my application, if I use the translations from /vendor/symfony/symfony/src/Symfony/Component/Validator/Resources/translations/validators.es.xlf and had then "fr" the messages will be on French or will be showed in English?

This is how the entity (just the relevant code) looks like:

<?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;

}

...

}

One extra doubt in this Entity: it's necessary the strip_tags in each set method? Or Doctrine or Symfony take care of this?

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';

}

}

网友答案:

1) At the very top of the symfony 2 validation doc:

"Validation is a very common task in web applications. Data entered in forms needs to be validated. Data also needs to be validated before it is written into a database or passed to a web service. Symfony2 ships with a Validator component that makes this task easy and transparent."

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

2) Depends on if you set up everything correctly: http://symfony.com/doc/current/book/translation.html#translating-constraint-messages

3) It doesn't hurt to have the strip tags. Symfony forms will only pass back the information given by the user, but the user may enter tags, so if that's a concern leave the strip_tags in.

相关阅读:
Top