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