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

Create user action called more than once

Hi guys,

I am developing an single module application that amongst others, can handle multiple users. I have created the required form with all the required fields and the action that handles the creation of the user. However, when I try to add a user, I get a message after a few seconds that this email has already been registered (the message come from the uniqueness of the email field). However, when I view the users table, I see that the user has been created and can login too. This hints that the action was called more than once.

Here is my create action:

public function createAction()
    {
        if ($this->request->isPost()) {

            $user = new Users();

            $user->assign(array(
                'first_name' => $this->request->getPost('first_name', 'striptags'),
                'last_name' => $this->request->getPost('last_name', 'striptags'),
                'business_name' => $this->request->getPost('business_name', 'striptags'),
                'business_type_id' => $this->request->getPost('business_type_id', 'int'),
                'vat_no' => $this->request->getPost('vat_no', 'striptags'),
                'doy_id' => $this->request->getPost('doy_id', 'int'),
                'country_id' => $this->request->getPost('country_id', 'int'),
                'address' =>$this->request->getPost('address', 'striptags'),
                'phone1' => $this->request->getPost('phone1', 'striptags'),
                'phone2' => $this->request->getPost('phone2', 'striptags'),
                'user_url' => $this->request->getPost('user_url'),
                'profiles_id' => $this->request->getPost('profiles_id', 'int'),
                'email' => $this->request->getPost('email', 'email'),
                'banned' => $this->request->getPost('banned'),
                'suspended' => $this->request->getPost('suspended'),
                'active' => $this->request->getPost('active')
            ));

            if (!$user->save()) {
                $this->flash->error($user->getMessages());
            } else {

                $this->flash->success("User was created successfully");

                Tag::resetInput();
            }
        }

        $this->view->form = new UsersForm(null);

        $this->view->heading = "Create user";
        $this->tag->setTitle("Create User");
    }

Can anyone help me figure this out? As far as I can tell, the action is correct. The form is just a simple bootstrap form with a few fields and a submit button...



58.4k

Hi man

You can upload the validate function your model users? Anyway you can take look my example at https://github.com/phanbook/phanbook/blob/master/core/modules/oauth/controllers/RegisterController.php#L145 it working for me

Hi,

I am using the code below to make sure no email can be registered more than once:

public function validation()
    {
        $this->validate(new Uniqueness(array(
            "field" => "email",
            "message" => "The email is already registered"
        )));

        return $this->validationHasFailed() != true;
    }

Has anyone managed to find something that can help me out???



58.4k

Hey man

Try something code below

            $user = new Users();
            $form = new UsersForm(null);
            $form->bind($_POST, $user);
             if (!$form->isValid($_POST)) {
                foreach ($form->getMessages() as $message) {
                    $this->flashSession->error($message);
                }
                return false;
            } else {
                 if (!$user->save()) {
                         $this->flash->error($user->getMessages());
                } else {

                    $this->flash->success("User was created successfully");

                    Tag::resetInput();
                }

            }

        $this->view->form = $form;

        $this->view->heading = "Create user";
        $this->tag->setTitle("Create User");

No luck. The code above behaves the same way. If I remove the validator for the email address, the action just keeps adding users indefinitely....

Any ideas???



8.9k
Accepted
answer

OK, figured it out. The form included a URL field the user had to fill in. However, URLs that included the "https://" part just failed to be inserted into the database, even when using the string filter. Entering a url without "https://" worked perfectly. So I made a custom filter to strip 'https://', 'https://' and 'www' from all urls. The code for it is below:

class UrlFilter
{

    public function filter($value)
    {
        return preg_replace('/(?:https?:\/\/)?(?:www\.)?(.*)\/?$/i', '$1', $value);
    }

}