Welcome Guest, Not a member yet? Register   Sign In
Issue with form validation
#7

(This post was last modified: 09-30-2018, 02:46 PM by happyape.)

(09-30-2018, 10:48 AM)unodepiera Wrote:
(09-30-2018, 03:57 AM)InsiteFX Wrote: This has to do with the News Tutorial. Try that and tell me what you get.

You are right InsiteFX, the documentation is wrong, the validation accept an array of rules, but $this->request is an object, this works:

PHP Code:
public function create()
{
 
   helper('form');
 
   $model = new \App\Models\News();

 
   if (! $this->validate([
 
       'title' => 'required|min[3]|max[255]',
 
       'text'  => 'required'
 
   ]))
 
   {
 
       echo view('templates/header', ['title' => 'Create a news item']);
 
       echo view('news/create');
 
       echo view('templates/footer');

 
   }
 
   else
    
{
 
       $model->save([
 
           'title' => $this->request->getVar('title'),
 
           'slug'  => url_title($this->request->getVar('title')),
 
           'text'  => $this->request->getVar('text'),
 
       ]);
 
       echo view('news/success');
 
   }



I just tested this.

My controller method -
PHP Code:
public function login()
 {
 
       $validation $this->validate([
 
           'email' => 'required|valid_email',
 
           'password' => 'required|min_length[8]']);
 
       if ( ! $validation) {
 
           echo view('form');
 
       } else {
 
           echo 'Valid.';
 
       }


in my views/form.php
Code:
<?php echo service('validation')->listErrors() ?>

<form action="/login" method="post" class="dsform">
   <h1 class="h3 mb-3 font-weight-normal">Please login</h1>
   <label for="inputEmail" class="sr-only">Email address</label>
   <input type="text" id="inputEmail" name="email" class="form-control" placeholder="Email address" autofocus value="">
   <label for="inputPassword" class="sr-only">Password</label>
   <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" value="">
   <button class="btn btn-success btn-large btn-block" type="submit">Sign in</button>
</form>

It all works fine except when you open login page it shows you error list before even submitting the form.

I also noticed you mentiond this validation rule ...
Code:
'title' => 'required|min[3]|max[255]',

Min and max don't seem to be valid rule as they are min_length and max_length https://bcit-ci.github.io/CodeIgniter4/l...hlight=max
Reply


Messages In This Thread
Issue with form validation - by happyape - 09-27-2018, 12:47 AM
RE: Issue with form validation - by InsiteFX - 09-27-2018, 04:04 AM
RE: Issue with form validation - by happyape - 09-27-2018, 05:32 AM
RE: Issue with form validation - by unodepiera - 09-30-2018, 12:26 AM
RE: Issue with form validation - by InsiteFX - 09-30-2018, 03:57 AM
RE: Issue with form validation - by unodepiera - 09-30-2018, 10:48 AM
RE: Issue with form validation - by happyape - 09-30-2018, 02:46 PM
RE: Issue with form validation - by unodepiera - 09-30-2018, 10:47 PM
RE: Issue with form validation - by happyape - 10-01-2018, 12:47 AM
RE: Issue with form validation - by unodepiera - 10-01-2018, 01:18 AM
RE: Issue with form validation - by happyape - 10-01-2018, 01:55 AM
RE: Issue with form validation - by dave friend - 10-01-2018, 09:16 AM
RE: Issue with form validation - by InsiteFX - 10-01-2018, 10:40 AM
RE: Issue with form validation - by dave friend - 10-01-2018, 11:05 AM
RE: Issue with form validation - by InsiteFX - 10-01-2018, 12:08 PM
RE: Issue with form validation - by Ekley - 10-11-2019, 07:22 AM
RE: Issue with form validation - by Bart Goossens - 08-22-2020, 08:58 AM
RE: Issue with form validation - by jreklund - 08-22-2020, 12:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB