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

An error occurred when model validation

use Phalcon\Mvc\Model\Validator\StringLength as StringLengthValidator;

class Subscriptors extends Phalcon\Mvc\Model
{

public function validation()
{
    $this->validate(new StringLengthValidator(array(
            'field' => 'name_last',
            'max' => 10,
            'min' => 2,
            'messageMaximum' => 'We don\'t like really long names',
            'messageMinimum' => 'We want more than just their initials'
    )));
    if ($this->validationHasFailed() == true) {
            return false;
    }
}
}

This code does not run properly. 'messageMaximum' 'messageMinimum' can not show their own definitions.

The following data is displayed 。 It is not my definitions.

array (size=1)
  0 => 
    object(Phalcon\Mvc\Model\Message)[57]
      protected '_type' => string 'TooLong' (length=7)
      protected '_message' => string 'Value of field 'name' exceeds the maximum 10 characters' (length=55)
      protected '_field' => string 'name' (length=4)
      protected '_model' => null

Works for me.

Context : I fill nothing into the "description" input

bool(false) array(1) { [0]=> object(Phalcon\Mvc\Model\Message)#57 (4) { ["_type":protected]=> string(8) "TooShort" ["_message":protected]=> string(37) "We want more than just their initials" ["_field":protected]=> string(11) "description" ["_model":protected]=> NULL } }

Example controller :

<?php

class SnippetController extends ControllerBase
{
    public function addAction()
    {
        if ($this->request->isPost())
        {
            // Get post values
            $language = $this->request->getPost('language');
            $description = $this->request->getPost('description');
            $code = $this->request->getPost('code');

            // Create new snippet
            $snippet = new Snippets();
            $snippet->language = $language;
            $snippet->description = $description;
            $snippet->code = $code;
            $saved = $snippet->save();

            var_dump($saved);
            echo '<pre>';
            var_dump($snippet->getMessages());

            die();
        }

        // Set languages code into the view
        $this->view->setVar('languages', $this->config->languages->toArray());

    }
}

Example model :

<?php
use Phalcon\Mvc\Model\Validator\StringLength as StringLengthValidator;

class Snippets extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $language;

    /**
     *
     * @var string
     */
    public $description;

    /**
     *
     * @var string
     */
    public $code;

    public function validation()
    {

        $this->validate(new StringLengthValidator(array(
            'field' => 'description',
            'max' => 10,
            'min' => 2,
            'messageMaximum' => 'We don\'t like really long names',
            'messageMinimum' => 'We want more than just their initials'
            )));
        if ($this->validationHasFailed() == true) {
            return false;
        }

    }

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'language' => 'language', 
            'description' => 'description', 
            'code' => 'code'
            );
    }

}


1.3k
Accepted
answer
edited Mar '14

I tested with a long string too :

bool(false)
array(1) {
  [0]=>
  object(Phalcon\Mvc\Model\Message)#57 (4) {
    ["_type":protected]=>
    string(7) "TooLong"
    ["_message":protected]=>
    string(31) "We don't like really long names"
    ["_field":protected]=>
    string(11) "description"
    ["_model":protected]=>
    NULL
  }
}

thanks ! I have found the wrong place . Define the error information should be used "messageMaximum" not messageMaximum 。 What I saw yesterday is 0.7 version of the manual .