What is the proper syntax for describing a <SEL

2019-09-12 22:43发布

问题:

I am using an XML config file to tell Zend_Form what elements I want. I would like to have a <select> element, but I am unsure as to how to add <option> tags using the XML syntax.

Sure I am missing something pretty basic.

Ben

回答1:

Programmatic forms in the ZF only support the parameters type, name and options (not in the meaning of choices but of element settings, like required or label) for the form elements. It is assumed that multiple values will be set dynamically, e.g:

$formConfig = new Zend_Config_Xml('/path/to/form.xml');
$form = new Zend_Form($formConfig);
$form->getElement('myselect')->setMultiOptions($arrayOfOptions);

Of course there's the possibility of actually setting the element options in the XML file using your own name convention (will be ignored by Zend_Form) and then load them from there instead of having the hardcoded or retrieved at runtime, for instance:

<?xml version="1.0" encoding="UTF-8"?>
<form>
    <user>
        <example>
            <name>mysampleform</name>
        <method>post</method>
        <elements>
        <myselect>
                <type>select</type>
                <name>myselect</name>
                <multioptions> <!-- custom tag -->
                    <option value="First">1</option>
                    <option value="Second">2</option>
                    <option value="Third">3</option>
                </multioptions>
                <options>
                    <label>Choose an option:</label>                        
                    <required>true</required>
                </options>
            </myselect>
            <submit>
            <type>submit</type>
            <options>
                <value>Submit</value>
            </options>
            </submit>   
        </elements>    
    </example>
</user>

$formConfig = new Zend_Config_Xml('/path/to/form.xml');
$form = new Zend_Form($formConfig);
$form->getElement('myselect')->setMultiOptions(
    $formConfig->user->example->elements->myselect->multioptions->toArray()
);

Yet it doesn't seem to be more effective than just having those options stored somewhere else.