Setelah sebelumnya saya sudah menulis tentang membuat CRUD API di Symfony, kali ini saya lanjutkan untuk menulis tentang membuat validasi di symfony. Tulisan ini adalah lanjutan dari tulisan saya sebelum sebelumnya tentang symfony 4. Untuk membaca tulisan saya sebelumnya tentang CRUD di Symfony, anda bisa buka di https://catatan-pemrograman.blogspot.com/2018/09/membuat-api-crud-di-symfony-4.html.
Pada tulsian saya sebelumnya, saat membuat fungsi untuk melakukan insert data maupun edit data, saya membuat array untuk melakukan validasi terhadap inputan yang masuk.
<?php // src/Controller/ApiController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\MCategory;
use Symfony\Component\HttpFoundation\Request;
class ApiController extends AbstractController
{
/**
* @Route("/api", name="api")
*/
public function index()
{
....
}
/**
* @Route("/api/insert", name="api_category_insert", methods={"GET","POST"})
*/
public function insert(Request $request)
{
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true],
['name' => 'name', 'type' => 'text', 'required' => true],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
foreach($datas['fields'] as $field) {
if($field['required']) {
if(!isset($data[$field['name']])) {
$result = ['status' => false, 'messages' => 'format tidak valid ('.$field['name'].' is required)'];
return $this->json($result);
}else{
if(empty($data[$field['name']])) {
$result = ['status' => false, 'messages' => $field['name'].' is required.'];
return $this->json($result);
}
}
}
}
......
}
return $this->json($datas);
}
/**
* @Route("/api/update/{id}", name="api_category_update", methods={"GET","POST"})
*/
public function update($id = null, Request $request)
{
if(is_null($id)){
return $this->json(['status' => false, 'messages' => 'id tidak boleh kosong']);
}
$repository = $this->getDoctrine()->getRepository(MCategory::class);
$MCategory = $repository->find($id);
if(!$MCategory) {
return $this->json(['status' => false, 'messages' => 'object tidak ditemukan.']);
}
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true, 'value' => $MCategory->getCode()],
['name' => 'name', 'type' => 'text', 'required' => true, 'value' => $MCategory->getName()],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
foreach($datas['fields'] as $field) {
if($field['required']) {
if(isset($data[$field['name']])) {
if(empty($data[$field['name']])) {
$result = ['status' => false, 'messages' => $field['name'].' is required.'];
return $this->json($result);
}
}
}
}
.....
}
return $this->json($datas);
}
/**
* @Route("/api/delete/{id}", name="api_category_delete", methods={"DELETE"})
*/
public function deleted($id = null)
{
....
}
}
pada kode di atas, saya membuat array untuk melakukan validasi. Di Symfony, ada cara lain untuk melakukan validasi terhadap data yang di inputkan ke model dan yang selanjutnya disimpan ke tabel di database.
Membuat Validasi
untuk membuat validasi di symfony 4, kita perlu menambahkan dependency symfony/validator dan doctrine/annotations :
composer require symfony/validator doctrine/annotations
jalankan perintah di atas di command line di folder project anda. Setelah selesai instalasi, selanjutnya kita buka entity MCategory di src/Entity/MCategory.php.
<?php // src/Entity/MCategory.php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\MCategoryRepository")
*/
class MCategory
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\MNews", mappedBy="category")
*/
private $mNews;
public function __construct()
{
$this->mNews = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|MNews[]
*/
public function getMNews(): Collection
{
return $this->mNews;
}
public function addMNews(MNews $mNews): self
{
if (!$this->mNews->contains($mNews)) {
$this->mNews[] = $mNews;
$mNews->setCategory($this);
}
return $this;
}
public function removeMNews(MNews $mNews): self
{
if ($this->mNews->contains($mNews)) {
$this->mNews->removeElement($mNews);
// set the owning side to null (unless already changed)
if ($mNews->getCategory() === $this) {
$mNews->setCategory(null);
}
}
return $this;
}
}
entity MCategory di atas mempunyai attribute id, code, dan name. Kemudian Sebagai contoh, attribute code dan name saya buat required atau wajib diisi. Nah untuk mengimplementasikannya, tambahkan Constraint di Entity MCategory seperti berikut:
setelah itu, tambahkan juga di masing masing attribute seperti berikut:
sehingga keseluruhan entity menjadi seperti ini:
setelah itu ubah Controller ApiController dan tambahkan beberapa kode berikut: - tambahkan kode berikut di header controller :
- ubah function insert() dan update menjadi seperti berikut :
sehingga keseluruhan controller menjadi seperti berikut :
kemudian apabila saya coba POST data dengan nilai kosong /null akan muncul pesan seperti berikut:
bagaimana ? mudah bukan ? untuk merubah pesan error yang muncul cukup ubah Entity dibagian @Assert menjadi seperti berikut:
untuk tipe Validasi lainnya, bisa dibaca pada link berikut : Validation Constraints Reference
Cukup sekian tulisan saya kali ini, semoga bermanfaat. Untuk tulisan saya selanjutnya tentang symfony (REST API dengan FOS REST Bundle) bisa di baca di https://catatan-pemrograman.blogspot.com/2018/10/membuat-api-crud-di-symfony-4.html
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank() // tambahkan kode berikut
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank() // tambahkan kode berikut
*/
private $name;
<?php // src/Entity/MCategory.php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\MCategoryRepository")
*/
class MCategory
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank() // tambahkan kode berikut
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank() // tambahkan kode berikut
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\MNews", mappedBy="category")
*/
private $mNews;
public function __construct()
{
$this->mNews = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|MNews[]
*/
public function getMNews(): Collection
{
return $this->mNews;
}
public function addMNews(MNews $mNews): self
{
if (!$this->mNews->contains($mNews)) {
$this->mNews[] = $mNews;
$mNews->setCategory($this);
}
return $this;
}
public function removeMNews(MNews $mNews): self
{
if ($this->mNews->contains($mNews)) {
$this->mNews->removeElement($mNews);
// set the owning side to null (unless already changed)
if ($mNews->getCategory() === $this) {
$mNews->setCategory(null);
}
}
return $this;
}
}
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @Route("/api/insert", name="api_category_insert", methods={"GET","POST"})
*/
public function insert(Request $request, ValidatorInterface $validator)
{
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true],
['name' => 'name', 'type' => 'text', 'required' => true],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
// kita definisikan entity manager
$entityManager = $this->getDoctrine()->getManager();
// membuat object category dan set value
$MCategory = new MCategory();
$MCategory->setCode($data['code']);
$MCategory->setName($data['name']);
// validate
$errors = $validator->validate($MCategory); // melakukan validasi terhadap data yang di inputkan ke entity
if(count($errors)>0){ // jalankan jika ada error
$error = [];
foreach($errors as $k => $v){
$error[$v->getPropertyPath()] = $v->getMessage();
}
return $this->json(['errors' => $error]); // kembalikan data error
}
// menyimpan object ke database
$entityManager->persist($MCategory);
$entityManager->flush();
return $this->json(['status' => true, 'messages' => 'saved data successfully.', 'id' => $MCategory->getId()]);
}
return $this->json($datas);
}
/**
* @Route("/api/update/{id}", name="api_category_update", methods={"GET","POST"})
*/
public function update($id = null, Request $request)
{
if(is_null($id)){
return $this->json(['status' => false, 'messages' => 'id tidak boleh kosong']);
}
$repository = $this->getDoctrine()->getRepository(MCategory::class);
$MCategory = $repository->find($id);
if(!$MCategory) {
return $this->json(['status' => false, 'messages' => 'object tidak ditemukan.']);
}
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true, 'value' => $MCategory->getCode()],
['name' => 'name', 'type' => 'text', 'required' => true, 'value' => $MCategory->getName()],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
// kita definisikan entity manager
$entityManager = $this->getDoctrine()->getManager();
// membuat object category dan set value
if(isset($data['code'])){
$MCategory->setCode($data['code']);
}
if(isset($data['name'])) {
$MCategory->setName($data['name']);
}
// validate
$errors = $validator->validate($MCategory); // melakukan validasi terhadap data yang di inputkan ke entity
if(count($errors)>0){ // jalankan jika ada error
$error = [];
foreach($errors as $k => $v){
$error[$v->getPropertyPath()] = $v->getMessage();
}
return $this->json(['errors' => $error]); // kembalikan data error
}
// menyimpan object ke database
$entityManager->persist($MCategory);
$entityManager->flush();
return $this->json(['status' => true, 'messages' => 'update data successfully.', 'id' => $MCategory->getId()]);
}
return $this->json($datas);
}
<?php // src/Controller/ApiController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\MCategory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ApiController extends AbstractController
{
/**
* @Route("/api", name="api")
*/
public function index()
{
$repository = $this->getDoctrine()->getRepository(MCategory::class);
$MCategorys = $repository->findAll();
$datas = [];
foreach($MCategorys as $MCategory) {
$datas[] = [
'id' => $MCategory->getId(),
'code' => $MCategory->getCode(),
'name' => $MCategory->getName()
];
}
return $this->json($datas);
}
/**
* @Route("/api/insert", name="api_category_insert", methods={"GET","POST"})
*/
public function insert(Request $request, ValidatorInterface $validator)
{
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true],
['name' => 'name', 'type' => 'text', 'required' => true],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
// kita definisikan entity manager
$entityManager = $this->getDoctrine()->getManager();
// membuat object category dan set value
$MCategory = new MCategory();
$MCategory->setCode($data['code']);
$MCategory->setName($data['name']);
// validate
$errors = $validator->validate($MCategory); // melakukan validasi terhadap data yang di inputkan ke entity
if(count($errors)>0){ // jalankan jika ada error
$error = [];
foreach($errors as $k => $v){
$error[$v->getPropertyPath()] = $v->getMessage();
}
return $this->json(['errors' => $error]); // kembalikan data error
}
// menyimpan object ke database
$entityManager->persist($MCategory);
$entityManager->flush();
return $this->json(['status' => true, 'messages' => 'saved data successfully.', 'id' => $MCategory->getId()]);
}
return $this->json($datas);
}
/**
* @Route("/api/update/{id}", name="api_category_update", methods={"GET","POST"})
*/
public function update($id = null, Request $request)
{
if(is_null($id)){
return $this->json(['status' => false, 'messages' => 'id tidak boleh kosong']);
}
$repository = $this->getDoctrine()->getRepository(MCategory::class);
$MCategory = $repository->find($id);
if(!$MCategory) {
return $this->json(['status' => false, 'messages' => 'object tidak ditemukan.']);
}
$datas = [
'name' => 'category',
'fields' => [
['name' => 'code', 'type' => 'text', 'required' => true, 'value' => $MCategory->getCode()],
['name' => 'name', 'type' => 'text', 'required' => true, 'value' => $MCategory->getName()],
]
];
if($request->getMethod() === Request::METHOD_POST) { // jalankan jika method POST
$data = $request->get($datas['name']);
// validasi data
if(empty($data)) {
$result = ['status' => false, 'messages' => 'format tidak valid.'];
return $this->json($result);
}
// kita definisikan entity manager
$entityManager = $this->getDoctrine()->getManager();
// membuat object category dan set value
if(isset($data['code'])){
$MCategory->setCode($data['code']);
}
if(isset($data['name'])) {
$MCategory->setName($data['name']);
}
// validate
$errors = $validator->validate($MCategory); // melakukan validasi terhadap data yang di inputkan ke entity
if(count($errors)>0){ // jalankan jika ada error
$error = [];
foreach($errors as $k => $v){
$error[$v->getPropertyPath()] = $v->getMessage();
}
return $this->json(['errors' => $error]); // kembalikan data error
}
// menyimpan object ke database
$entityManager->persist($MCategory);
$entityManager->flush();
return $this->json(['status' => true, 'messages' => 'update data successfully.', 'id' => $MCategory->getId()]);
}
return $this->json($datas);
}
/**
* @Route("/api/delete/{id}", name="api_category_delete", methods={"DELETE"})
*/
public function deleted($id = null)
{
if(is_null($id)){
return $this->json(['status' => false, 'messages' => 'id tidak boleh kosong']);
}
$repository = $this->getDoctrine()->getRepository(MCategory::class);
$MCategory = $repository->find($id);
if(!$MCategory) {
return $this->json(['status' => false, 'messages' => 'object tidak ditemukan.']);
}
// menghapus data
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($MCategory);
$entityManager->flush();
return $this->json(['status' => true, 'messages' => 'data berhasil dihapus']);
}
}
bagaimana ? mudah bukan ? untuk merubah pesan error yang muncul cukup ubah Entity dibagian @Assert menjadi seperti berikut:
@Assert\NotBlank(message="wajib diisi")
Cukup sekian tulisan saya kali ini, semoga bermanfaat. Untuk tulisan saya selanjutnya tentang symfony (REST API dengan FOS REST Bundle) bisa di baca di https://catatan-pemrograman.blogspot.com/2018/10/membuat-api-crud-di-symfony-4.html
0 comments:
Post a Comment