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 uniqueness

Why is that i can't use

use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;

like this:

use Phalcon\Mvc\Model\Validator\Uniqueness; use Phalcon\Validation\Validator\Confirmation;

$this->add('email', new Uniqueness([
            'field'  => 'email',
            'message' => 'error!'
    ]));

Does it only work inside models, like this?:

$this->validate(new Uniqueness(array(
      'field' => 'email'
  )));

I think you're confused between Phalcon\Validation\Validator\Uniqueness and Phalcon\Mvc\Model\Validator\Uniqueness.

In the context of a model you should be using Phalcon\Mvc\Model\Validator\Uniqueness, but you've posted a mix of the usage between both of those validation classes.

Proper usage (as shown in API docs) is as follows:

use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Validator\Uniqueness;

class Subscriptors extends Model
{

  public function validation()
  {
      $this->validate(new Uniqueness(array(
          "field" => 'email',
          "message" => 'Error!'
      )));
      if ($this->validationHasFailed() == true) {
          return false;
      }
  }

I know that there is a diffrent. I just get this error with i include the unique:

Class 'Phalcon\Validation\Validator\Uniqueness' not found in

That can't be right, can it?

I know that there is a diffrent. I just get this error with i include the unique:

Class 'Phalcon\Validation\Validator\Uniqueness' not found in

That can't be right, can it?

Sounds a bit off, as it is the right namespace+class.

Do you have a namespace set in the script that you're using this in? If so, either use do

use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
//or just use (note the prefixed '\')
\Phalcon\Validation\Validator\Uniqueness

No I dont.. That's what dosent make any sence... here is my full code

namespace Validation;

use Phalcon\Validation; use Phalcon\Validation\Validator\Email; use Phalcon\Validation\Validator\PresenceOf; use \Phalcon\Validation\Validator\Uniqueness as UniquenessValidator; use Phalcon\Validation\Validator\Confirmation;

class Validate extends Validation {

public function initialize()
{

    $this->add('firstname', new PresenceOf(array(
        'message' => 'The name is required'
    )));

    $this->add('lastname', new PresenceOf(array(
        'message' => 'The name is required'
    )));

    $this->add('email', new PresenceOf(array(
        'message' => 'The e-mail is required',
        'cancelOnFail' => true
    )));

    $this->add('email', new Email(array(
        'message' => 'The e-mail is not valid',
    )));

    $this->add('email', new UniquenessValidator(array(
        'message' => 'The e-mail is required',
    )));

    $this->add('password', new Confirmation(array(
       'message' => 'Password doesn\'t match confirmation',
       'with' => 'confirmPassword'
    )));

    $this->setFilters('email', 'trim');

}

}