We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Form validation msgs outside of the form's page

Hi Guys,

Firstable let me tell you how excited is to work with Phalcon, it's awesome and it saves a lot of time!

I created my first users authentication and registration forms and everything is working fine, I'm able to add new users and I'm able to authenticate but the issue that I'm having is that I want to display the validation message on the same page that I have the form and not on another page or view, right now if the user types a wrong password or username my validation message gets displayed in a new page or view under login/start instead of under /login where the form I resides. I tried specifing the view using $this->view->pick("login"); but is not working. Please help! I uploaded my project files to githug under PhalconIssues/phalcontutorial.com.zip



43.9k
edited Jan '18

hi welcome.

for a better help some code is needed.

it looks like the form submission action forwards you to the wrong page ...

Hi, thanks for your reply.

Here is my form under the login/index/phtml

                        <form id="login-form" action="login/start" method="post" role="form" style="display: block;">
                            <div class="form-group">
                                <input type="text" name="username" id="username" tabindex="1" class="form-control" placeholder="Username" value="">
                            </div>
                            <div class="form-group">
                                <input type="password" name="password" id="password" tabindex="2" class="form-control" placeholder="Password">
                            </div>
                            <div class="form-group text-center">
                                <input type="checkbox" tabindex="3" class="" name="remember" id="remember">
                                <label for="remember"> Remember Me</label>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-sm-6 col-sm-offset-3">
                                        <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-lg-12">
                                        <div class="text-center">
                                            <a href="https://phpoll.com/recover" tabindex="5" class="forgot-password">Forgot Password?</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>

This is my login controller:

    public function indexAction()
{
    //Inserto los assets de los CSS remotos y locales
    $this->assets
        ->addCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',false)
        ->addCss('css/estilos.css');

    //Inserto los assets de los javascript remotos y locales
    $this->assets
        ->addJs('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js',false)
        ->addJs('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js',false)
        ->addJs('js/main.js');

}

public function startAction(){
    //Inserto los assets de los CSS remotos y locales
    $this->assets
        ->addCss('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',false)
        ->addCss('css/estilos.css');

    //Inserto los assets de los javascript remotos y locales
    $this->assets
        ->addJs('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js',false)
        ->addJs('https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js',false)
        ->addJs('js/main.js');

    if ($this->request->isPost()) {

        // Get the data from the user
        $email    = $this->request->getPost('username');
        $password = $this->request->getPost('password');

        $user = Users::findFirstByemail($email);

        if ($user) {

            if ($this->security->checkHash($password, $user->password)){
                $this->_registerSession($user);

                $this->flash->success(
                    'Welcome ' . $user->name
                );
            }
            else {
                $this->security->hash(rand());

                $this->flashSession->error("Wrong email/password");
                return $this->view->pick("login");
                /*$this->flash->error(
                    'Wrong email/password'
                );*/

            }
        }
        else {
            $this->security->hash(rand());
            $this->flash->error(
                'Wrong email/password'
            );

        }
    }
}

private function _registerSession($user){

    $this->session->set(
        'auth',
        [
            'id' => $user->id,
            'name' => $user->name,
        ]
    );
}


43.9k
edited Jan '18

you have to use forwarding https://docs.phalcon.io/en/3.2/dispatcher#forwarding,

let's say that the login form page is: LoginController with indexAction:


            else {
                $this->security->hash(rand());

                $this->dispatcher->forward([
                     "controller" => "login",
                        "action" => "index"
                 ]);
                $this->flash->error(
                    'Wrong email/password'
                );

            }

Awesome!!!! I just tried and it worked!

I'm in love with Phalcon!

Thanks for the documentation, I was trying to figure out how to pass the control to another controller.

One more question, which tool do you use to share the code ? any code snipped ?

Thanks,



43.9k
Accepted
answer
edited Jan '18

you can use github's gist utility. Look at your user menu when you're logged in and are browsing github.

You can mark post as solved also ;-)

Framework has an excellent documentation, it is really easy to use, flexible and powerfull.