Show validation error in View

0

I have a problem, I can not show the form validation error in View (MVC done by me) correctly, it is showing in the wrong place, I would like it to stay on top of the button or down.

Model:

<?php

class Login_Model extends Model {

    public $_errorVal;

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

    public function login() {
        if (isset($_POST['btn-login'])) {
            try {

                $form = new Form();

                $form->post('username')
                     ->val('required')

                 ->post('password')
                 ->val('required');

                $this->_errorVal = $form->submit();

            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }
}

Controller

<?php

class Login extends Controller {

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

    public function index() {

        $this->view->title = 'Nome da Web - login';

        $this->model->login();

        $this->view->formValidation = $this->model->_errorVal;

        $this->view->render('header');
        $this->view->render('login/index');
        $this->view->render('footer');
    }
}

View:

<section class="section">
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-md-offset-4">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <form method="post">
                            <div class="form-group">
                                <input type="text" name="username" placeholder="Usuário" class="form-control">
                            </div>

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

                            <div class="text-right">
                                <button type="submit" name="btn-login" class="btn btn-success">Entrar</button>
                            </div>
                        </form>
                        <?php
                            if (isset($this->formValidation)) {
                                echo $this->formValidation;
                            }
                        ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Image of how you are printing on the screen:

Iwantthemessagetoappearbelowtheenterbuttonorontopofit.Note:IdonotwanttomakeuseofFrameworkssoifsomeonesuggestsI'lljustignoreitright?

TheprojectcodeisinGitHub: Project here

    
asked by anonymous 29.02.2016 / 02:32

1 answer

2

See that submit() is not returning anything, only true on success:

public function submit()
{
    if (empty($this->_error)) 
    {
        return true;
    } 
    else 
    {
        $str = '';
        foreach ($this->_error as $key => $value)
        {
            $str .= ucfirst($key) . ' ' . $value . "<br/>";
        }
        throw new Exception($str);
    }
}

However here you try to get the value of it, this is unnecessary because at most it will get true :

$this->_errorVal = $form->submit();

Already you have fired a echo in case of throw new Exception

    $this->_errorVal = $form->submit();

} catch (Exception $e) {
    echo $e->getMessage();
}

The correct one seems to capture the error like this:

try {
    $form = new Form();

    $form->post('username')
         ->val('required')

     ->post('password')
     ->val('required');

    $form->submit();
} catch (Exception $e) {
    $this->_errorVal = $e->getMessage();
}
    
29.02.2016 / 02:52