欢迎各位兄弟 发布技术文章
这里的技术是共享的
Because of you are using the FormRequest in your Controller method, you can use the $requets->all()
, $request->only('yourInput')
, ... to manage it.
Try it:
$request->replace(array('inputname' => 'new value'));
//or
$request->merge(array('inputname' => 'new value'));
Here, in the official repository you can find in the Request class the replace
method:
/**
* Replace the input for the current request.
*
* @param array $input
* @return void
*/
public function replace(array $input)
{
$this->getInputSource()->replace($input);
}
/**
* Merge new input into the current request's input array.
*
* @param array $input
* @return void
*/
public function merge(array $input)
{
$this->getInputSource()->add($input);
}
https://github.com/laravel/framework/blob/master/src/Illuminate/Http/Request.php
Hope it helps you.
I think you can override formatInput()
method in your FormRequest subclass to do just that.
Cool, I didn't know about that possibility. Inside of the FormRequest I added this function it and it works.
EDIT: THIS FUNCTION HAS BEEN REMOVED FROM THE LARAVEL 5.0 RELEASE VERSION
public function formatInput()
{
$input = array_map('trim', $this->all());
$input['first_name'] = ucwords($input['first_name']);
$input['last_name'] = ucwords($input['last_name']);
$this->replace($input);
return $this->all();
}
Very helpful, thanks!
It is a great improvement for the FormRequest :)
Thanks @Cocoon ! That's very cool. Just a much pretty way to do that :
public function treatment()
{
$input = (object) $this->all();
$input->firstname = ucfirst($input->firstname);
$input->lastname = ucfirst($input->lastname);
return (array) $input;
}
@sdebacker Yes, it has been removed (it had been renamed to sanitize before).
It seems like Taylor tried to extend this feature, but finally removed it before releasing Laravel 5. Have a look at GitHub. Someone posted a solution there, but I am not sure if it will work.
https://github.com/laravel/framework/commit/924a7fcf21bbba4f4efc8e367f456cea5e4d25c1
Thanks @Cocoon, it works.
I've been using this on L5.0.1:
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
/**
* Validate the input.
*
* @param \Illuminate\Validation\Factory $factory
* @return \Illuminate\Validation\Validator
*/
public function validator($factory)
{
return $factory->make(
$this->sanitizeInput(), $this->container->call([$this, 'rules']), $this->messages()
);
}
/**
* Sanitize the input.
*
* @return array
*/
protected function sanitizeInput()
{
if (method_exists($this, 'sanitize'))
{
return $this->container->call([$this, 'sanitize']);
}
return $this->all();
}
}
// Individual Request Class
/**
* Sanitize input before validation.
*
* @return array
*/
public function sanitize()
{
$input = $this->all();
$input['phone'] = phoneToDigits($input['phone']);
$this->replace($input);
return $this->all();
}
Thanks for posting @jhauraw! This is just what I needed.
Is there a way to do this globally? I would like to trim all input globally.
@pmall Do you have an example of what the middleware might look like?
来自 https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation