1. Go to this page and download the library: Download ray/web-form-module library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ray / web-form-module example snippets
use Ray\Di\AbstractModule;
use Ray\WebFormModule\AuraInputModule;
class AppModule extends AbstractModule
{
protected function configure()
{
$this->install(new AuraInputModule);
}
}
use Ray\WebFormModule\AbstractForm;
use Ray\WebFormModule\SetAntiCsrfTrait;
class MyForm extends AbstractForm
{
// for anti CSRF
use SetAntiCsrfTrait;
/**
* {@inheritdoc}
*/
public function init()
{
$this->setField('name', 'text')
->setAttribs([
'id' => 'name'
]);
$this->filter->validate('name')->is('alnum');
$this->filter->useFieldMessage('name', 'Name must be alphabetic only.');
}
/**
* {@inheritdoc}
*/
public function submit()
{
return $_POST;
}
/**
* {@inheritdoc}
*/
public function __toString()
{
$form = $this->form();
// name
$form .= $this->helper->tag('div', ['class' => 'form-group']);
$form .= $this->helper->tag('label', ['for' => 'name']);
$form .= 'Name:';
$form .= $this->helper->tag('/label') . PHP_EOL;
$form .= $this->input('name');
$form .= $this->error('name');
$form .= $this->helper->tag('/div') . PHP_EOL;
// submit
$form .= $this->input('submit');
$form .= $this->helper->tag('/form');
return $form;
}
}
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\FormInterface;
class MyController
{
/**
* @var FormInterface
*/
protected $contactForm;
/**
* @Inject
* @Named("contact_form")
*/
public function setForm(FormInterface $form)
{
$this->contactForm = $form;
}
/**
* @FormValidation(form="contactForm", onFailure="badRequestAction")
*/
public function createAction()
{
// validation success
}
public function badRequestAction()
{
// validation faild
}
}
echo $form; // render entire form html
echo $form->input('name'); // <input id="name" type="text" name="name" size="20" maxlength="20" />
echo $form->error('name'); // "Name must be alphabetic only." or blank.
use Ray\WebFormModule\SetAntiCsrfTrait;
class MyController
{
use SetAntiCsrfTrait;
use Ray\Di\AbstractModule;
class FakeVndErrorModule extends AbstractModule
{
protected function configure()
{
$this->install(new AuraInputModule);
$this->override(new FormVndErrorModule);
}