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

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

    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>';
                echo '</pre>';
                echo view('contact');
            } else {
                echo 'no';
    if ($validation->run() == FALSE) {
    echo view('contact', $data);
    }else {
    $email->setTo('[email protected]');
    $email->setSubject('Online Wasteland Contact Form');
    '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';
                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.

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 )

Thanks. That worked.

Theme © iAndrew 2016 - Forum software by © MyBB