Xtreamwayz\HTMLFormValidator\FormFactory::asString PHP Method

asString() public method

public asString ( Xtreamwayz\HTMLFormValidator\ValidationResultInterface $result = null )
$result Xtreamwayz\HTMLFormValidator\ValidationResultInterface
    public function asString(ValidationResultInterface $result = null)
    {
        if ($result) {
            // Inject data if a result is set
            $this->setData($result->getValues());
            $this->setMessages($result->getMessages());
        }
        // Always remove form validator specific attributes before rendering the form
        // to clean it up and remove possible sensitive data
        foreach ($this->getNodeList() as $name => $node) {
            $node->removeAttribute('data-reuse-submitted-value');
            $node->removeAttribute('data-input-name');
            $node->removeAttribute('data-validators');
            $node->removeAttribute('data-filters');
        }
        $this->document->formatOutput = true;
        // Return the first form only to prevent returning the XML declaration
        return $this->document->saveHTML($this->document->getElementsByTagName('form')->item(0));
    }

Usage Example

Exemplo n.º 1
1
 public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
 {
     /* @var \PSR7Session\Session\SessionInterface $session */
     $session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);
     // Generate csrf token
     if (!$session->get('csrf')) {
         $session->set('csrf', md5(random_bytes(32)));
     }
     // Generate form and inject csrf token
     $form = new FormFactory($this->template->render('app::contact-form', ['token' => $session->get('csrf')]), $this->inputFilterFactory);
     // Validate form
     $validationResult = $form->validateRequest($request);
     if ($validationResult->isValid()) {
         // Get filter submitted values
         $data = $validationResult->getValues();
         $this->logger->notice('Sending contact mail to {from} <{email}> with subject "{subject}": {body}', $data);
         // Create the message
         $message = new Message();
         $message->setFrom($this->config['from'])->setReplyTo($data['email'], $data['name'])->setTo($this->config['to'])->setSubject('[xtreamwayz-contact] ' . $data['subject'])->setBody($data['body']);
         $this->mailTransport->send($message);
         // Display thank you page
         return new HtmlResponse($this->template->render('app::contact-thank-you'), 200);
     }
     // Display form and inject error messages and submitted values
     return new HtmlResponse($this->template->render('app::contact', ['form' => $form->asString($validationResult)]), 200);
 }
All Usage Examples Of Xtreamwayz\HTMLFormValidator\FormFactory::asString