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(),
];
}
}
You should do it in
formatInput
method. Data you return in this method will be used for validator to check:For example:
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:
EDIT2
I put here the whole working
TestRequest
class. I send empty data and because of modifiedall()
method even if empty data, validation passes because I manually set those data to validate. If I remove thisall()
method and send empty data of course I will get errors displayed