Welcome Guest, Not a member yet? Register   Sign In
Validation Always Print Error Message

(This post was last modified: 03-31-2019, 03:52 AM by rmcdahal.)

Here is my Controller

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 {
                   'title' => $this->request->getvar('title'),
                   'content' => $this->request->getVar('body')
           return redirect()->to('');

Here is my Views
<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 ?

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.

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

<?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.

(This post was last modified: 04-03-2019, 06:35 AM by elephpantech. Edit Reason: typo error )

My approach would be:

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('');
                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());
            return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
    return view('Backend/Pages/Create');

PHP Code:
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>
    <?php endif ?>
    <?= $this->renderSection('content'?>        
PHP Code:
// Backend/Pages/Create
<?= $this->extend('Backend/Themes/Base'?>
<?= $this
<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>
<?= $this->endSection() ?>

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...er.php#L62

Theme © iAndrew 2016 - Forum software by © MyBB