strmizo's avatar

Laravel 5 Form Requests have a sanitize method ?

Hello guys !! i just installed Laravel 5 after watching L5 series here in Laracast, after i dig in the form requests i found there is a new method called sanitize

    public function sanitize()
    {
        return $this->all();
    }

I want to know how it works ?

0 likes
6 replies
bart's avatar

What about trying it out yourself?

1 like
strmizo's avatar

i have just play with it, i think it sanitize the data that return from it, in the code above, it sanitize all input from the request ! but i can't see where it get invoked ?!

bestmomo's avatar

This method sanitize nothing but let you sanitize inputs if you want.

pmall's avatar

Yes you can modify your input here. The result of the sanitize method is used to make the validator instance.

As you can see by default the whole input is used.

kacyblack's avatar

It works like this: <?php namespace Phpleaks\Http\Requests;

use Phpleaks\Http\Requests\Request;

class LinkCreateFormRequest extends Request {

public function authorize()
{
    return true;
}

public function rules()
{

    $this->sanitize();

    return [
      'name' => 'required',
      'url' => 'required|url|unique:links,url',
      'category' => 'required|integer|min:1',
      'description' => 'required|max:300'
    ];
}

public function sanitize()
{
    $input = $this->all();

    if (preg_match("#https?://#", $input['url']) === 0) {
        $input['url'] = 'http://'.$input['url'];
    }

    $input['name'] = filter_var($input['name'], FILTER_SANITIZE_STRING);
    $input['description'] = filter_var($input['description'], 
    FILTER_SANITIZE_STRING);

    $this->replace($input);     
}

}

Please or to participate in this conversation.