What is the proper syntax for describing a <SELECT> form element to Zend_Form using XML as the config?

时光毁灭记忆、已成空白 提交于 2019-12-24 19:23:09

问题


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.



来源:https://stackoverflow.com/questions/2876344/what-is-the-proper-syntax-for-describing-a-select-form-element-to-zend-form-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!