cphalcon: Regex validation pattern doesn't work after upgrade to 3

Expected and Actual Behavior

The following Regex validator pattern worked fine before I upgraded to version 3:

'pattern' =>  '/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/'

Also the new date validator doesn’t produce an error message 'format' => 'd.m.Y'.

Example

/*
* Duration
 */
$duration = new Text("duration", array(
        'class' => 'form-control',
        'id' => 'duration'
        ));
                $duration->setLabel('Dauer');
                $duration->setAttribute('autocomplete', 'off');
                $duration->addValidator(new Regex(array(
                        'pattern' => '/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/',
                        'message' => 'Falsche oder fehlende Dauer.'
                )
));
$this->add($duration);

Details

  • Phalcon version: 3
  • PHP Version: 5.6
  • Operating System: Ubuntu
  • Server: Apache

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 25 (12 by maintainers)

Most upvoted comments

Just change their code to return actual values, not changed and use afterFetch/afterSave if you need formatted values.

Or remove them totally because i don’t know why you need getters/setters with public properties, just decide - protected properties and getters - or public properties and no getters. there is no point in having both public.

What you mean before 3 ? Code in 2.1.x is exactly the same. In 2.0.x it’s diffrent and validation is done on data form, not on model - https://github.com/phalcon/cphalcon/blob/2.0.x/phalcon/validation.zep#L77. That’s why it was working. Now it isn’t beacause your getters returns correct values which are passing validation. The same will happen however if you will even use validation() in model. Just wrong php code.

$duration = new Text("duration", array(
    'class' => 'form-control',
    'id' => 'duration'
));
$duration->setLabel('Dauer');
$duration->setAttribute('autocomplete', 'off');
$duration->addValidator(new Regex(array(
        'pattern' => '/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/',
        'message' => 'Falsche oder fehlende Dauer.'
    )
));
$duration->addValidator(new Phalcon\Validation\Validator\Date([
        'format' => 'd.m.Y',
        'message' => 'Falsche oder fehlende Dauer 2.'
    ]
));
$form = new \Phalcon\Forms\Form();
$form->add($duration);
var_dump($form->isValid(['duration'=>'xyz']));
var_dump($form->getMessages());
bool(false) object(Phalcon\Validation\Message\Group)#30 (2) { ["_position":protected]=> int(0) ["_messages":protected]=> array(2) { [0]=> object(Phalcon\Validation\Message)#34 (4) { ["_type":protected]=> string(5) "Regex" ["_message":protected]=> string(28) "Falsche oder fehlende Dauer." ["_field":protected]=> string(8) "duration" ["_code":protected]=> int(0) } [1]=> object(Phalcon\Validation\Message)#35 (4) { ["_type":protected]=> string(4) "Date" ["_message":protected]=> string(30) "Falsche oder fehlende Dauer 2." ["_field":protected]=> string(8) "duration" ["_code":protected]=> int(0) } } }

Looks like working for me. Phalcon 3.0.0.

Without message in date validator i have Field duration is not a valid date as well.