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

Phalcon Validation

I want to validate some parameter in my code, I wanna be sure that params value is an integer. I know that in Phalcon I have build in mechanism that allow me to validate params.

e.g:

class NumberValidator extends Validation
{
    public function initialize()
    {
        $this->add('number', new RegexValidator(
                array(
                    'pattern' => '^[0-9]+$',
                    'message' => 'Should be an integer number.'
                )
            )
        );
    }
}

now if I want validate that my variable $number is a number I should execute:

$numberVal = new Validators\NumberValidator();
$numberVal->validate(array('number' => $myParam));

My question is: Is there a way to validate only the parameter value without providing a parameter name. For example, if I want to be sure that the value of my parameter (any parameter) is integer I will execute:

<?php

$numberVal = new Validators\NumberValidator();
$numberVal->validate($myParam);


98.9k
Accepted
answer
edited Jul '14

You can create a plain class to do that:

namespace Validators;

class NumberValidator
{
    public function validate($value)
    {
        return is_numeric($value);
    }
}

However, If you plan to use this validator among Phalcon\Validation, the validator needs to know which field must be validated:

<?php

namespace Validators;

use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Message;

class NumberValidator extends Validator implements ValidatorInterface
{

    /**
     * Executes the validation
     *
     * @param Phalcon\Validation $validator
     * @param string $attribute
     * @return boolean
     */
    public function validate($validator, $attribute)
    {
        $value = $validator->getValue($attribute);
        if (!is_numeric($value)) {
            $validator->appendMessage(new Message('The number is not valid', $attribute, 'Number'));
            return false;           
        }
        return true;
    }

}

how the source code to set autoincrement in the framework Phalcon ..?? :D

You can create a plain class to do that:

namespace Validators;

class NumberValidator
{
  public function validate($value)
  {
      return is_numeric($value);
  }
}

However, If you plan to use this validator among Phalcon\Validation, the validator needs to know which field must be validated:

<?php

namespace Validators;

use Phalcon\Validation\Validator,
  Phalcon\Validation\ValidatorInterface,
  Phalcon\Validation\Message;

class NumberValidator extends Validator implements ValidatorInterface
{

  /**
   * Executes the validation
   *
   * @param Phalcon\Validation $validator
   * @param string $attribute
   * @return boolean
   */
  public function validate($validator, $attribute)
  {
      $value = $validator->getValue($attribute);
      if (!is_numeric($value)) {
          $validator->appendMessage(new Message('The number is not valid', $attribute, 'Number'));
          return false;           
      }
      return true;
  }

}

sorry Can you show the data to be validated.? like what format it is?

You can Extend the class Numericality like this and then use it as below:

/* *
 * File name: Numericality.php created at 25/08/2020
 * Created with PhpStorm on Clickmedia.es at 21:58:03
 * License: See License file
 */

class Numericality extends \Phalcon\Validation\Validator\Numericality {

    private $params;

    public function __construct(array $options = []){
        $this->params = $options;
        parent::__construct($options);
    }

    public function validate(\Phalcon\Validation $validation, $field): bool{

        $isValid = true;

        if(isset($this->params['min']) && is_numeric($this->params['min'])){
            if($validation->getValue($field) < $this->params['min']){
                $validation->appendMessage(
                    new Message($this->params['minMessage'], $field, self::class)
                );

                $isValid = false;
            }
        }

        if(isset($this->params['max']) && is_numeric($this->params['max'])){
            if($validation->getValue($field) > $this->params['max']){
                $validation->appendMessage(
                    new Message($this->params['maxMessage'], $field, self::class)
                );

                $isValid = false;
            }
        }

        return $isValid ? parent::validate($validation, $field) : $isValid;
    }
}

Example of use:

$items->addValidator(new Numericality([
            'min' => 1,
            'max' => 50,
            'message' => _('It mus be a number'),
            'minMessage' => _('It must be greater than 1'),
            'maxMessage' => _('It must be less than 50')
        ]));