Skip to content

Instantly share code, notes, and snippets.

@kostaspt
Last active October 30, 2015 17:56
Show Gist options
  • Save kostaspt/e6c0bcc94889591d1759 to your computer and use it in GitHub Desktop.
Save kostaspt/e6c0bcc94889591d1759 to your computer and use it in GitHub Desktop.
[Laravel 5.1] Form Request
<?php
namespace App\Http\Requests;
class ExampleRequest 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()
{
$this->setRules([
'first_name' => 'required',
'last_name' => 'required',
'email' => 'uniqueExceptMe',
'username' => 'required',
'phone' => 'optional'
]);
return $this->rules;
}
}
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest
{
/**
* The rules that are currently active.
*
* @var array
*/
protected $rules = [];
/**
* All the available rules.
*
* @var array
*/
protected $allRules = [
'email' => ['email'],
'password' => ['min:6', 'confirmed'],
'password_confirmation' => ['min:6'],
'first_name' => ['between:2,30'],
'last_name' => ['between:2,30'],
'username' => ['max:15'],
'phone' => ['size:10']
];
/**
* Create a list of rules
*
* @param array $rulesToInclude
* @return array
*/
protected function setRules(array $rulesToInclude)
{
$rules = [];
foreach ($rulesToInclude as $rule => $type) {
if ($type == 'required') {
$this->setRuleAsRequired($rule);
} elseif (strpos($type, 'uniqueExceptMe') !== false) {
$this->setUniqueRuleExceptMe(
$this->getAttribute($type, 'uniqueExceptMe')
);
} elseif (strpos($type, 'unique') !== false) {
$this->setUniqueRule(
$this->getAttribute($type, 'unique')
);
} else {
$this->setRule($rule);
}
}
return $rules;
}
/**
* Set the rule as required
*
* @param $ruleName
*/
protected function setRuleAsRequired($ruleName)
{
$requiredRule = $this->allRules[$ruleName];
$requiredRule[] = 'required';
$this->setCustomRule($ruleName, $requiredRule);
}
/**
* Set the email to be unique except to my account
*/
protected function setUniqueRule($attribute)
{
$uniqueRule = $this->allRules[$attribute];
$uniqueRule[] = 'required';
$uniqueRule[] = 'unique:users';
$this->setCustomRule($attribute, $uniqueRule);
}
/**
* Set the email to be unique except to my account
*/
protected function setUniqueRuleExceptMe($attribute)
{
$uniqueRule = $this->allRules[$attribute];
$uniqueRule[] = 'required';
$uniqueRule[] = "unique:users,{$attribute}," . auth()->id();
$this->setCustomRule($attribute, $uniqueRule);
}
/**
* Set a custom rule.
*
* @param $ruleName
* @param $rule
*/
protected function setCustomRule($ruleName, $rule)
{
$this->rules[$ruleName] = $rule;
}
/**
* Set a rule.
*
* @param $rule
* @return mixed
*/
protected function setRule($rule)
{
$this->rules[$rule] = $this->allRules[$rule];
}
/**
* Get the attribute from type.
*
* @param $type
* @param $identifier
* @param string $default
* @return string
*/
protected function getAttribute($type, $identifier, $default = 'email')
{
$attribute = substr($type, strlen($identifier) + 1);
return strlen($attribute) > 0 ? $attribute : $default;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment