• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with form validation

#1
Here is my code -

This gives me "Undefined property: App\Controllers\Home::$validation" error.
Code:
public function login()
    {

        if (! $this->validate([
            'email' => 'required',
            'password'  => 'required'
        ])) {
            echo Services::plates('login', [
                'validation' => $this->validation
            ]);
        } else {
            //'title' => $this->request->getVar('title'),
        }

    }

However, I can fix this error using this b
Code:
$validation =  \Config\Services::validation();
but I think as per documentation I should be able to pass validation object as above.

Another issue is in the view
Code:
<?= $validation->listErrors() ?>

It just shows me error messages for the given rules above without even submitting the form along with the name of the validation view file name.

Code:
1 ROOTPATH/system/Validation/Views/list.php

    The email field is required.
    The password field is required.

What am I doing wrong?
Reply

#2
There is a bug in the validate method it is expecting $data but its not there.

I have already reported this bug and it is being looked into.

[Image: type-error.png]
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
I see. I thought so that my code was correct. Thanks.
Reply

#4
(09-27-2018, 12:47 AM)happyape Wrote: Here is my code -

This gives me "Undefined property: App\Controllers\Home::$validation" error.
Code:
   public function login()
{

       if (! $this->validate([
           'email' => 'required',
           'password'  => 'required'
       ])) {
           echo Services::plates('login', [
               'validation' => $this->validation
           ]);
       } else {
           //'title' => $this->request->getVar('title'),
       }

}

However, I can fix this error using this b
Code:
$validation =  \Config\Services::validation();
but I think as per documentation I should be able to pass validation object as above.

Another issue is in the view
Code:
   <?= $validation->listErrors() ?>

It just shows me error messages for the given rules above without even submitting the form along with the name of the validation view file name.

Code:
1 ROOTPATH/system/Validation/Views/list.php

   The email field is required.
   The password field is required.

What am I doing wrong?

Validation works fine for me:

Some Controller:
PHP Code:
$validation $this->validate(Array rules, Array messages);
if ( ! 
$validation) {
 
   return redirect('/register')->withInput();


withInput method from CodeIgniter Core:
PHP Code:
public function withInput()
{
    
$session $this->ensureSession();

    
$input = [
        
'get'     => $_GET ?? [],
        
'post'     => $_POST ?? [],
    ];

    
$session->setFlashdata('_ci_old_input'$input);

    
// If the validator has any errors, transmit those back
    // so they can be displayed when the validation is
    // handled within a method different than displaying the form.
    
$validator Services::validation();
    if (! empty(
$validator->getErrors()))
    {
        
$session->setFlashdata('_ci_validation_errors'serialize($validator->getErrors()));
    }

    return 
$this;


Register View:
PHP Code:
<?php echo service('validation')->listErrors() ?>

If you want access to validator from some controller you can do it:
[Image: 2ZqiHIG.png]
Reply

#5
This has to do with the News Tutorial. Try that and tell me what you get.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
(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');
 
   }

Reply

#7
(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

#8
(09-30-2018, 02:46 PM)happyape Wrote:
(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

It is the expected behavior, you are validating the form each time that the login method is called, you only need to validate the form if it is a POST request, refactor your code:

PHP Code:
public function login () {
    
//  if ($this->request->getPost()) { Also works
    
if ($this->request->getGetPost()) {
        
$validation $this->validate(
            [
                
'email' => 'required|valid_email',
 
               'password' => 'required|min_length[8]']);
            ]
        );
        if ( ! 
$validation) {
            return 
redirect('/login')->withInput();
        } else {
            
//TODO: login user
        
}
    } else {
        return 
view'login');
    }

Reply

#9
Yes that makes sense to me. Thank you.

In the documentation there was no suggestion to check for the POST request so I thought it would do validation for POST requests only.
Reply

#10
(10-01-2018, 12:47 AM)happyape Wrote: Yes that makes sense to me. Thank you.

In the documentation there was no suggestion to check for the POST request so I thought it would do validation for POST requests only.

Your are right, the documentation must be updated with best practices.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.