質問

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;
    }
}
役に立ちましたか?

解決

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.

他のヒント

You should try to refer to the actual controller.

Both of these will work.

 $form_ok = self::validate();

or

$this->validate();

It should be $this->validate as its referring to a method within the class.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top