Laravel 5 Request - altering data

半腔热情 提交于 2020-01-23 07:06:28

问题


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:


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;
    }

}


来源:https://stackoverflow.com/questions/28600823/laravel-5-request-altering-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!