CodeIgniter Forums
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()
   {
       $validation = $this->validate([
           'title' => 'required',
           'body' => 'required'
       ]);
       if (!$validation) {
           $data['page_heading'] = 'Create New Page';
           $data['content_view'] = 'Backend/Pages/Create';
           echo view('Backend/Themes/Base', $data);
       } else {
           $this->model->save(
               [
                   'title' => $this->request->getvar('title'),
                   'content' => $this->request->getVar('body')
               ]
           );
           return redirect()->to('');
       }
   }

Here is my Views
Code:
<div class="row">
   <div class="col-md-12">
       <div class="card-box">
           <h4 class="m-t-0 header-title"><?php echo $page_heading; ?></h4>

           <?= service('validation')->listErrors() ?>
           
           <?php echo form_open('admin/pages/create'); ?>

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()
{
    if ($this->request->getMethod() === 'post')
    {
        if ($this->validate(['title' => 'required''body' => 'required']))
        {
            if ($this->model->save(['title' => $this->request->getPost('title'), 'content' => $this->request->getPost('body')]))
            {
                return redirect()->to('');
            }
            else
            
{
                return redirect()->back()->withInput()->with('message''Unable to Save.');
                //Change the code above for the following if the validation is done in the model and remove 'if validate section'
                //return redirect()->back()->withInput()->with('errors', $this->model->errors());
            }
        }
        else
        
{
            return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
        }
    }
    return view('Backend/Pages/Create');


views:
PHP Code:
//Backend/Themes/Base
<html>...
<
body>
...
<
div class="container">
    <?
php if (session('message')) : ?>
        <div class="alert alert-info alert-dismissible">
            <?= session('message'?>
            <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
        </div>
    <?php endif ?>
    <?= $this->renderSection('content'?>        
</div>
</body>
</html> 
PHP Code:
// Backend/Pages/Create
<?= $this->extend('Backend/Themes/Base'?>
<?= $this
->section('content'?>
<h4>Create New Page</h4>
<form method="post">
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control <?= session('errors.title') ? 'is-invalid' '' ?>" id="title" name="title" value="<?= old('title'?>">
        <div class="invalid-feedback"><?= session('errors.title'?></div>
    </div>
    ...
</form>
<?= $this->endSection() ?>



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