-
StackOverflow 文件
-
symfony3 教程
-
驗證
-
使用 YAML 進行 Symfony 驗證
- 在
AppBundle/Entity
目錄中建立一個實體。你可以手動執行此操作,也可以使用 Symfony 的命令 php bin/console doctrine:generate:entity
並在每個步驟中填寫所需資訊。你必須在 Configuration format (yml, xml, php or annotation)
步驟指定 yml
選項。
<?php
# AppBundle/Entity/Person.php
namespace AppBundle\Entity;
/**
* Person
*/
class Person
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $age;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Person
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set age
*
* @param integer $age
*
* @return Person
*/
public function setAge($age)
{
$this->age = $age;
return $this;
}
/**
* Get age
*
* @return int
*/
public function getAge()
{
return $this->age;
}
}
- 為 Entity 類建立實體對映資訊。如果你使用 Symfony 的命令
php bin/console doctrine:generate:entity
,則會自動生成以下程式碼。否則,如果不使用該命令,則可以手動建立以下程式碼。
# AppBundle/Resources/config/doctrine/Person.orm.yml
AppBundle\Entity\Person:
type: entity
table: persons
repositoryClass: AppBundle\Repository\PersonRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: '50'
age:
type: integer
lifecycleCallbacks: { }
# AppBundle/Resources/config/validation/person.yml
AppBundle\Entity\Person:
properties:
name:
- NotBlank:
message: "Name is required"
- Length:
min: 3
max: 50
minMessage: "Please use at least 3 chars"
maxMessage: "Please use max 50 chars"
- Regex:
pattern: "/^[A-Za-z]+$/"
message: "Please use only letters"
age:
- NotBlank:
message: "Age is required"
- Length:
min: 1
max: 3
minMessage: "The age must have at least 1 number in length"
maxMessage: "The age must have max 3 numbers in length"
- Regex:
pattern: "/^[0-9]+$/"
message: "Please use only numbers"
- 在
AppBundle/Form
目錄中建立一個新表單。
<?php
# AppBundle/Form/PersonType.php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
class PersonType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, ['label'=>'Name'])
->add('age', IntegerType::class, ['label'=>'Age'])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Person'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
// TODO: Implement setDefaultOptions() method.
}
public function getName()
{
return 'person_form';
}
}
- 在
AppBundle/Resources/config/routing.yml
中建立一條新路線
app_person:
path: /person
defaults: { _controller: AppBundle:Default:person }
<?php
# AppBundle/Controller/DefaultController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Person;
use AppBundle\Form\PersonType;
class DefaultController extends Controller
{
public function personAction(Request $request)
{
$person = new Person();
$form = $this->createForm(
PersonType::class,
$person,
[
'action' => $this->generateUrl('app_person'),
'method'=>'POST',
'attr'=>[
'id'=>'form_person',
'class'=>'person_form'
]
]
);
$form->handleRequest($request);
return $this->render(
'AppBundle:Default:person.html.twig', [
'form'=>$form->createView()
]
);
}
}
- 在
AppBundle/Resources/views/Default/person.html.twig
中建立檢視
{% extends '::base.html.twig' %}
{% block body %}
{{ form_start(form, {'attr': {'novalidate':'novalidate'}}) }}
{{ form_row(form.name) }}
{{ form_row(form.age) }}
<button type="submit">Go</button>
{{ form_end(form) }}
{% endblock %}
- 啟動 Symfony 的內建伺服器(
php bin/console server:run
)並在瀏覽器中訪問 127.0.0.1:8000/person
路由。應該有一個由兩個輸入框和一個提交按鈕組成的表單。如果按下提交按鈕而未在輸入框中輸入任何資料,則將顯示錯誤訊息。