好吧,我使用 zend_form 中的 ViewScript 从 edit.phtml 中的 editAction 加载内容,但是当用户单击保存按钮而不更改任何内容时,$form->isValid() 行总是返回 null。
查看我所有的代码:
CadastroRegiao.php(此代码构建所有组件以编辑.pthml 页面)
<?php
类 Cadastros_Form_CadastroRegiao 扩展 Zend_Form {
public function init() {
//Captura o View atual para trabalharmos com seus métodos
$view = Zend_Layout::getMvcInstance()->getView();
$front_controller = Zend_Controller_Front::getInstance();
//Array para removeção de acentos
$caracterComAcento = array("/á/", "/à/", "/â/", "/ã/", "/ä/", "/é/", "/è/", "/ê/", "/ë/", "/í/", "/ì/", "/î/", "/ï/", "/ó/", "/ò/", "/ô/", "/õ/", "/ö/", "/ú/", "/ù/", "/û/", "/ü/", "/ç/"
, "/Á/", "/À/", "/Â/", "/Ã/", "/Ä/", "/É/", "/È/", "/Ê/", "/Ë/", "/Í/", "/Ì/", "/Î/", "/Ï/", "/Ó/", "/Ò/", "/Ô/", "/Õ/", "/Ö/", "/Ú/", "/Ù/", "/Û/", "/Ü/", "/Ç/");
$caracterSemAcento = array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c"
, "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C");
$filterPregReplace = new Zend_Filter_PregReplace();
//Configuração e Inicialização do Componentes
$cod_regiao = new Zend_Form_Element_Hidden('cod_regiao');
$cod_regiao->setName('cod_regiao');
$nome = new Zend_Form_Element_Text('nome');
$nome->setLabel('Nome*: ')
->setRequired(true)
->addFilter($filterPregReplace->setMatchPattern($caracterComAcento)->setReplacement($caracterSemAcento))
->addFilters(array('StripTags', 'StringTrim', 'StringToUpper'))
->addFilter(new Zend_Filter_Alnum(true))
->addFilter(new Zend_Filter_Null(Zend_Filter_Null::ALL))
->addValidators(array('NotEmpty'))
->addValidator(new Zend_Validate_StringLength(array('min' => 2, 'max' => '40')))
->addValidator(new Zend_Validate_Db_NoRecordExists('regiao', 'nome'))
->setAttribs(array('class' => 'upper'))
->setName('nome');
$submit = new Zend_Form_Element_Submit('salvar');
$submit->setName('Salvar');
$cancelar = new Zend_Form_Element_Button('cancelar');
$cancelar->setName('Cancelar')
->setAttribs(array("onClick" => "dialogoConfirm('Cancelar','Deseja Cancelar ?','" . $view->url(array('module' => 'cadastros', 'controller' => 'regiao', 'action' => 'index')) . "');"));
$this->addElements(array($cod_regiao, $nome, $submit, $cancelar));
//Configuração dos Decorators
$this->setDecorators(array(
array('ViewScript', array('viewScript' => "regiao/" . $front_controller->getRequest()->getActionName() . "phtml"))
));
$this->setElementDecorators(array(
'ViewHelper',
array('Errors', array('class' => 'erroValidacao')),
'Description',
'Label'
));
$submit->removeDecorator('label');
$cancelar->removeDecorator('label');
}
}
RegiaoController.php [editAction](这个动作链接了CadastroRegiao.php和edit.html)
看:当用户不更改任何信息时,此代码总是返回“NOT IS VALID”,只是让原始信息保存。
public function editAction() {
$model = new Cadastros_Model_Regiao();
$form = new Cadastros_Form_CadastroRegiao();
if ($this->_request->isPost()) {
if ($form->isValid($this->_request->getPost())) {
$model->update($form->getValues());
$this->_redirect('cadastros/regiao');
}else
echo "NOT IS VALID";
} else {
$regiao = $model->find($this->_getParam('cod_regiao'));
$form->populate($regiao);
$this->view->form = $form;
}
}
最后,我的 edit.phtml
<div id="tabs">
<ul>
<li><a href="#tabs-1">Edição de Região</a></li>
</ul>
<div id="tabs-1">
<div class="msg_aviso">
<img src="<?= $this->baseUrl(); ?>/css/imagens/close.png" width="16" height="16" title="Clique para fechar" alt="Clique para fechar" class="fechar_aviso" />
<span class="title">Aviso !</span>
<p>
1. Todos os caracteres especiais e acentos presentes no campo <b>Nome</b> serão removidos.<br />
</p>
</div>
<form action="" enctype="multipart/form-data" method="post" name="form_cad_regiao">
<?= $this->form->cod_regiao; ?>
<?= $this->form->nome; ?><br />
<br />
<div class="acoes">
<?= $this->form->Salvar; ?>
<?= $this->form->Cancelar; ?>
</div>
</form>
</div>