Undefined Variable, PHP MVC Application

1

I'm having the following error returning in my code

  

Notice: Undefined variable: errors in D: \ wamp \ www \ mvc \ application \ controllers \ login.php on line 33

This is my Controller:

class Login extends BaseController {

    public function __construct() {
        parent::__construct();
    }

    public function index() {

        if (Input::exists()) {
            $validate = new Validate();

            $validation = $validate->check($_POST, array(
                'username' => array(
                    'required' => true
                ),
                'password' => array(
                    'required' => true
                ),
            ));

            if ($validation->passed()) {
                //logic code for this if
            } else {
                $errors = $validation->errors();
            }
        }

        $data = [
            'title' => WEB_TITLE,
            'validation' => $errors
        ];

        $this->view->display('templates/default/header', $data);
        $this->view->display('login/index', $data);
        $this->view->display('templates/default/footer', $data);
    }

}

And this is my View:

<section class="section">
    <div class="container">
        <div class="row">
            <div class="col-lg-4 col-lg-offset-4">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <h3 class="page-header">
                            Member login
                        </h3>

                        <form method="post">
                            <div class="form-group">
                                <input type="text" name="username" placeholder="Username or E-mail" class="form-control input-sm" autocomplete="off">
                            </div>

                            <div class="form-group">
                                <input type="password" name="password" placeholder="Your password" class="form-control input-sm">
                            </div>

                            <button type="submit" name="loginBtn" class="btn btn-sm btn-success btn-block">
                                <i class="glyphicon glyphicon-log-in"></i> Sign in
                            </button>
                        </form>

                        <?php
                       if ($data['validation']) {
                           echo $data['validation'][0];
                       }
                        ?>
                    </div>
                </div>

                <div class="text-center">
                    <a href="<?=Url::url_base()?>">Back to home!</a>
                </div>
            </div>
        </div>
    </div>
</section>

Note: error only fades if I put a @ before my variable @$errors

    
asked by anonymous 09.05.2016 / 16:20

1 answer

3

The problem is the scope of $errors , what happens if the code drops in if? $error is not created / defined.

if ($validation->passed()) {
  //logic code for this if
} else {
   $errors = $validation->errors();
}

$data = ['title' => WEB_TITLE, 'validation' => $errors];

1 - You can solve this by setting $errors to empty array before if.

$errors = [];
if ($validation->passed()) {

2 - If you do not have any logic associated with else , you can take the errors and define in $data creation, it eliminates the variable $errors

$data = ['title' => WEB_TITLE, 'validation' => $validation->errors()];
    
09.05.2016 / 16:33