Skip to content

Instantly share code, notes, and snippets.

@gravitano
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save gravitano/9831442 to your computer and use it in GitHub Desktop.

Select an option

Save gravitano/9831442 to your computer and use it in GitHub Desktop.
Kelas sederhana untuk mempermudah proses validation saat pengimputan data atau update data.
<?php namespace Smile\Services\Creators;
use Illuminate\Validation\Factory as Validator;
abstract class Creator implements CreatorInterface
{
protected $model;
protected $errors;
protected $rules = [];
protected $messages = [];
protected $input = [];
public function __construct(Validator $validator) {
$this->model = new $this->model; // atau bisa gunakan App::make($this->model)
$this->validator = $validator;
}
abstract public function create($input);
public function validate($input, $rules = array(), $messages = array())
{
$validator = $this->validator->make($input, $rules, $messages);
if($validator->passes())
{
return true;
}
$this->errors = $validator->messages();
return false;
}
public function isValid($input = null)
{
$input = $input ?: $this->input;
return $this->validate($input, $this->rules, $this->messages);
}
public function getErrors()
{
return $this->errors;
}
}
<?php namespace Smile\Services\Creators;
use Exception;
class CreatorException extends Exception
{
protected $errors;
public function __construct($errors, $code = 0, Exception $previous = null)
{
$this->errors = $errors;
}
public function getErrors()
{
return $this->errors;
}
}
<?php namespace Smile\Services\Creators;
interface CreatorInterface
{
public function create($input);
}
@gravitano
Copy link
Author

Penggunaan :

  1. Pastikan Anda sudah membuat folder Simple/Services didalam folder app.
    Seperti ini:
- laravel/
  |-- app/
      |-- Smile
          |-- Services
              |-- Creators
                  |-- Creator.php
                  |-- CreatorException.php
                  |-- CreatorInterface.php

  1. Buat class yang meng-extend ke class Simple\Services\Creators\Creator.
    Dan jangan lupa untuk meletakannya di folder app/Smile/Services/Creators juga.
    Contohnya seperti dibawah ini.
<?php namespace Smile\Services\Creators;

use Smile\Services\Creators\CreatorException;

class UserCreator extends Creator
{
    protected $model = 'User';

    protected $rules = [
        'username'      => 'required|max:15',
        'email'         => 'required|email',
        'password'      => 'required',
    ];

    public function create($input)
    {
        if($this->isValid($input))
        {
            $this->model->create($input);
            return true;
        }
        throw new CreatorException($this->getErrors());     
    }
}
  1. Nanti di controller nya gini:
<?php

use Smile\Services\Creators\UserCreator as Creator;

class UsersController extends BaseController
{
    protected $creator;

    public function __construct(Creator $creator) {
        $this->creator = $creator;
    }

    public function store()
    {
        try
        {
            $this->creator->create(Input::all());
            return Redirect::route('users.index')
                ->withFlashSuccess('Pengguna baru berhasil dibuat.')
            ;
        }
        catch(CreatorException $e)
        {
            return Redirect::back()
                ->withInput()
                ->withErrors($e->getErrors())
                ->withFlashError('Whoops, ada yang error.');
            ;
        }
    }
}
  1. Selesai

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment