我在创建Web应用程序方面相当新,并希望寻求有关dojo和zend框架的帮助。我在表单提交上遇到验证问题,并且还需要在单击表单内的按钮(添加新的主持人按钮)时创建动态元素。
我需要的是:表单提交。
If an error occurs during validation show the errors on the
popped up dialog and let user fix the error.
On success redirect the user to the parent page that calls the popup dialog.
以下是我修剪过的代码:
形成class Form_Test
{
public $processed = false;
public function init()
{
parent::init();
$this->setAttribs(array('name'=>'test'));
$this->setAction('/myapp/new')->setMethod('
$this->addElement('ValidationTextBox', 'topic', array(
'label' => 'Topic: ',
'required' => true,
'trim' => true,
'validators' => array("alnum"),
'filters' => array(new Zend_Filter_StringToLower(),
new Zend_Filter_StringTrim()
)
)
);
$this->addElement('SimpleTextArea', 'desc', array(
'label' => 'Description: ',
'trim' => true
)
);
$this->addElement('ValidationTextBox', 'moderator', array(
'label' => 'Moderator: ',
'required' => true,
'trim' => true,
'validators' => array("EmailAddress"),
'filters' => array(new Zend_Filter_StringToLower(),
new Zend_Filter_StringTrim()
)
)
);
$this->addElement('SubmitButton', 'submit', array(
'label' => 'Create'
));
}
}
视图
<button class="myButton" type="button" onclick="dijit.byId('formDialog').show()">
New Topic
</button>
<div dojoType="dijit.Dialog" id="formDialog" title="Topic" style="width:500px; height:300px;">
<?php echo $this->form; ?>
</div>
调节器
public function newAction()
{
$form= new Form_Test();
$this->view->form = $form;
$form->submit->setLabel('Create');
$values = $form->getValues();
if( $this->_request->isPost())
{
if($form->isValid($_POST)){
$topic = new Application_Model_Topic();
$result = $topic->createNewTopic($_POST);
if($result == false){
$form->addError($result->error);
}
}
}
$this->view->form = $form;
// How to redirect to form if there's error?
$this->_redirect('/myapp/index');
}
我见过一些帖子,创建了一个使用ajax的动态元素,但它没有在表单上使用dijit对话框,而且大多数都在jquery中,我也没有任何背景。
我已经在网上搜索但无济于事。请帮帮我。提前谢谢。
答案 0 :(得分:1)
我终于解决了这个问题!见下文......我希望这可以帮助那些遇到同样问题的人。如果您有更优雅的解决方案,请将您的产品放在这里。
在回显表单之前,添加一个div元素,该元素将把错误保存到dijit对话框中。
<div dojoType="dijit.Dialog" id="formDialog" title="Topic" style="width:500px height:300px;">
<div id='form-holder' style="overflow: auto; height: 250px;">
<div id='errors' class='errors'></div>
<?php echo $this->form; ?>
</div>
</div>
检索zend前缀,格式化并附加到创建的div元素。
var xhrArgs = {url: "your controller action",
form: dojo.byId("your form"),
handleAs: "json",
load: function(data) {
if(data['success']==false){
destroyErrorList(); //will remove errors previously set
dojo.place(formatError(data['error']),
dojo.byId("errors"),'last');
}else{
// on success redirect back to the page
window.location = "redirect url";
}
},
error: function(error) {
console.log(error);
}
};
dojo.xhrPost(xhrArgs);
在表单中添加一个隐藏字段,其中包含“版主ID”
$this->addElement('hidden', 'id', array('value' => 1);
在表单中添加preValidation函数,稍后将在提交表单时使用该函数 根据(http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/)blogpost。
public function preValidation(array $data) {
// Search $data for dynamically added fields using findFields callback
foreach ($newFields as $fieldName) {
// strip the id number off of the field name and use it to set new order
}
}
“新主持人”按钮的onClick操作检索隐藏ID并动态创建新文本框以添加主持人电子邮件。
function addModeratorField() {
var id = parseInt(dojo.byId("id").value);
var newTextBox = new dijit.form.TextBox({id:'moderator_'+id, name:'moderator_'+id});
dojo.byId("moderators_holder").appendChild(newTextBox.domNode);
dojo.parser.parse(dojo.byId('moderator_'+id));//dijitize the textbox
// Increment and store id
dojo.byId("id").value = parseInt(id) + 1;
}
在提交表单之前在控制器上,然后对帖子数据执行任何操作
// Form has been submitted - run data through preValidation() to populate the new fields
$form->preValidation($_POST);
if($form->isValid($_POST)){//do something}