Example
- Create an Entity in
AppBundle/Entity
directory. You can do this manually, or by using Symfony's command php bin/console doctrine:generate:entity
and filling in the required information in each step. You must specify yml
option at Configuration format (yml, xml, php or annotation)
step.
<?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;
}
}
- Create the Entity mapping information for the Entity class. If you are using Symfony's command
php bin/console doctrine:generate:entity
, then the following code will be auto-generated. Otherwise, if you don't use the command, you can create the following code by hand.
# 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: { }
- Create the validation for the Entity class.
# 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"
- Create a new form in
AppBundle/Form
directory.
<?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';
}
}
- Create a new route in
AppBundle/Resources/config/routing.yml
app_person:
path: /person
defaults: { _controller: AppBundle:Default:person }
- Now create a new action method for that route.
<?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()
]
);
}
}
- Create the view in
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 %}
- Start Symfony's built-in server (
php bin/console server:run
) and access 127.0.0.1:8000/person
route in your browser. There should be a form consisting of two input boxes and a submit button. If you press the submit button without entering any data into the input boxes, then the error messages will be displayed.