PHP Laminas DoctrineObjectInputFilter Ruft den Wert anderer Eigenschaften im Callback-Eingabefilter ab
P粉883278265
P粉883278265 2024-03-28 17:17:08
0
1
437

Ich verwende Laminas DoctrineObjectInputFilter 并希望获取回调输入过滤器中其他属性的值,就像此代码位于 Filter 类的 init 函数中,该类扩展了 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;
                    },
                ],
            ],
        ],
    ]);

Überprüft $this->getRawValues(), aber es gibt für alle Eingaben Null zurück.

P粉883278265
P粉883278265

Antworte allen(1)
P粉842215006

有点晚了,但我猜您正在搜索$context。由于 name 的值位于同一个 InputFilter 实例中,因此您只需在回调函数中使用 $context 即可。

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'];
                            ...
                        },
                    ],
                ],
            ],
        ]);
    }
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage