![]() |
Form Validation Allowing Empty Strings To Be Stored Instead of Null - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Form Validation Allowing Empty Strings To Be Stored Instead of Null (/showthread.php?tid=74554) |
Form Validation Allowing Empty Strings To Be Stored Instead of Null - viewfromthenorth - 10-07-2019 I'm trying to get form validation to work at the model level. I have three fields, id, name and email, and I have them all set to required. I'm using the CI 4 Entity class, so on an incoming request, I get the JSON, fill my new Entity on creation, and then pass to the model. No matter what I do, for the fields name and email (set to varchar 150 and not NULL in SQL), when I try to insert, name and email are stored as empty strings when I don't include them in my JSON (for instance, if I pass id alone). How can I make it so that if the name and email keys are not passed, the required error is shown? Model: PHP Code: <?php Controller: PHP Code: public function create() RE: Form Validation Allowing Empty Strings To Be Stored Instead of Null - kilishan - 10-07-2019 You're missing an end quote on this line: PHP Code: 'email' => 'required|valid_email|is_unique[users.email] Not sure if that's doing it for you not, but wouldn't help anything. RE: Form Validation Allowing Empty Strings To Be Stored Instead of Null - viewfromthenorth - 10-07-2019 Sorry, I think I deleted them accidentally when editing my post. Here's the rules and messages again. Still having the problem. If the JSON doesn't contain name and email, I want it to throw a required error. PHP Code: protected $validationRules = [ |