Skip to content

Instantly share code, notes, and snippets.

@vocolboy
Last active April 18, 2016 05:02
Show Gist options
  • Save vocolboy/3aa1a63465d559fcf0c1018c7638e2c7 to your computer and use it in GitHub Desktop.
Save vocolboy/3aa1a63465d559fcf0c1018c7638e2c7 to your computer and use it in GitHub Desktop.
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' : '',
];
}
}
@vocolboy
Copy link
Author

Api 驗證設計這樣好嗎?

@Mombuyish
Copy link

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
   }
}

@vocolboy
Copy link
Author

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