Created
June 14, 2017 11:59
-
-
Save jayaregalinada/bb539db9caf078c40bb5e26c698dc684 to your computer and use it in GitHub Desktop.
One Request for User creation and editing
This file contains 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 App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
class UserRequest extends FormRequest | |
{ | |
protected $base_rules = [ | |
'name' => 'required|string|max:255', | |
'email' => 'required|string|email|max:255', | |
'username' => 'required|string|max:255', | |
'password' => 'string|min:6', | |
]; | |
protected $create_rules = [ | |
'name' => 'required|string|max:255', | |
'email' => 'required|string|email|max:255|unique:users', | |
'username' => 'required|string|max:255', | |
'password' => 'string|min:6', | |
]; | |
protected $add_role_rules = [ | |
'role' => 'required' | |
]; | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return ! auth()->guest(); | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
switch ($this->get('action')) { | |
case 'edit': | |
if ($this->has('password')) { | |
return $this->base_rules; | |
} | |
return collect($this->base_rules)->except('password')->toArray(); | |
break; | |
case 'create': | |
return $this->create_rules; | |
break; | |
case 'role': | |
return $this->add_role_rules; | |
break; | |
default: | |
return $this->base_rules; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment