phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

[BUG]: IncludedMaximum & IncludedMinimum behavior inverted for StringLength, Min, Max validation #16560

Closed oyeaussie closed 7 months ago

oyeaussie commented 7 months ago

Expected behavior The includedMinimum and includedMaximum in stringlength validation, when set to "true", should compare the string length with operators <= & >=, but instead they compare it with < & >

Workaround is to switch them to "false"

Steps to reproduce the behavior:

$data = [];
$data['testMin'] = '12345678';
$data['testMax'] = '12345678901234567890';
$this->validation->add(
    'testMin',
    StringLength::class,
        [
            "min"             => 8,
            "max"             => 20,
            "messageMinimum"  => "minimum length requirement failed.",
            "messageMaximum"  => "maximum length requirement failed.",
            "includedMinimum" => true,
            "includedMaximum" => true
        ]
    );
$this->validation->add(
    'testMax',
    StringLength::class,
        [
            "min"             => 8,
            "max"             => 20,
            "messageMinimum"  => "minimum length requirement failed.",
            "messageMaximum"  => "maximum length requirement failed.",
            "includedMinimum" => true,
            "includedMaximum" => true
        ]
    );

$validated = $this->validation->validate($data)->jsonSerialize();
var_dump($this->validation);die();

object(Validation)[211]
  protected 'validator' => 
    object(Phalcon\Filter\Validation)[390]
      protected 'container' => null
      protected 'combinedFieldsValidators' => 
        array (size=0)
          empty
      protected 'data' => 
        array (size=2)
          'testMin' => string '12345678' (length=8)
          'testMax' => string '12345678901234567890' (length=20)
      protected 'entity' => null
      protected 'filters' => 
        array (size=0)
          empty
      protected 'labels' => 
        array (size=0)
          empty
      protected 'messages' => 
        object(Phalcon\Messages\Messages)[385]
          protected 'position' => int 0
          protected 'messages' => 
            array (size=2)
              0 => 
                object(Phalcon\Messages\Message)[386]
                  protected 'code' => int 0
                  protected 'field' => string 'testMin' (length=7)
                  protected 'message' => string 'minimum length requirement failed.' (length=34)
                  protected 'type' => string 'Phalcon\Filter\Validation\Validator\StringLength\Min' (length=52)
                  protected 'metaData' => 
                    array (size=0)
                      empty
              1 => 
                object(Phalcon\Messages\Message)[389]
                  protected 'code' => int 0
                  protected 'field' => string 'testMax' (length=7)
                  protected 'message' => string 'maximum length requirement failed.' (length=34)
                  protected 'type' => string 'Phalcon\Filter\Validation\Validator\StringLength\Max' (length=52)
                  protected 'metaData' => 
                    array (size=0)
                      empty
      protected 'validators' => 
        array (size=2)
          'testMin' => 
            array (size=1)
              0 => 
                object(Phalcon\Filter\Validation\Validator\StringLength)[219]
                  protected 'template' => null
                  protected 'templates' => 
                    array (size=0)
                      empty
                  protected 'options' => 
                    array (size=0)
                      empty
                  protected 'validators' => 
                    array (size=2)
                      0 => 
                        object(Phalcon\Filter\Validation\Validator\StringLength\Min)[394]
                          protected 'template' => string 'minimum length requirement failed.' (length=34)
                          protected 'templates' => 
                            array (size=0)
                              empty
                          protected 'options' => 
                            array (size=3)
                              'min' => int 8
                              'message' => string 'minimum length requirement failed.' (length=34)
                              'included' => boolean true
                      1 => 
                        object(Phalcon\Filter\Validation\Validator\StringLength\Max)[410]
                          protected 'template' => string 'maximum length requirement failed.' (length=34)
                          protected 'templates' => 
                            array (size=0)
                              empty
                          protected 'options' => 
                            array (size=3)
                              'max' => int 20
                              'message' => string 'maximum length requirement failed.' (length=34)
                              'included' => boolean true
          'testMax' => 
            array (size=1)
              0 => 
                object(Phalcon\Filter\Validation\Validator\StringLength)[398]
                  protected 'template' => null
                  protected 'templates' => 
                    array (size=0)
                      empty
                  protected 'options' => 
                    array (size=0)
                      empty
                  protected 'validators' => 
                    array (size=2)
                      0 => 
                        object(Phalcon\Filter\Validation\Validator\StringLength\Min)[249]
                          protected 'template' => string 'minimum length requirement failed.' (length=34)
                          protected 'templates' => 
                            array (size=0)
                              empty
                          protected 'options' => 
                            array (size=3)
                              'min' => int 8
                              'message' => string 'minimum length requirement failed.' (length=34)
                              'included' => boolean true
                      1 => 
                        object(Phalcon\Filter\Validation\Validator\StringLength\Max)[392]
                          protected 'template' => string 'maximum length requirement failed.' (length=34)
                          protected 'templates' => 
                            array (size=0)
                              empty
                          protected 'options' => 
                            array (size=3)
                              'max' => int 20
                              'message' => string 'maximum length requirement failed.' (length=34)
                              'included' => boolean true
      protected 'values' => 
        array (size=2)
          'testMin' => string '12345678' (length=8)
          'testMax' => string '12345678901234567890' (length=20)

Details

Directive => Local Value => Master Value phalcon.db.escape_identifiers => On => On phalcon.db.force_casting => Off => Off phalcon.orm.case_insensitive_column_map => Off => Off phalcon.orm.cast_last_insert_id_to_int => Off => Off phalcon.orm.cast_on_hydrate => Off => Off phalcon.orm.column_renaming => On => On phalcon.orm.disable_assign_setters => Off => Off phalcon.orm.enable_implicit_joins => On => On phalcon.orm.enable_literals => On => On phalcon.orm.events => On => On phalcon.orm.exception_on_failed_save => Off => Off phalcon.orm.exception_on_failed_metadata_save => On => On phalcon.orm.ignore_unknown_columns => Off => Off phalcon.orm.late_state_binding => Off => Off phalcon.orm.not_null_validations => On => On phalcon.orm.resultset_prefetch_records => 0 => 0 phalcon.orm.update_snapshot_on_save => On => On phalcon.orm.virtual_foreign_keys => On => On phalcon.orm.dynamic_update => On => On phalcon.warning.enable => On => On `

niden commented 7 months ago

Resolved in https://github.com/phalcon/cphalcon/pull/16561

Thank you @oyeaussie for reporting this.

oyeaussie commented 4 months ago

Hello @niden

This doesn't seem like its fixed in version 5.7.x

It seems that your fix was reverted: https://github.com/phalcon/cphalcon/commits/5.0.x/phalcon/Filter/Validation/Validator/StringLength/Max.zep

Can you please confirm.

Thanks, Guru.