1. Go to this page and download the library: Download naucon/form 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/ */
naucon / form example snippets
use Naucon\Form\Helper\Twig\NcFormExtension;
$twig = new \Twig\Environment($loader);
$twig->addExtension(new NcFormExtension());
class User
{
protected $username;
protected $email;
protected $newsletter;
public function getUsername()
{
return $this->username;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getEmail()
{
return $this->email;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getNewsletter()
{
return $this->newsletter;
}
public function setNewsletter($newsletter)
{
$this->newsletter = $newsletter;
}
}
}
Create an instance of the entity to assign it to your form.
mManager();
$form = $formManager->createForm($user, 'yourform');
...
Now we can bind submitted form data. Therefore we call the method `bind()` and add the submitted form data eg. `$_POST` or any other array as parameter.
To ensure that the form works correctly you should allways call the `bind()` method, even if no form data were submitted.
With `isBound()` you can verify that the submitted form data are bound correctly to an entity.
If the entity was bound, you can perform any further action with the entity - like save to database.
$form->bind(isset($_POST) ? $_POST : null);
if ($form->isBound()) {
// some action, like saving the data to database
}
Make sure that the session will be closed to write the session data to the session storage.
// here rendered the form html!
session_write_close();
echo $form->getSynchronizerToken();
use Naucon\Form\FormHelper;
$formHelper = new FormHelper($form);
Form helper contains the following methodes to render form elements:
* formStart($method='post', $action=null, $enctype=null, $options=array())
* formEnd()
* formField($helperName, $propertyName, array $options=array())
* formChoice($helperName, $propertyName, $choices, array $options=array())
* formTag($helperName, $content=null, array $options=array())
Example how to render a form with FormHelper:
echo $formHelper->formStart();
echo $formHelper->formTag('errors');
foreach ($formHelper as $entityContainer) {