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.
jalankan perintah di atas di command line di folder project anda. Setelah selesai instalasi, selanjutnya kita buka entity MCategory di src/Entity/MCategory.php.
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:
use Symfony\Component\Validator\Constraints as Assert;
setelah itu, tambahkan juga di masing masing attribute seperti berikut:
/**
* @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;
sehingga keseluruhan entity menjadi seperti ini:
<?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;
}
}
setelah itu ubah Controller ApiController dan tambahkan beberapa kode berikut: - tambahkan kode berikut di header controller :
use Symfony\Component\Validator\Validator\ValidatorInterface;
- ubah function insert() dan update menjadi seperti berikut :
/**
* @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);
}
sehingga keseluruhan controller menjadi seperti berikut :
<?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']);
}
}
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:
@Assert\NotBlank(message="wajib diisi")
untuk tipe Validasi lainnya, bisa dibaca pada link berikut :
Validation Constraints ReferenceCukup 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