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 with PresenceOf not working

Hi there,

I'm trying to validate some data before writing it into my db ! I post my data with jquery´s post method to my controller. In the controller I am building a new instance of customer = new Customer <-- the name of my table and class !

In my Customer class I do :

use \Phalcon\Mvc\Model\Validator;
...bla bla bla
public function validation()
    {
        $this->validate(new Validator\PresenceOf([
            'field' => 'last_name',
            'message' => 'And here my own message thing'
        ]));
   }

and that is doing nothing ! The default validation is happening, but without my own messages. If i try the same with "Validator\Email" it works fine. What i´m doing wrong ?

Ok i figured out that if i set the "field" can be NULL in my table the validation works ?!?! Could anyone explain that to me ?? Or is there a way to manipulate the "original" validation message if the field can not be NULL ?



2.6k
Accepted
answer

Sorry for digging up the topic.

@Marc Trosekn:

The solution to your problem is to use initialize() method in your model with a setup array:

class Customers extends \Phalcon\Mvc\Model {

  public function initialize(){

    $this->setup(
        array('notNullValidations'=>false)
    );

  }

}

This way the Phalcon will NOT perform a standard PresenceOf validation which have these user-unfriendly default message "XXX is required". Instead Phalcon WILL perform your own PresenceOf validation with your own message. But you need to define it as you do.

Another way to do this is:

\Phalcon\Mvc\Model::setup(array(    
    'notNullValidations' => false
));

The second option will take affect to all models - as I understand it - I've just found the solution, have the same problem and ware digging...

It's described here:

edited Mar '15

We are using the propel ORM along with the phalcon framework and are facing the same issue. This solution is not relevant to us since we do not have phalcon ORM classes.

Some additional info

Code:

<?php
namespace Modules\Frontend\Forms\Application;

use Library\Form\BaseForm;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;

class ApplicationMessageForm extends BaseForm
{
    const TARGET_METHOD = 'POST';

    const MESSAGE = 'message';

    /**
     *
     * @return array|void
     */
    public function initialize()
    {
        if($this->getUserOptions() && isset($this->getUserOptions()['target_action'])) {
            $this->setAction($this->getDI()->get('url')->get($this->getUserOptions()['target_action']));
        }
        // email field
        $attributes = ['rows' => 6];
        $messageField = new TextArea(self::MESSAGE, $attributes);
        //$messageField->setLabel(48'label.' . self::MESSAGE));
        $messageField->addValidators(
            array(
                new PresenceOf(array("message" => $this->t('please.enter.a.message'), "cancelOnFail" => true)),
                new StringLength(array(
                        'max' => 50,
                        'min' => 5,
                        'messageMaximum' => 'We don\'t like really long names',
                        'messageMinimum' => 'We want more than just their initials'
                    )
                )
            )
        );

        $messageField->setFilters(array("trim"));
        $this->add($messageField);

        // submit field
        $this->add(
            new Submit(
                "submit", array(
                    "value" => "Send",
                    "class" => "btn btn-info pull-right everjobs-btn"
                )
            )
        );
    }

}

SQL data looks as following:

+------------------------+---------+--------------------+-----------------------------+---------------------+---------------------+
| id_application_message | fk_user | fk_job_application | text                        | created_at          | updated_at          |
+------------------------+---------+--------------------+-----------------------------+---------------------+---------------------+
|                      1 |   41671 |                  1 | lorem ipusum                | 2015-03-17 10:06:19 | 2015-03-17 10:06:19 |
|                      2 |   41671 |                  2 | dev                         | 2015-03-17 10:52:31 | 2015-03-17 10:52:31 |
|                      3 |   41673 |                  2 | lorem ipusum toler del amon | 2015-03-17 11:37:47 | 2015-03-17 11:37:47 |
|                      4 |   41673 |                  2 |                             | 2015-03-17 20:03:03 | 2015-03-17 20:03:03 |
|                      5 |   41673 |                  2 |                             | 2015-03-17 20:03:07 | 2015-03-17 20:03:07 |
|                      6 |   41673 |                  2 |                             | 2015-03-17 20:30:38 | 2015-03-17 20:30:38 |
|                      7 |   41673 |                  2 |                             | 2015-03-17 21:10:36 | 2015-03-17 21:10:36 |
|                      8 |   41673 |                  2 |                             | 2015-03-17 21:10:37 | 2015-03-17 21:10:37 |
|                      9 |   41673 |                  2 |                             | 2015-03-18 09:36:33 | 2015-03-18 09:36:33 |
|                     10 |   41671 |                  2 | thanks                      | 2015-03-18 09:43:54 | 2015-03-18 09:43:54 |
|                     11 |   41673 |                  2 |                             | 2015-03-18 10:09:56 | 2015-03-18 10:09:56 |
|                     12 |   41673 |                  2 |                             | 2015-03-18 10:40:46 | 2015-03-18 10:40:46 |
|                     13 |   41673 |                  2 | fdsfdsfds                   | 2015-03-18 10:41:08 | 2015-03-18 10:41:08 |
|                     14 |   41673 |                  2 | fdsfds                      | 2015-03-18 10:41:19 | 2015-03-18 10:41:19 |
|                     15 |   41673 |                  2 | fd                          | 2015-03-18 10:55:37 | 2015-03-18 10:55:37 |
|                     16 |   41673 |                  2 |                             | 2015-03-18 11:00:19 | 2015-03-18 11:00:19 |
|                     17 |   41673 |                  2 |                             | 2015-03-18 11:16:50 | 2015-03-18 11:16:50 |
+------------------------+---------+--------------------+-----------------------------+---------------------+---------------------+

e.g empty strings and not NULL

Thanks!