Sök…
Anmärkningar
Faktum är att validering av formulär baseras från en komponent, benämnd " Validator Component ".
Du kan ofta använda den dedikerade tjänsten om du inte behövde visa ett formulär i en mall. Som API: er. Du kan validera data på samma sätt, så här:
Till exempel baserat på symfony doc :
$validator = $this->get('validator');
$errors = $validator->validate($author);
if (count($errors) > 0) {
/*
* Uses a __toString method on the $errors variable which is a
* ConstraintViolationList object. This gives us a nice string
* for debugging.
*/
$errorsString = (string) $errors;
}
Symfony validering med anteckningar
- Aktivera validering med kommentarer i filen
app/config/config.yml
framework:
validation: { enable_annotations: true }
- Skapa en
AppBundle/Entity
iAppBundle/Entity
. Valideringarna görs med@Assert
kommentarer.
<?php
# AppBundle/Entity/Car.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Car
*
* @ORM\Table(name="cars")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CarRepository")
*/
class Car
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=50)
* @Assert\NotBlank(message="Please provide a name")
* @Assert\Length(
* min=3,
* max=50,
* minMessage="The name must be at least 3 characters long",
* maxMessage="The name cannot be longer than 50 characters"
* )
* @Assert\Regex(
* pattern="/^[A-Za-z]+$/",
* message="Only letters allowed"
* )
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="number", type="integer")
* @Assert\NotBlank(message="Please provide a number")
* @Assert\Length(
* min=1,
* max=3,
* minMessage="The number field must contain at least one number",
* maxMessage="The number field must contain maximum 3 numbers"
* )
* @Assert\Regex(
* pattern="/^[0-9]+$/",
* message="Only numbers allowed"
* )
*/
private $number;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Car
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set number
*
* @param integer $number
*
* @return Car
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* @return integer
*/
public function getNumber()
{
return $this->number;
}
}
- Skapa ett nytt formulär i
AppBundle/Form
katalogen.
<?php
# AppBundle/Form/CarType.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 CarType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, ['label'=>'Name'])
->add('number', IntegerType::class, ['label'=>'Number'])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Car'
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
// TODO: Implement setDefaultOptions() method.
}
public function getName()
{
return 'car_form';
}
}
- Skapa en ny rutt och en ny handlingsmetod i
AppBundle/Controller/DefaultController.php
. Rutten kommer också att deklareras med anteckningar, så se till att du har importerat den här rutten i huvudvägsfilen (app/config/routing.yml
).
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use AppBundle\Entity\Car;
use AppBundle\Form\CarType;
class DefaultController extends Controller
{
/**
* @Route("/car", name="app_car")
*/
public function carAction(Request $request)
{
$car = new Car();
$form = $this->createForm(
CarType::class,
$car,
[
'action' => $this->generateUrl('app_car'),
'method'=>'POST',
'attr'=>[
'id'=>'form_car',
'class'=>'car_form'
]
]
);
$form->handleRequest($request);
return $this->render(
'AppBundle:Default:car.html.twig',[
'form'=>$form->createView()
]
);
}
}
- Skapa vyn i
AppBundle/Resources/views/Default/car.html.twig
.
{% extends '::base.html.twig' %}
{% block body %}
{{ form_start(form, {'attr': {'novalidate':'novalidate'}}) }}
{{ form_row(form.name) }}
{{ form_row(form.number) }}
<button type="submit">Go</button>
{{ form_end(form) }}
{% endblock %}
- Starta Symfonys inbyggda server (
php bin/console server:run
) och få åtkomst till127.0.0.1:8000/car
i din webbläsare. Det bör finnas ett formulär som består av två inmatningsrutor och en skicka-knapp. Om du trycker på skicka-knappen utan att mata in data i inmatningsrutorna, visas felmeddelandena.
Symfony validering med YAML
- Skapa en
AppBundle/Entity
iAppBundle/Entity
. Du kan göra detta manuellt eller genom att använda Symfony's kommandophp bin/console doctrine:generate:entity
och fylla i den information som krävs i varje steg. Du måste ange alternativetyml
Configuration format (yml, xml, php or annotation)
.
<?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;
}
}
- Skapa information om kartläggning av enheter för klassen Enhet. Om du använder Symfony's kommando
php bin/console doctrine:generate:entity
, kommer följande kod att genereras automatiskt. Annars, om du inte använder kommandot, kan du skapa följande kod för 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: { }
- Skapa valideringen för entitetsklassen.
# 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"
- Skapa ett nytt formulär i
AppBundle/Form
katalogen.
<?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';
}
}
- Skapa en ny rutt i
AppBundle/Resources/config/routing.yml
app_person:
path: /person
defaults: { _controller: AppBundle:Default:person }
- Skapa nu en ny handlingsmetod för den rutten.
<?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()
]
);
}
}
- Skapa vyn i
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 %}
- Starta Symfonys inbyggda server (
php bin/console server:run
) och få åtkomst till127.0.0.1:8000/person
i din webbläsare. Det bör finnas ett formulär som består av två inmatningsrutor och en skicka-knapp. Om du trycker på skicka-knappen utan att mata in data i inmatningsrutorna, visas felmeddelandena.
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow