I want to use a field that the user entered in a form, in another field using a regular function:
I want to use the result of function addAppId() in the Regex of the other field addDbName()
So I want to know if it is possible to save the result of the first function in a variable and use it in the second function (of my form).
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]/',
),
));
}
If I get it right, you want to validate the field
db_name, using a regex expression, which have to containt theapp_idvalue.If so, the answer is to use the
Callbackvalidator. This is an exemple: