Last active
August 29, 2015 13:57
-
-
Save gravitano/9831442 to your computer and use it in GitHub Desktop.
Kelas sederhana untuk mempermudah proses validation saat pengimputan data atau update data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php namespace Smile\Services\Creators; | |
| interface CreatorInterface | |
| { | |
| public function create($input); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Penggunaan :
Simple/Servicesdidalam folderapp.Seperti ini:
Simple\Services\Creators\Creator.Dan jangan lupa untuk meletakannya di folder
app/Smile/Services/Creatorsjuga.Contohnya seperti dibawah ini.