Show validation error in View

Asked

Viewed 271 times

0

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

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>

Screenshot of how you are printing on screen:

inserir a descrição da imagem aqui

I want the message to be displayed below the enter button or on top of it. Note: I don’t want to make use of Frameworks so if anyone suggests I’ll just ignore right?

The project code is on Github: Project here

  • Are you sure the variable $this->view->formValidation is sitting the value?

  • So there is no other logic to do this, at least I did not find... when debugging using var_dump(); returns me null and I don’t know how to proceed :/, of all cases I put a project link on github pro staff try to help me.

  • Your architecture is a little confusing, but I think I’ve been able to answer.

1 answer

2


See that submit() is not returning anything, only the true in case of 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 catch the value of it, that is unnecessary because at most it will catch the true:

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

Already here you fired one echo in the event of throw new Exception

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

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

The correct seems to me to capture the error so:

try {
    $form = new Form();

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

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

    $form->submit();
} catch (Exception $e) {
    $this->_errorVal = $e->getMessage();
}
  • It worked, but that doubt is still in the air, the catch does not let the code send?

  • @Guilhermespinxo not this problem in catch, see your submit I didn’t return anything I just had the throw new Exception($str);, won’t display anything because nothing returned, and the validation error messages appeared on top of everything because you used echo in the catch when I should have set the variable, I don’t know if I could be clear.

  • 1

    All right then thank you very much.

Browser other questions tagged

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