Home  >  Q&A  >  body text

PHP Laminas DoctrineObjectInputFilter Gets the value of other properties in the callback input filter

I am using Laminas DoctrineObjectInputFilter and want to get the value of other properties in the callback input filter like this code is in the init function of the Filter class which extends DoctrineObjectInputFilter

// input filter whose value is required
$this->add([     
        'name' => 'name',
        'allow_empty' => false,
        'filters' => []
]);
// Input filter in which I want value of input name
$this->add([
        'name' => 'value',
        'allow_empty' => true,
        'filters' => [
            [
                'name' => 'Callback',
                'options' => [
                    'callback' => function ($value) {
                        $name = // want to get property name value here

                        if (key_exists($name, $this->applicationConfig) && gettype($value) === 'string') {
                            return trim(strip_tags($value));
                          }
                          else {
                              return trim($value);
                          }

                        return $value;
                    },
                ],
            ],
        ],
    ]);

Checked $this->getRawValues() but it returns null for all inputs.

P粉883278265P粉883278265178 days ago376

reply all(1)I'll reply

  • P粉842215006

    P粉8422150062024-03-29 13:22:50

    A bit late, but I guess you are searching for $context. Since the value of name is in the same InputFilter instance, you only need to use $context in the callback function.

    add([
                'name' => 'name',
                'required' => true,
                'allow_empty' => false,
                'filters' => [
                    [ 'name' => StripTags::class ],
                    [ 'name' => StringTrim::class ],
                    [
                        'name' => ToNull::class,
                        'options' => [
                            'type' => ToNull::TYPE_STRING,
                        ],
                    ],
                ],
            ]);
    
            $this->add([
                'name' => 'value',
                'required' => true,
                'allow_empty' => true,
                'filters' => [],
                'validators' => [
                    [
                        'name' => Callback::class,
                        'options' => [
                            'callback' => function($value, $context) {
                                $name = $context['name'];
                                ...
                            },
                        ],
                    ],
                ],
            ]);
        }
    }

    reply
    0
  • Cancelreply