Skip to content

Instantly share code, notes, and snippets.

@Ellrion
Last active July 24, 2018 03:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Ellrion/7a53bc7bfea03a8c579d to your computer and use it in GitHub Desktop.
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)
<?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