2015-06-20 129 views
0

我想标题说明了一切。表格是按照它应该呈现的,但是当我决定为它编写一个InputFilter时,一切都开始了。表格仍然呈现,但当然我不得不适应它。然而,现在,发送POST请求(Firebug同意),getPost()方法现在返回false。Zend Framework 2 - 提交表单

行动(控制器):

public function contactAction() { 
    $form = new ContactForm(); 
    $request = $this->getRequest(); 
    if ($request->isPost()) { 
     $vm = new ViewModel(); 
     $vm->setTerminal(true); 
     $form->setData($request->getPost()); 
     if($form->isValid()) { 
      $to = ""; 
      $from = $request->getPost("email"); 
      $name = $request->getPost("nome"); 
      $subject = "Message sent by ".$name." (phone ".$request->getPost("phone").")"; 
      $body = $request->getPost("message"); 
      $mail = new SendEMail($to, $from, $subject, $body); 

      if (!$mail) { 
       $vm->setVariables(array(
         "result" => "Error while sending. Retry." 
       )); 
       return $vm; 
      } else { 
       $vm->setVariables(array(
         "result" => "Thank you! We'll answer as soon as possible." 
       )); 
       return $vm; 
      } 
     } else { 
      $vm->setVariables(array(
       "result" => "Some fields aren't properly fullfilled." 
      )); 
      return $vm; 
     } 
    } else { 
    return new ViewModel(array (
     "welcome" => $this->homeService->findText(), 
     "form" => $form, 
    )); 
    } 
} 

Form类:

<?php 
namespace Site\Form; 

use Zend\Form\Form; 
use Zend\Form\Element; 

class ContactForm extends Form { 
    public function __construct($name=null, $options=array()) { 
     parent::__construct ($name, $options); 

     $this->setAttributes(array(
      "action" => "./", 
     )); 


     $nameInput = new Element\Text("nome"); 
     $nameInput->setAttributes(array(
      "placeholder" => "Nome e cognome", 
      "tabindex" => "1" 
     )); 

     $this->add($nameInput); 

     $emailInput = new Element\Text("email"); 
     $emailInput->setAttributes(array(
      "placeholder" => "Indirizzo e-mail", 
      "tabindex" => "2" 
     )); 

     $this->add($emailInput); 

     $phoneInput = new Element\Text("phone"); 
     $phoneInput->setAttributes(array(
      "placeholder" => "Numero di telefono", 
      "tabindex" => "3", 
     )); 

     $this->add($phoneInput); 

     $messageArea = new Element\Textarea("messaggio"); 
     $messageArea->setAttributes(array(
      "placeholder" => "Scrivi il tuo messaggio", 
      "tabindex" => "4" 
     )); 

     $this->add($messageArea); 

     $submitButton = new Element\Button("submit"); 
     $submitButton 
      ->setLabel("Invia messaggio") 
      ->setAttributes(array(
       "type" => "submit" 
      )); 

     $this->add($submitButton); 

     $resetButton = new Element\Button("reset"); 
     $resetButton 
     ->setLabel("Cancella") 
     ->setAttributes(array(
       "type" => "reset" 
     )); 

     $this->add($resetButton); 

     $this->setInputFilter(new ContactInputFilter()); 
    } 
} 
?> 

输入过滤器类:

<?php 
namespace Site\Form; 

use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\Input; 
use Zend\Validator; 

class ContactInputFilter extends InputFilter { 

    public function init() { 
     $nome = new Input("nome"); 
     $nome->getValidatorChain() 
      ->attach(new Validator\StringLength(3)); 

     $email = new Input("email"); 
     $email->getValidatorChain() 
      ->attach(new Validator\EmailAddress()); 

     $phone = new Input("phone"); 
     $phone->getValidatorChain() 
      ->attach(new Zend\I18n\Validator\PhoneNumber()); 

     $message = new Input("message"); 
     $phone->getValidatorChain() 
      ->attach(new Validator\StringLength(10)); 

     $this->add($nome) 
      ->add($email) 
      ->add($phone) 
      ->add($message); 
    } 

} 
?> 

文档并非如此乐于助人,和类似的主题围绕这里看起来像有一个不同的问题(我没有)。有任何想法吗?谢谢。
EDIT
执行var_dump($ request-> isPost())后,我注意到它返回true,如果它接收到发布数据。然后我不知道现在发生了什么。

回答

0

那么我明白了。由于我的行动是我以前的两个行动的合并,我仍然有一些代码适合不同的模板。问题不是getPost()返回false,而是接收意外数据的视图。现在我的行​​动是:

public function contactAction() { 
    $form = new ContactForm(); 
    $request = $this->getRequest(); 
    $vm = new ViewModel(); 
    $vm->setTerminal(true); 
    $vm->setTemplate("Site\Skeleton\Email"); //here the proper template is set 
    if ($request->isPost()) { 
     $form->setData($request->getPost()); 
     if($form->isValid()) { 
      $to = ""; 
      $from = $request->getPost("email"); 
      $name = $request->getPost("nome"); 
      $subject = "Messaggio inviato da ".$name." (tel. ".$request->getPost("phone").")"; 
      $body = $request->getPost("messaggio"); 
      $mail = new SendEMail($to, $from, $subject, $body); 

      if (!$mail) { 
       $vm->setVariables(array(
         "result" => "Error while sending. Retry." 
       )); 
       return $vm; 
      } else { 
       $vm->setVariables(array(
         "result" => "Thank you! We'll answer as soon as possible." 
       )); 
       return $vm; 
      } 
     } else { 
      $vm->setVariables(array(
       "result" => "Some fields aren't properly fullfilled." 
      )); 
      return $vm; 
     } 
    } else { 
    return new ViewModel(array (
     "welcome" => $this->homeService->findText(), 
     "form" => $form, 
    )); 
    } 
} 

现在我有另一个问题,所以我会转发到另一个线程。