Reputation: 5628
In My controller :
$this->validate($request, [
'name' => 'required',
]);
$tasks = new Task;
$tasks->name = $request->todo;
if($tasks->save()){
$tasks->save();
return back();
}
The field is filled and the issue is that the validator still throws error:
The name field is required
.
Am i missing something.
Upvotes: 1
Views: 60
Reputation: 1769
You need to change 'name' => 'required'
to 'todo' => 'required'
, because here you need to specify the name which wrote in the FORM FIELD
If you have used <input type="text" name"todo" />
then in while defining the RULES in controller you need use "todo" just as follows:
$this->validate($request, [
'todo' => 'required',
]);
Hope this helps you.
Upvotes: 0
Reputation: 3961
The validator validates REQUEST input, not your model. If you have a field in your request called todo
and you want to make it required, you would do:
$this->validate($request, [
'todo' => 'required', // this is the name of the field from your form as it comes through to the Request object
]);
$task = new Task();
$task->name = $request->todo;
if($task->save()) { // note, you don't need to call save() twice
return back();
}
// you probably want to do something here in case save fails?
Upvotes: 1