// ....
use Phalcon\Mvc\Model;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Uniqueness;
// ....
public function validation() {
$this->validate(new Email([
"field" => "email",
"message" => "Bla bla Email"
]));
$this->validate(new Uniqueness([
"field" => "email",
"message" => "Bla bla Uniq"
]));
return $this->validationHasFailed() != true;
}
PHP Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Validation\Validator\Email
use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
class Users extends Model
{
public function validation()
{
$validator = new Validation();
$validator->add(
'email',
new EmailValidator()
);
return $this->validate($validator);
}
}
Thank you! Rather all this would appear in the documentation.
Uniqueness is still an error.
Phalcon\Validation::add() must implement interface Phalcon\Validation\ValidatorInterface
@jeftom This is relevant since 2.1.x version
public function validation() {
$validator = new Validation();
$translate = $this->getDI()->get('translate');
$validator->add('email', new Email([
"message" => $translate->t('Validator.email')
]));
$validator->add('email', new Uniqueness([
'model' => $this,
'message' => $translate->t('Account/Registration.message.unique')
]));
return $this->validate($validator);
}
Phalcon 2.1.0 RC1 Works fine.
@fenixphp
Just wonder if method called as t from translation is some default behaviour in translate component ? Cuz im planning do add validation as annotations to model in 2.2 or 2.3 and i guess option to set translation would be good.
Or it's just some your component and you just define method t ?
@Jurigag
Это класс от "Phalcon\Translate\AdapterInterface" дополнен "array dot notation" и использует в качестве словарей json файлы.
А в целом если передавать в нотации ключ к словарю с переводом то будет очень удобно. При этом для каждой модели определить объект словаря переводов.
translate.google.com
This is the class of the "Phalcon \ Translate \ Adapter Interface" supplemented "array dot notation" and uses as dictionaries json files.
And as a whole if you pass in the notation key to a dictionary with translations will be very convenient. In addition, for each model to determine the object translation dictionary.
@fenixphp
Phalcon 2.1.0 RC1 Works fine.
not work for me. Code:
$validator = new Validation();
$validator->add('phone', new PresenceOf([
'message' => $this->getDI()->get('t')->_('users_phone_validate')
]));
return $this->validate($validator);
I have same error: Phalcon\Validation::add() must implement interface Phalcon\Validation\ValidatorInterface, instance of Phalcon\Mvc\Model\Validator\PresenceOf given
You need to use Phalcon\Validation\Validator\PresenceOf, not Phalcon\Mvc\Model\Validator\PresenceOf
Thanks)
What should I do in 2.0.10?
in my model I have:
use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness;
use Phalcon\Validation\Validator\PresenceOf;
public function validation()
{
$validator = new Validation();
$validator->add('username', new Uniqueness([
'message' => "Value of field 'username' is already present",
]));
$validator->add('username', new PresenceOf([
'message' => "'username' field is required",
]));
$validator->add('password', new PresenceOf([
'message' => "'password' field is required",
]));
return $this->validate($validator);
}
I get following error:
PHP message: PHP Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\Mvc\Model\ValidatorInterface, instance of Phalcon\Validation given in /var/www/oauth/app/models/User.php
On 2.0.10 use just old validation from docs.
use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Validation\Validator\Uniqueness;
class Users extends Model
{
public function validation()
{
$validator = new Validation();
$validator->add(
'email',
new EmailValidator()
);
$validator->add(
'email',
new Uniqueness(['model'=>$this])
);
return $this->validate($validator);
}
}
Same problem with Phalcon 3.0.1. Using Phalcon\Validation\Validator\InclusionIn throws ErrorException with this message: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Validation\Validator\InclusionIn given
You need to pass Object Phalcon\Validation to validate method, NOT VALIDATOR class like Phalcon\Validation\Validator\InclusionIn. Check code above.
Ohh, thanks. Now I see the difference above. May be you should update the Phalcon documentation.
Regards!
It's already updated in docs repo.
But not here: https://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity
Yes because it's need to be updated on server. Like git pull or something.
Just a heads up, I used phalcon-devtools to create my model for me, and in 3.1.2 it used use Phalcon\Mvc\Model\Validator\Email as EmailValidator; instead of use Phalcon\Validation\Validator\Email as EmailValidator; This method seems like devtools created it right though.
/**
* Validations and business logic
*
* @return boolean
*/
public function validation()
{
$validator = new Validation();
$validator->add(
'email',
new EmailValidator(
[
'model' => $this,
'message' => 'Please enter a correct email address',
]
)
);
return $this->validate($validator);
}
'model' => $this, this is not needed, really devtools generate it with this? Needs to fix @sergeyklay
Didn't someone fix this issue? already on latest devtools? You use latest devtools? @n0nag0n
@Jurigag Yup, I used devtools 3.1.2 to create it. I'm new to Phalcon so I rely heavily on dev tools to set things up right.
Still got error !!
Uncaught TypeError: Argument 2 passed to Phalcon\Validation::add() must implement interface Phalcon\Validation\ValidatorInterface, instance of Phalcon\Mvc\Model\Validator\Uniqueness given in F:\xampp\htdocs\phalcon-apimodels\user.php:26
namespace Store\Toys;
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Message;
use Phalcon\Mvc\Model\Validator\InclusionIn;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Validator\Uniqueness as UniquenessValidator;
use Phalcon\Validation\Validator\PresenceOf;
class user extends Model
{
public function validation()
{
$validator = new Validation();
$validator->add(
'email',
new EmailValidator()
);
$validator->add(
'email',
new UniquenessValidator(['model'=>$this])
);
Phalcon\Mvc\Model\Validator\Uniqueness you must use Phalcon\Validation\Validator\Uniqueness
Yes ! i got it Thanks :)
Most helpful comment