'Why required rule not working in CodeIgniter
When I add required rule the validation pass even in the post data not contain (name), but if I have other rules like (valid_email), the validation return false.
My Model:
namespace App\Models;
use CodeIgniter\Model;
class ProjectsMD extends Model
{
protected $table = 'projects';
protected $primaryKey = 'sid';
protected $returnType = 'App\Entities\Project';
protected $allowedFields = ['name', 'description', 'location', 'area', 'client', 'stage'];
protected $validationRules = [
'name' => ['label' => 'App.name', 'rules' => 'required'],
];
protected $useTimestamps = true;
}
My Controller:
public function projectsCreate()
{
$projectsMD = new \App\Models\ProjectsMD();
if($projectsMD->validate($this->request->getPost()) === false)
{
return false;
}
return true;
}
Solution 1:[1]
Have you inspected the request body $this->request->getPost() to confirm if the name value is not an empty array, empty string, null or false?
| Rule | Parameter | Description |
|---|---|---|
required |
No | Fails if the field is an empty array, empty string, null or false. |
Nonetheless, these validation rules should be sufficient.
required|string|min_length[3]|max_length[120]
You may add additional validation rules depending on your specific requirements.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | steven7mwesigwa |
