Last active
July 24, 2018 03:20
-
-
Save Ellrion/7a53bc7bfea03a8c579d to your computer and use it in GitHub Desktop.
Laravel FromRequest with methods: prepare (for change and replace input) and sometimes (for add sometimes rules to validator)
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 as BaseFormRequest; | |
abstract class FormRequest extends BaseFormRequest | |
{ | |
protected $prepared; | |
/** | |
* Проверяет является ли метод методом создания по REST. | |
* | |
* @return bool | |
*/ | |
public function isMethodCreation() | |
{ | |
return $this->isMethod('post'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function prepareForValidation() | |
{ | |
if ($this->prepared) { | |
return; | |
} | |
$attributes = $this->input(); | |
if (method_exists($this, 'prepare')) { | |
$attributes = $this->prepare($attributes); | |
} | |
$this->prepared = true; | |
$this->replace($attributes); | |
parent::prepareForValidation(); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
protected function getValidatorInstance() | |
{ | |
$validator = parent::getValidatorInstance(); | |
if (method_exists($this, 'sometimes')) { | |
$this->sometimes($validator); | |
} | |
return $validator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment