Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

php - Zend Framework 2 - Submitting a form

I suppose title says everything. The form is rendered as it should, but everything begun when i decided to write an InputFilter for it. The form is still rendered, but of course i had to adapt something around it. Now, however POST request is sent (Firebug agrees), the getPost() method returns false now.

Action (in the controller):

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 class:

<?php
namespace SiteForm;

use ZendFormForm;
use ZendFormElement;

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

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


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

        $this->add($nameInput);

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

        $this->add($emailInput);

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

        $this->add($phoneInput);

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

        $this->add($messageArea);

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

        $this->add($submitButton);

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

        $this->add($resetButton);

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

InputFilter class:

<?php
namespace SiteForm;

use ZendInputFilterInputFilter;
use ZendInputFilterInput;
use ZendValidator;

class ContactInputFilter extends InputFilter {

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

        $email = new Input("email");
        $email->getValidatorChain()
            ->attach(new ValidatorEmailAddress());

        $phone = new Input("phone");
        $phone->getValidatorChain()
            ->attach(new ZendI18nValidatorPhoneNumber());

        $message = new Input("message");
        $phone->getValidatorChain()
            ->attach(new ValidatorStringLength(10));

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

}
?>

Documentation isn't being so helpful, and similar threads around here look like having had a different problem (which i don't have). Any ideas? Thank you.
EDIT
After performing a var_dump($request->isPost()) i noticed that it returns true if it receives post data. Then i don't know what is really happening now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well i got the point. Since my action was a merge of 2 previous actions i had, i still had some code that was good for a different template. The problem wasn't getPost() returning false, but the view receiving unexpected data. Now my action is:

public function contactAction () {
    $form = new ContactForm();
    $request = $this->getRequest();
    $vm = new ViewModel();
    $vm->setTerminal(true);
    $vm->setTemplate("SiteSkeletonEmail"); //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,
    ));
    }
}

Now i have another problem, so i'll forward to another thread.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...