Tuesday, September 29, 2020

Laravel request validation extend

 <?php namespace App\Http\Requests;


use Illuminate\Foundation\Http\FormRequest;

class PostRequest extends FormRequest
{

    public function authorize()
    {
    	// we can check more things 
        return true;
    }

    public function rules()
    {
		switch ($this->method()) {
		    case 'GET':
		    case 'DELETE': {
		        return [
		        	'id'=>'required|exists:posts,id'
		        ];
		    }
		    case 'POST': {
		        return [
		    		'title'=> 'required|min:10',
		    		'description' => 'required'
		        ];
		    }
		    case 'PUT':
		    case 'PATCH': {
		        return [
		        	'id'=>'required|exists:posts,id'
		    		'title'=> 'required|min:10',
		    		'description' => 'required'
		        ];
		    }
		    default:
		        break;
		}
    }

}

No comments:

Post a Comment