Welcome Guest, Not a member yet? Register   Sign In
Issues with validator [SOLVED]
#1

(This post was last modified: 08-14-2021, 01:31 AM by InsiteFX.)

I programmed using Codeigniter back in Version 2. I am just now getting back into programming and getting up to speed with version and how all the features work. I figured a lot out but for some reason I can't seem to figure out how to get the validator working. 
I re-learned how to do a lot within the controller but nothing I try to do with validation is working. Here is my controller test code.

Code:
    public function contact() {
       
        helper(['form', 'url']);
       
        $validation = \Config\Services::validation();
        $email = \Config\Services::email();
        $data['submit_check']  = $this->request->getVar('submit_check');
       
        if ($data['submit_check'] == 1) {
           
    // Set Validation Rules
            $validation->setRule('email_address', 'Email Address', 'required');
    $validation->setRule('email_confirm', 'Confirm Email', 'required');
    $validation->setRule('subject', 'Subject', 'required');
    $validation->setRule('message', 'Message', 'required');
           
            $data['email_address'] = $this->request->getVar('email_address');
            $data['email_confirm'] = $this->request->getVar('email_confirm');
            $data['subject']      = $this->request->getVar('subject');
            $data['message']      = $this->request->getVar('message');
           
            if ($validation->check()) {
                echo '<pre>';
                print_r($validation->getErrors());
                echo '</pre>';
                echo view('contact');
            } else {
                echo 'no';
            }           
            /*
    if ($validation->run() == FALSE) {
    echo view('contact', $data);
    }else {
    $email->setFrom($this->input->post('email_address'));
    $email->setTo('[email protected]');
    $email->setSubject('Online Wasteland Contact Form');
    $email->setMessage(
    'Subject: ' . $this->input->post('subject') . "\n" .
    'message: ' . $this->input->post('message') . "\n");
   
    if ($email->send()) {
        $data['contact_message'] = 'I have received your contact email. I will respond within 24/48 hours.';
                    $data['precontacted'] = 1;
    }else {
        $data['contact_message'] = 'I have NOT received your email.  Please try again';
    }
    //redirect('contact');
                echo view('contact', $data);
    }
            */
        } else {
            echo view('contact', $data);
        }
         
    }
Now when it comes to validation I have tried everything. validate(), run(), check() and various others. I tried accessing it via $validation-> and via $this. When I try to run any of the validate commands the whole page crashes and says. "Whoops you have hit a snag".. Any advice on how I can get basic validation working so I can start learning from there as I port over an old website.

Also, quick questions. What libraries/helpers can I still access via $this and which ones need to be called using this format: 
        $validation = \Config\Services::validation();

I see some examples online that use $this->validation->functionname; and others that put it in a variable as show above.
Reply
#2

Try this:

CodeIgniter 4 Form Validation Example
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Thanks. That worked.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB