Laravel 5 Request - altering data

2019-04-29 15:49发布

I have come across an instance where I need to alter the data that needs to be validated i.e. when no slug has been submitted, create one from the title and then validate that it is unique.

The request has a method replace() which is supposed to replace the input data on the request but it doesn't appear to work. Can anyone shine a light on this? This is what I have:

<?php namespace Purposemedia\Pages\Http\Requests;

use Dashboard\Http\Requests\Request;
use Illuminate\Auth\Guard;

class PageRequest extends Request {

    /**
     * [authorize description]
     * @return {[type]} [description]
     */
    public function authorize( Guard $auth) {
        return true;
    }

    /**
     * [rules description]
     * @return {[type]} [description]
     */
    public function rules() {
        // Get all data from request
        $data = $this->request->all();
        if( $data['slug'] === '' ) {
            // if the slug is blank, create one from title data
            $data['slug'] = str_slug( $data['title'], '-' );
            // replace the request data with new data to validate
            $this->request->replace( $data );
        }
        return [

            'title' => 'required|min:5|max:255',
            'slug' => 'min:5|max:255|alpha_dash|unique:pages,slug' . $this->getSegmentFromEnd(),

        ];
    }

}

1条回答
Anthone
2楼-- · 2019-04-29 16:23

You should do it in formatInput method. Data you return in this method will be used for validator to check:

For example:

protected function formatInput()
{
    $data = $this->all();
    if( $data['slug'] === '') {
        // if the slug is blank, create one from title data
        $data['slug'] = str_slug( $data['title'], '-' );
    }

    return $data;
}

EDIT

This method was in Laravel 2 months ago (I'm still using this version), that's quite strange it was removed.

The same effect you should receive when you change above method into:

public function all()
{
    $data = parent::all();
    if( $data['slug'] === '') {
        // if the slug is blank, create one from title data
        $data['slug'] = str_slug( $data['title'], '-' );
    }

    return $data;
}

EDIT2

I put here the whole working TestRequest class. I send empty data and because of modified all() method even if empty data, validation passes because I manually set those data to validate. If I remove this all() method and send empty data of course I will get errors displayed

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class TestRequest 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()
    {
        return [
            //
            'title' => 'required|min:2|max:255',
            'slug' => 'min:2|max:255'
        ];
    }

    public function response(array $errors){
        dd($errors);
    }

    public function all(){
        $data = parent::all();
        $data['title'] = 'abcde';
        $data['slug'] = 'abcde';
        return $data;
    }

}
查看更多
登录 后发表回答