简体   繁体   中英

Laravel 5.2 : Validation issue

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM