CodeIgniter Forums
Validation errors visible on form load - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Validation errors visible on form load (/showthread.php?tid=76377)



Validation errors visible on form load - obu2020 - 05-07-2020

When using validation in a controller like so:

Code:
public function login() {

    if (!$this->validate([
      'username' => 'required',
      'password'  => 'required'
    ]))
    {
I see validation errors on page load (without submitting the form). This behaviour is new to CI4. All my CI3 code only shows validation errors on form submit.

The official documentation also has validation errors on page load - https://codeigniter.com/user_guide/tutorial/create_news_items.html as shown in this image [Image: tutorial3.png]

My question is can validation errors be suppressed at least until the form is submitted?


RE: Validation errors visible on form load - jreklund - 05-07-2020

If you don't want it to validate/run on $_GET request you can add "$this->request->getMethod() === 'get'".

PHP Code:
public function create()
{
    $model = new NewsModel();

    if (
        $this->request->getMethod() === 'get' ||
        $this->validate([
            'title' => 'required|min_length[3]|max_length[255]',
            'body'  => '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'), '-'TRUE),
            'body'  => $this->request->getVar('body'),
        ]);

        echo view('news/success');
    }




RE: Validation errors visible on form load - obu2020 - 05-08-2020

Many thanks.