Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel controller thinks my function is undefined

Tags:

php

laravel

Pretty simple. I'm trying to call a function from within my controller. The function is, in fact, defined. Yet, when the function is called I get "PHP Fatal Error: Call to undefined function validate() ..."

Here's my code. Any ideas? Thanks.

<?php

class HomeController extends BaseController {

    /**
     * Controller for the index action of the home page.  Displays the landing page.
     */
    public function index()
    {
        return View::make('landing', array('success' => false));
    }

    /**
     * Controller to handle processing the contact form and re-displaying the landing page
     */
    public function processForm()
    {
        $form_array = array();
        $errors = array();

        foreach (array('email','fname','lname','message','newsletter') as $val)
        {
            if (isset($_POST[$val]))
                $form_array[$val] = $_POST[$val];
            else
                $form_array[$val] = null;
        }

        $form_ok = validate();

        if ($form_ok)
        {
            echo "GOOD!";
        }
        else
        {
            echo "BAD!";
        }
    }

    /**
     * Helper function for validating the form.  Returns true if the form was 
     * submitted without errors.
     */
    public function validate()
    {
        return true;
    }
}
like image 256
Vince Avatar asked May 14 '14 20:05

Vince


2 Answers

It looks like you're trying to call $this->validate(), instead of validate(). You've defined validate() as a class method, not a stand alone function.

like image 86
Jason Avatar answered Oct 17 '22 12:10

Jason


You should try to refer to the actual controller.

Both of these will work.

 $form_ok = self::validate();

or

$this->validate();
like image 4
Wistar Avatar answered Oct 17 '22 11:10

Wistar