Last active
April 18, 2016 05:02
-
-
Save vocolboy/3aa1a63465d559fcf0c1018c7638e2c7 to your computer and use it in GitHub Desktop.
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
class MeetController extends Controller | |
{ | |
public function store() | |
{ | |
$v = Validator::make(request()->all(), Meets::rules()); | |
if($v->fails()) { | |
#return error | |
} | |
#continue | |
} | |
} | |
------------- | |
class Meets | |
{ | |
public static function rules() | |
{ | |
return [ | |
'date_type' => 'required|numeric|in:1,2,3', | |
'meal_times' => (request('date_type') == 1) ? 'required|numeric|in:1,2,3,4,5' : '', | |
]; | |
} | |
} |
try this:
class MeetController extends Controller
{
public function store(MeetRequest $request)
{
#do something.
}
}
Your validation must to keep in request your application service before.
so, you do request follow:
class MeetRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$meal_times = ($this->request->get('date_type') == 1)? 'required|numeric|in:1,2,3,4,5' : '';
return [
'date_type' => 'required|numeric|in:1,2,3',
'meal_times' => $meal_times,
];
}
//Also, you can replace method response to custom your response if you need.
public function response(array $errors)
{
//do something
}
}
@Mombuyish thx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Api 驗證設計這樣好嗎?