Welcome Guest, Not a member yet? Register   Sign In
Form validation - Always false?
#1

I have a very basic form with a controller that looks like this:
class Home extends BaseController
PHP Code:
{
    public function index()
    {
        helper(['form''url']);
        if (!$this->validate([

            'naam' => 'required'
        
        
])) {        
            
echo view('form', [
                'validation' => $this->validator,
            ]);
        } else {
            echo "success!";
        }
    }


The view has a section at the top for showing the errors:
PHP Code:
<?php if($validation->listErrors()) {?>
  <div class="alert alert-info" role="alert">
Please check the marked fields
</div>
<?php ?>
</div> 

For some reason the validation is always triggered and I can't figure out why. Should you use seperate functions in CI4?
Reply
#2

The user guide is a little vague or maybe even incorrect. If I follow the tutorial on:
https://codeigniter.com/user_guide/libra...n-tutorial
I get the same error as you get.
Reply
#3

(This post was last modified: 07-25-2022, 11:32 AM by demyr.)

I will post here an example from my Register page. I hope it will also work for you.

Controller:

PHP Code:
$data = [
    'user_name' => strip_tags(trim($this->request->getVar('user_name'))),
    'user_surname' => strip_tags(trim($this->request->getVar('user_surname'))),
    'user_email' => strip_tags(trim($this->request->getVar('user_email'))),
    'user_password' => strip_tags(trim($this->request->getVar('user_password')))
];

if(
$this->request->getMethod() == 'post'){
    $rules = [
        'user_name' => [
            'rules' => 'required|min_length[3]|max_length[60]',
            'errors' => [
                'required' => 'Cannot be left empty',
                'min_length' => 'You should type at least 3 characters',
                'max_length' => 'Too long for a name'
            ]
                        ],
        'user_surname' => [
            'rules' => 'required|min_length[3]|max_length[60]',
            'errors' => [
              'required' => 'Cannot be left empty',
              'min_length' => 'You should type at least 3 characters',
              'max_length' => 'Too long for a surname'
                        ]
          ],
          'user_email' => [
              'rules' => 'required|valid_email',
              'errors' => [
                'required' => 'Cannot be left empty.',
                'valid_email' => 'Not in a correct format'
                          ]
            ],
          'user_password' => [
                'rules' => 'required|min_length[4]|max_length[20]',
                'errors' => [
                  'required' => 'Cannot be left empty',
                  'min_length' => 'Your password must have at least 4 characters',
                  'max_length' => 'Max 20 characters for the password, please'
                            ]
              ],


    ];

    if($this->validate($rules)){

      // do sth here, send your data and show your view etc.

}else{
      $data['title'] = 'Register Page';

      echo view ('your_view_folder/assets/header',$data);
      echo view('your_view_folder/register', ['validation' => $this->validator]);
      echo view('your_view_folder/assets/footer');



In your Register page

PHP Code:
<?php if(isset($validation)){
      echo $validation->listErrors();
    }?>
Reply
#4

(This post was last modified: 07-26-2022, 12:00 AM by vinyl.)

Thanks, wil give that a try today!

[edit]
Codeigniter 4 is giving me a whole lot of headaches so I am seriously considering going back to version 3....

[/edit]
Reply
#5

(07-25-2022, 10:05 PM)vinyl Wrote: Thanks, wil give that a try today!

[edit]
Codeigniter 4 is giving me a whole lot of headaches so I am seriously considering going back to version 3....

[/edit]

No need for this. Believe me there are lots of tutorials now, both on blogs or on YouTube. You will handle it easily. CI4 is nice enough.

P.S. Maybe you should paste more code examples here.. eg: your view, controller, results etc.
Reply
#6

(07-25-2022, 05:56 AM)JustJohnQ Wrote: The user guide is a little vague or maybe even incorrect. If I follow the tutorial on:
https://codeigniter.com/user_guide/libra...n-tutorial
I get the same error as you get.

What error do you get?
Reply
#7

Sorry, I should have been more clear. I did not get an error, just unexpected results. While loading the page for the first time it shows the validation errors straight away.
It looks like the manual is missing some additional code to check for a POST request before displaying any validation errors.
Reply
#8

(07-26-2022, 12:28 PM)JustJohnQ Wrote: While loading the page for the first time it shows the validation errors straight away.
It looks like the manual is missing some additional code to check for a POST request before displaying any validation errors.

Indeed, it is a bit odd to see validation errors for the first time.

I sent a PR to improve the tutorial code:
https://github.com/codeigniter4/CodeIgniter4/pull/6300
Reply
#9

@JustJohnQ
Form Validation Tutorial has been updated:
https://codeigniter4.github.io/CodeIgnit...n-tutorial
Reply




Theme © iAndrew 2016 - Forum software by © MyBB