Zend Form:使用用户在另一个字段中输入的字段的结果

时间:2019-05-07 08:14:59

标签: php zend-framework2 zend-form zend-framework3

我想使用用户以表单形式输入的字段,在另一个使用常规功能的字段中使用:

我想在其他字段function addAppId()的正则表达式中使用addDbName()的结果

所以我想知道是否有可能将第一个函数的结果保存在变量中,并在第二个函数(我的表单)中使用它。

protected function addAppId()
{
    $this->add(array(
        'name' => 'app_id',
        'type' => 'Zend\Form\Element\Select',
        'options' => array(
            'label' => 'App Name',
            'label_size' => 4,
            'elm_size' => 8,
            'empty_option' => __('---Selectionner une Application---'),
            'value_options' => array()
        ),
        'input_filter' => array(
            'required' => true,
            'filters' => array(
                new \Zend\Filter\StripTags(),
                new \Zend\Filter\StringTrim(),
            ),
            'validators' => array(
                new \Zend\Validator\GreaterThan(array(
                    'min' => 0,
                )),
                new \Zend\I18n\Validator\IsInt(),
            ),
        ),
    ));
}

protected function addDbName()
    {

        $this->add(array(
            'name' => 'db_name',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => 'Database Name',               
            ),
.............

                    new \Zend\Validator\Regex(array(
                        'pattern'=> '/^[I_WANT_TO_ADD_THE_VARIABLE_HERE]/',

            ),
        ));
    }

1 个答案:

答案 0 :(得分:0)

如果我做对了,您想使用正则表达式来验证字段db_name,该字段必须包含app_id值。

如果是,答案是使用Callback验证程序。 这是一个例子:

/**
 * This must be populated in the constructor or in the init function,
 * however before calling addAppId()
 *
 * @var array
 */
protected $appId = array(
    1 => array(
        'name' => 'Wordpress',
        'dbPrefix' => 'wp'
    ),
    2 => array(
        'name' => 'Magento',
        'prefix' => 'magento'
    ),
    3 => array(
        'name' => 'My wonderful app',
        'prefix' => 'my_wonderful_app'
    )
);

protected function addAppId() {
    $valueOptions = [];
    foreach($this->appId as $appId => $app){
        $valueOptions[$appId] = $app['name'];
    }
    $this->add(array(
        'name' => 'app_id',
        'type' => 'Zend\Form\Element\Select',
        'options' => array(
            'label' => 'App Name',
            'value_options' => $valueOptions
        )
    ));

}

protected function addDbName() {
    $prefixes = [];
    foreach($this->appId as $appId => $app){
        $prefixes[$appId] = $app['prefix'];
    }
    $this->add(array(
        'name' => 'db_name',
        'type' => 'Zend\Form\Element\Text',
        'options' => array(
            'label' => 'Database Name',
        ),
        'validators' => array(
            array(
                'name' => 'Callback',
                'options' => [
                    'callback' => function($value, $context) use ($prefixes) {
                        if(!isset($context['app_id'])){
                            return false;
                        }
                        $appId = $context['app_id'];
                        if(!isset($prefixes[$appId])){
                            return false;
                        }

                        // If you want to check that the app_id name is the
                        // prefix of the db, pattern should be:
                        // $pattern = '/^'.$appId.'.*/';
                        $pattern = '/^' . $prefixes[$appId] . '/';

                        $status = preg_match($pattern, $value);
                        return $status !== '' && $status === false;
                    },
                    'messages' => [
                        \Zend\Validator\Callback::INVALID_VALUE => "Database name doesn't match the App name"
                    ]
                ]
            )
        )
    ));
}
}