Skip to content

Instantly share code, notes, and snippets.

@ferdiunal
Last active October 15, 2016 14:19
Show Gist options
  • Save ferdiunal/9d74cc3eb60b9582fd80fa4e26274b28 to your computer and use it in GitHub Desktop.
Save ferdiunal/9d74cc3eb60b9582fd80fa4e26274b28 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
use App\Acme\Helpers\RequestHelper;
class EducationCreate extends FormRequest
{
use RequestHelper;
protected $casts = [
"status" => "boolean"
];
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
"title" => "required",
"content" => "required",
"category" => "required|integer",
"status" => "required|integer",
"images" => "required",
];
}
public function messages(){
return [
"title.required" => "Bu alan boş bırakılmaz",
"content.required" => "Bu alan boş bırakılmaz",
"category.required" => "Bu alan boş bırakılmaz",
"category.integer" => "Bu alan boş bırakılmaz",
"status.required" => "Bu alan boş bırakılmaz",
"images.required" => "Bu alan boş bırakılmaz",
];
}
}
<?php
namespace App\Acme\Helpers;
trait RequestHelper{
public function input($key = null, $default = null){
$input = $this->convertCast();
return data_get($input, $key, $default);
}
private function convertBoolean($value){
$acceptable = ['0', '1'];
$checked = in_array($value, $acceptable, true);
if($checked === true) $value = intval($value);
return (bool) $value;
}
private function convertCast(){
$input = $this->getInputSource()->all() + $this->query->all();
if(property_exists($this,'casts')){
foreach($this->casts as $key => $val){
if(array_key_exists($key,$input)){
switch($val){
case"bool" :
case"boolean" :
$input[$key] = $this->convertBoolean($input[$key]);
break;
}
}
}
return $input;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment