У меня возникли трудности с добавлением метода и действия и их отображением.
Я создаю форму, затем извлекаю некоторую информацию о полях, которые должны войти в нее из базы данных, затем добавляю их в форму, а затем отображаю ее. Все это прекрасно работает до того момента, когда я хочу отправить форму, так как нет метода или действия.
Вот код, var_dump и сгенерированный HTML:
public function getForm($id) {
$ar= $this->formElementModel->getElements($id);
//print_r($ar);
$form= new Zend_Form();
$form->setAction('/resource/process')
->setMethod('post');
foreach ($ar as $arr) {
$form->addElement($this->elementModel->getElement($arr['formElementId'], $this->genRandomString(),false));
//$fieldArray[] = ;
}
$form->addElement('submit', 'submit',array('order'=>100))->setMethod('post');
var_dump($form);
// var_dump($fieldArray);
return $form;
}
Вывод var_dump ():
object(Zend_Form)[97]
protected '_attribs' =>
array
'action' => string '/resource/process' (length=17)
'method' => string 'post' (length=4)
protected '_decorators' =>
array
'FormElements' =>
array
'decorator' => string 'FormElements' (length=12)
'options' => null
'HtmlTag' =>
array
'decorator' => string 'HtmlTag' (length=7)
'options' =>
array
...
'Form' =>
array
'decorator' => string 'Form' (length=4)
'options' => null
protected '_defaultDisplayGroupClass' => string 'Zend_Form_DisplayGroup' (length=22)
protected '_description' => null
protected '_disableLoadDefaultDecorators' => boolean false
protected '_displayGroupPrefixPaths' =>
array
empty
protected '_displayGroups' =>
array
empty
protected '_elementDecorators' => null
protected '_elementPrefixPaths' =>
array
empty
protected '_elements' =>
array
'textElement40_yu75r8x22i' =>
object(Zend_Form_Element_Text)[106]
public 'helper' => string 'formText' (length=8)
protected '_allowEmpty' => boolean true
protected '_autoInsertNotEmptyValidator' => boolean true
protected '_belongsTo' => null
protected '_decorators' =>
array
...
protected '_description' => null
protected '_disableLoadDefaultDecorators' => boolean false
protected '_errorMessages' =>
array
...
protected '_errors' =>
array
...
protected '_errorMessageSeparator' => string '; ' (length=2)
protected '_filters' =>
array
...
protected '_ignore' => boolean false
protected '_isArray' => boolean false
protected '_isError' => boolean false
protected '_isErrorForced' => boolean false
protected '_label' => string 'Teksta elements' (length=15)
protected '_loaders' =>
array
...
protected '_messages' =>
array
...
protected '_name' => string 'textElement40_yu75r8x22i' (length=24)
protected '_order' => null
protected '_required' => boolean false
protected '_translator' => null
protected '_translatorDisabled' => boolean false
protected '_type' => null
protected '_validators' =>
array
...
protected '_validatorRules' =>
array
...
protected '_value' => null
protected '_view' => null
protected '_isPartialRendering' => boolean false
'selectElement39_19voehc8bz' =>
object(Zend_Form_Element_Select)[101]
public 'helper' => string 'formSelect' (length=10)
public 'options' =>
array
...
protected '_registerInArrayValidator' => boolean true
protected '_separator' => string '<br />' (length=6)
protected '_translated' =>
array
...
protected '_allowEmpty' => boolean true
protected '_autoInsertNotEmptyValidator' => boolean true
protected '_belongsTo' => null
protected '_decorators' =>
array
...
protected '_description' => string 'Vienlkāršs elementsa' (length=22)
protected '_disableLoadDefaultDecorators' => boolean false
protected '_errorMessages' =>
array
...
protected '_errors' =>
array
...
protected '_errorMessageSeparator' => string '; ' (length=2)
protected '_filters' =>
array
...
protected '_ignore' => boolean false
protected '_isArray' => boolean false
protected '_isError' => boolean false
protected '_isErrorForced' => boolean false
protected '_label' => string 'Dynamic elements' (length=16)
protected '_loaders' =>
array
...
protected '_messages' =>
array
...
protected '_name' => string 'selectElement39_19voehc8bz' (length=26)
protected '_order' => null
protected '_required' => boolean false
protected '_translator' => null
protected '_translatorDisabled' => boolean false
protected '_type' => null
protected '_validators' =>
array
...
protected '_validatorRules' =>
array
...
protected '_value' => null
protected '_view' => null
protected '_isPartialRendering' => boolean false
public 'escape' => boolean false
'textElement37_wz47pigru9' =>
object(Zend_Form_Element_Text)[109]
public 'helper' => string 'formText' (length=8)
protected '_allowEmpty' => boolean true
protected '_autoInsertNotEmptyValidator' => boolean true
protected '_belongsTo' => null
protected '_decorators' =>
array
...
protected '_description' => null
protected '_disableLoadDefaultDecorators' => boolean false
protected '_errorMessages' =>
array
...
protected '_errors' =>
array
...
protected '_errorMessageSeparator' => string '; ' (length=2)
protected '_filters' =>
array
...
protected '_ignore' => boolean false
protected '_isArray' => boolean false
protected '_isError' => boolean false
protected '_isErrorForced' => boolean false
protected '_label' => string 'Nosaukums' (length=9)
protected '_loaders' =>
array
...
protected '_messages' =>
array
...
protected '_name' => string 'textElement37_wz47pigru9' (length=24)
protected '_order' => null
protected '_required' => boolean false
protected '_translator' => null
protected '_translatorDisabled' => boolean false
protected '_type' => null
protected '_validators' =>
array
...
protected '_validatorRules' =>
array
...
protected '_value' => null
protected '_view' => null
protected '_isPartialRendering' => boolean false
'submit' =>
object(Zend_Form_Element_Submit)[98]
public 'helper' => string 'formSubmit' (length=10)
protected '_allowEmpty' => boolean true
protected '_autoInsertNotEmptyValidator' => boolean true
protected '_belongsTo' => null
protected '_decorators' =>
array
...
protected '_description' => null
protected '_disableLoadDefaultDecorators' => boolean false
protected '_errorMessages' =>
array
...
protected '_errors' =>
array
...
protected '_errorMessageSeparator' => string '; ' (length=2)
protected '_filters' =>
array
...
protected '_ignore' => boolean true
protected '_isArray' => boolean false
protected '_isError' => boolean false
protected '_isErrorForced' => boolean false
protected '_label' => null
protected '_loaders' =>
array
...
protected '_messages' =>
array
...
protected '_name' => string 'submit' (length=6)
protected '_order' => int 100
protected '_required' => boolean false
protected '_translator' => null
protected '_translatorDisabled' => boolean false
protected '_type' => null
protected '_validators' =>
array
...
protected '_validatorRules' =>
array
...
protected '_value' => null
protected '_view' => null
protected '_isPartialRendering' => boolean false
protected '_elementsBelongTo' => null
protected '_errorMessages' =>
array
empty
protected '_errorsExist' => boolean false
protected '_errorsForced' => boolean false
protected '_formOrder' => null
protected '_isArray' => boolean false
protected '_legend' => null
protected '_loaders' =>
array
'DECORATOR' =>
object(Zend_Loader_PluginLoader)[107]
protected '_loadedPluginPaths' =>
array
...
protected '_loadedPlugins' =>
array
...
protected '_prefixToPaths' =>
array
...
protected '_useStaticRegistry' => null
'ELEMENT' =>
object(Zend_Loader_PluginLoader)[111]
protected '_loadedPluginPaths' =>
array
...
protected '_loadedPlugins' =>
array
...
protected '_prefixToPaths' =>
array
...
protected '_useStaticRegistry' => null
protected '_methods' =>
array
0 => string 'delete' (length=6)
1 => string 'get' (length=3)
2 => string 'post' (length=4)
3 => string 'put' (length=3)
protected '_order' =>
array
'textElement40_yu75r8x22i' => null
'selectElement39_19voehc8bz' => null
'textElement37_wz47pigru9' => null
'submit' => int 100
protected '_orderUpdated' => boolean true
protected '_subFormPrefixPaths' =>
array
empty
protected '_subForms' =>
array
empty
protected '_translator' => null
protected '_translatorDisabled' => boolean false
protected '_view' => null
protected '_isRendered' => boolean false
и сгенерированный HTML:
<form id="fZvN9un1xn4Uh9yZzgWOi9NluFf6CiOuFWH6ugVQSkapm2SV2Jb">
<dl class="zend_form">
<dt id="textElement40_yu75r8x22i-label"><label for="textElement40_yu75r8x22i" class="optional">Teksta elements</label></dt>
<dd id="textElement40_yu75r8x22i-element">
<input name="textElement40_yu75r8x22i" id="textElement40_yu75r8x22i" value="" type="text"></dd>
<dt id="selectElement39_19voehc8bz-label"><label for="selectElement39_19voehc8bz" class="optional">Dynamic elements</label></dt>
<dd id="selectElement39_19voehc8bz-element">
<select name="selectElement39_19voehc8bz" id="selectElement39_19voehc8bz">
<option value="sdffdsf" label="Nekāda sudfdfdss s s nebūs! - newText26">Nekāda sudfdfdss s s nebūs! - newText26</option>
<option value="newName21" label="newTitle22 - newText23">newTitle22 - newText23</option>
</select>
<p class="description">Vienlkāršs elementsa</p></dd>
<dt id="textElement37_wz47pigru9-label"><label for="textElement37_wz47pigru9" class="optional">Nosaukums</label></dt>
<dd id="textElement37_wz47pigru9-element">
<input name="textElement37_wz47pigru9" id="textElement37_wz47pigru9" value="" type="text"></dd>
<dt id="submit-label"> </dt><dd id="submit-element">
<input name="submit" id="submit" value="submit" type="submit"></dd></dl></form>
Скрипт контроллера
public function viewAction($formId=NULL) {
$id = $formId;
if ($formId === NULL) {
$id = $this->_getParam('id');
}
$form=$this->formModel->getForm($id);
if ($this->getRequest()->isPost()) {
print_r($this->_getAllParams());
}
$this->view->fields =$form ->render();
}
Справка?!
[EDIT]
Просто обнаружил что-то странное.
Если я создаю форму внутри контроллера и добавляю туда поля, она отображается нормально. Если я передам Форму в Модель и обратно, метод и действие исчезнут, хотя это было раньше.
Я попытаюсь создать новую форму в своем собственном файле, динамически добавить поля в файл формы и затем вернуть ее.
[EDIT2]
Просто узнал что-то еще, это странно.
Когда я повторяю форму прямо из контроллера, он работает просто отлично. Если я назначу его переменной представления, а затем отобразлю его в представлении, это не сработает.