function like __construct function but for specific function.
my route
Route::resource('form00', 'Form00Controller');
Route::resource('form001', 'Form001Controller');
........ and more
my __contsruct in Form00Controller
public function __construct()
{
$this->validate(request(), [
'projectName' =>
array(
'required',
'regex:/(^([a-zA-Z]+)(\d+)?$)/u'
)
];
}
my another Form00Controller function
public function create()// this function and another not effected
{
return view('form00.create');
}
public function store(Request $request)//__construct just for this function
{
$requestData = $request->all();
Form00::create($requestData);
return redirect('form00')->with('flash_message', 'Form00 added!');
}
i need validate just for function store and not change that function.
This is the perfect example for Custom Form Requests you can follow this tutorial
Basically you'll end up with
public function store(StoreFormFormRequest $request)//__construct just for this function
{
$requestData = $request->all();
Form00::create($requestData);
return redirect('form00')->with('flash_message', 'Form00 added!');
}
And you'll have a StoreFormFormRequest
class into app/Http/Requests
which will be something like
class StoreFormFormRequest extends FormRequest {
public function rules() {
return [
'projectName' => 'required|regex:/(^([a-zA-Z]+)(\d+)?$)/u'
]
}
}
Validation will be triggered automatically and, if it passes, the controller's code will be executed.