• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation errors visible on form load

#1
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/tutor...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?
Reply

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

Reply

#3
Many thanks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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