Undefined variable, PHP MVC application

Asked

Viewed 153 times

1

I have 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: the error only disappears if I put one @ before my variable @$errors

1 answer

3


The problem is the scope of $errors, what happens if the code falls on the 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 as empty array before if.

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

2 - If you have no logic associated with else, can pick up errors and set in creating $data this eliminates the variable $errors

$data = ['title' => WEB_TITLE, 'validation' => $validation->errors()];
  • Show rray, I had done it but I removed it and forgot to put it again. Thank you very much.

  • I’m having another different error in this same code Can you help me? I can’t find the solution: Notice: Trying to get property of non-object in D:\wamp\www\humble\application\controllers\auth.php on line 46

  • 1

    @Guilhermespinxo can yes, create a new question, cm the updated code.

  • Pronto @rray http://answall.com/questions/128214/qual-o-motivo-trying-to-get-property-of-non-object-in

Browser other questions tagged

You are not signed in. Login or sign up in order to post.