Create advanced, consistent and secure forms and validations.
Start by creating a new model file e.g. "app/Models/Forms/ContactForm.php" naming is completely up to you. The open the file and add the content bellow:
Then you just need to build the ContactForm Modal inside your controller. Create the Controller bellow "app/Http/Controllers/ContactPage.php" and add the content bellow.
<?php
namespace Http\Controllers\Examples;
use MaplePHP\Http\Interfaces\ResponseInterface;
use MaplePHP\Http\Interfaces\RequestInterface;
use Http\Controllers\BaseController;
use MaplePHP\Foundation\Http\Provider;
use MaplePHP\Foundation\Form\Validate;
use Models\Forms\ContactForm;
class ContactPage extends BaseController
{
protected $form;
protected $validate;
public function __construct(Provider $provider, ContactForm $form, Validate $validate)
{
$this->form = $form;
$this->validate = $validate;
}
/**
* Build the contact form
* @param ResponseInterface $response PSR-7 Response
* @param RequestInterface $request PSR-7 Request
* @return void
*/
public function contactUs(ResponseInterface $response, RequestInterface $request)
{
// This will build the form
$this->form->build();
// Set partial template view
$this->view()->setPartial("form", [
"tagline" => "Contact us",
"name" => "Lorem ipsum dolor",
"content" => "Lorem ipsum dolor sit amet.",
"form" => [
"method" => "post",
"action" => $this->url()->getUrl(),
"form" => $this->form,
"submit" => "Send"
]
]);
}
/**
* POST: Validate the contact form
* @param ResponseInterface $response PSR-7 Response
* @param RequestInterface $request PSR-7 Request
* @return object json data
*/
public function postContactUsForm(ResponseInterface $response, RequestInterface $request): object
{
if ($requests = $this->validate->validate($this->form, $request->getParsedBody())) {
// The $requests variable will contain all "expected" form fields post values
$this->responder()->message("Completed!");
}
// Responder will pass response to frontend and Stratox.js
return $this->responder()->build();
}
}
Add controller to your router
Open the app/Http/Routes/web.php file and add the rows bellow to expected place (see router section):
You will allways need to build the form before read or validations.
$this->form->build();
4. Read form
Now you can read the form by utilizing the form partial in the template engine.
// This will build the form
$this->form->build();
// Set partial template view
$this->view()->setPartial("form", [
"tagline" => "Contact us",
"name" => "Lorem ipsum dolor",
"content" => "Lorem ipsum dolor sit amet.",
"form" => [
"method" => "post",
"action" => $this->url()->getUrl(),
"form" => $this->form,
"submit" => "Send"
]
]);
5. Validate form
Lastly use the MaplePHP validation engine.
if ($requests = $this->validate->validate($this->form, $request->getParsedBody())) {
// The $requests variable will contain all "expected" form fields post values
$this->responder()->message("Completed!");
}
// Responder will pass response to frontend
// IF form is not valid then this will be showned on the HTML form.
return $this->responder()->build();