Validation Always Print Error Message - 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: Validation Always Print Error Message (/showthread.php?tid=73209) |
Validation Always Print Error Message - rmcdahal - 03-31-2019 Here is my Controller Code: public function pagecreate() Here is my Views Code: <div class="row"> And issue is Views always print validation error message withour submiting form ? The title field is required. The body field is required. Is there any problems with my code ? RE: Validation Always Print Error Message - MGatner - 03-31-2019 My guess is that it is because you are initiating Validation as a service in the view, so you are still getting the same instance (I.e. rules) but not the run result from the controller. Try passing $validation into the view directly and using that to display errors. RE: Validation Always Print Error Message - ci_user99 - 04-01-2019 I had the same problem and thought it was its normal way of working while it checks if validation has passed and if not, shows the form. "If not" meaning there are errors. To solve this I added in my controller function PHP Code: $data['isPost'] = $this->request->getMethod()=='post'; And then in the html where I show the errors Code: <?php if($isPost) echo service('validation')->listErrors() ?> That way only when the form is sent (if actually sent through post) will the errors show. If there is a better way, I'd be glad to hear it. RE: Validation Always Print Error Message - elephpantech - 04-03-2019 My approach would be: controller: PHP Code: public function pagecreate() views: PHP Code: //Backend/Themes/Base PHP Code: // Backend/Pages/Create RE: Validation Always Print Error Message - MGatner - 04-03-2019 I think how @elephpantech (awesome handle) does it is in line with "intended" use (whatever that is worth). You can also check out @kilishan's implementation in Myth:Auth, with the caveat that some of that code is based on an older version of CI4: https://github.com/lonnieezell/myth-auth/blob/develop/src/Controllers/AuthController.php#L62 |