mruz / base-app

The base application in PhalconPHP
63 stars 19 forks source link

using form on each module #31

Closed newzen closed 9 years ago

newzen commented 9 years ago

first, nice work

wonder to now how to user form's on your base-app.

Good example could be use a form for sign-up and sign-in.

Thanks for your help

mruz commented 9 years ago

See the signup action in the UserController. If request is post then runs signup from the Users model which try to validate and create new user. If user was created returns user object, else returns validation messages. signup.volt view displays form with errors if occurs.

newzen commented 9 years ago

thanks for quick response,

i think is just a style, you prefer to make validation inside the model. On my side i think is more clear to put form's in form directory, but I'm pretty new to phalcon to figure how take outside the model and include in form module directory. Would you orient me to take outside. i respect your working style and your nice work on this base-app

thanks for your time

mruz commented 9 years ago

You can move validation from model to module: controller:

public function signupAction()
{
    if ($this->request->isPost() == TRUE) {
        $validation = new Validation();
        // rules...

        $messages = $validation->validate($_POST);

        if (!count($messages)) {
            $user = new Users();
            $messages = $user->signup();
        }

        if (!count($messages)) {
            $this->flashSession->notice("Check Email to activate your account.");
        } else {
            $this->view->setVar('errors', $messages);
            $this->flashSession->warning("Please correct the errors.");
        }
    }
}

model:

public function signup()
{
    $this->username = $this->request->getPost('username');
    $this->password = $this->getDI()->getShared('auth')->hash($this->request->getPost('password'));
    $this->email = $this->request->getPost('email');
    $this->logins = 0;

    if ($this->create() === true) {
        $hash = md5($this->id . $this->email . $this->password . $this->getDI()->getShared('config')->auth->hash_key);
        $email = new Email();
        $email->prepare(__('Activation'), $this->request->getPost('email'), 'activation', array('username' => $this->request->getPost('username'), 'hash' => $hash));

        if ($email->Send() === true) {
            unset($_POST);
            return true;
        } else {
            \Baseapp\Bootstrap::log($email->ErrorInfo);
            return false;
        }
    } else {
        return $this->getMessages();
    }
}