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

Validation preventing redirect

Hello i have a code where i check for a Uniqueness on a field, i do :

$user = new User();
$user->save(array(
'username' => $username,
'password' => $password
));

And in my User model i have :

public function validation()
{
  $this->validate(new Uniqueness(
      'field' => 'username',
      'message' => 'User already exists'
  ))

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

But when it goes through validation and fails, the code doesn't go past the save function called a above. I added an echo 'test'; after save codea above and it never shows up

Your return statement is false, thus your execution flow stops.

edited Aug '16

Offtopic: Please check the HELP button on top of your editor when you write a post. This way you will format your code so it will be easier for others to read it later :)

Here is a code sample for registration action of old project of mine:

public function registrationAction()
{
    // Already logged?
    if ($this->session->has('user')) {
        return $this->response->redirect(['for' => 'profile']);
    }

    $form = new UsersRegistration();
    if ($this->request->isPost() AND $this->security->checkToken() AND $form->isValid($this->request->getPost())) {
        $whitelisted = ['..', '..'];
        $obj = new Users();
        $result = $obj->save($this->request->getPost(), $whitelisted);
        if ($result === true) {
            $this->assignSession($obj);
            $this->flash->success($this->translations->public->general->registrationSuccess);
            return $this->response->redirect(['for' => 'home']);
        } else {
            $this->view->formErrors = $obj->getMessages();
        }
    }
    $this->view->form = $form;
}

The Users model above has Uniqueness validation like you do.



4.5k
edited Aug '16

Ok after enabling error_reporting in php.ini i get this error :

Fatal error: Class 'Phalcon\Di\FactoryDefault' not found in /var/www/project/public/index.php on line 15

I get this error on all pages whenever i enable error_reporting(E_ALL) in php.ini, is this normal ? php5.6 on a debian wheezy

Oh, this error usually means that Phalcon is not installed correctly? Strange...

Check PHP info. But how come that you had issue with your Model, if you cannot even load Phalcon?!



4.5k
edited Aug '16

Check PHP info. But how come that you had issue with your Model, if you cannot even load Phalcon?!

Well this happens only when i enable error_reporting(E_ALL) in php.ini if i disable the error_reporting it works again, but blank page on validation. And i remove say a comma from the validation function, no error reported..

I installed phalcon by compiling sources, since using the script to add the repo and doing : apt-get install php5-phalcon says it cannot find the package (i already checked if repo is in /etc/apt/sources.list.d/ and it is, then did an apt-get update in case.. but same)

Then you have issue with installation. Check documentation and forums here for various topics regarding installing/compilation.



4.5k

I get this when i

php - i | grep phalcon

phalcon
phalcon => enabled
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.column_renaming => On => On
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.enable_literals => On => On
phalcon.orm.events => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.ignore_unknown_columns => Off => Off
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.not_null_validations => On => On
phalcon.orm.virtual_foreign_keys => On => On
OLDPWD => /var/www/cphalcon/build
PWD => /var/www/cphalcon
_SERVER["OLDPWD"] => /var/www/cphalcon/build
_SERVER["PWD"] => /var/www/cphalcon


145.0k
Accepted
answer
edited Aug '16

Then phalcon is installed for cli but you don't have phalcon.ini linked to fpm/apache2 php loaded modules.

Question started as validation problem and ended up as installation problem, what a twist hehe :)