欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

[Laravel 5] Modify input before validation 修改 改变 request 参数 replace 有大用

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();
}
xingfucoder

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.

ratiw
ratiw
3 years ago(45,505 XP)

I think you can override formatInput() method in your FormRequest subclass to do just that.

Cocoon
 Cocoon
3 years ago(22,875 XP)

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();
}
trevorg

Very helpful, thanks!

martindilling

Thanks a lot @ratiw and @Cocoon, that's gonna be very helpful :p

pmall
 pmall
3 years ago(582,695 XP)

It is a great improvement for the FormRequest :)

fdusautoir

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

@ratiw @Cocoon : Does formatInput() method still works ? I can’t find it in FormRequest class.

Cocoon
 Cocoon
3 years ago(22,875 XP)

@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

sdebacker

Thanks @Cocoon, it works.

jhauraw

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();
}
jesseschutt

Thanks for posting @jhauraw! This is just what I needed.

Swaz
Swaz
3 years ago(44,945 XP)

Is there a way to do this globally? I would like to trim all input globally.

pmall
 pmall
3 years ago(582,695 XP)

@Swaz

Is there a way to do this globally? I would like to trim all input globally.

Use a middleware

Swaz
Swaz
3 years ago(44,945 XP)

@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

普通分类: