Search code examples
concrete5

Concrete5 validation not working on a single page


I am trying to validate a form on a single page. Below is what I've tried but $validation->test() doesn't seem to be working. It's supposed to redirect to another page upon validating the first_name field. I get stuck at this url: http://localhost/mysite/index.php/buy/process/. I am using 5.6.x. Not sure what I am doing wrong.

public function process() {
    $validation = Loader::helper('validation/form');
    $validation->setData($this->post());
    $validation->addRequired($this->post('first_name'), 'First Name must not be empty');
    if ($validation->test()) {
        ....
        ....
        $this->redirect('/buy/step2');
    } else {
        $errorArray = $validation->getError()->getList();
        $this->set('errorArray', $errorArray);
    }
}

This function is called from the view:

<form enctype="multipart/form-data" id="buy" class="buy-form" method="post" action="<?php echo $this->action('process')?>">.....</form>

Solution

  • I had to replace

    $validation->addRequired($this->post('first_name'), 'First Name must not be empty');

    with

    $validation->addRequired('first_name', 'First Name must not be empty');

    That did it.