PHP code example of naucon / form

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());

 {{ ncform_start(form, method='post', action='some-action', enctype='some type', {furtherOptions:'option'}) }}
 {{ ncform_field(form, 'text', 'activation_code', { style: 'some style', id: 'some id', value: 'some value', maxlength: 'some lenght', class: 'css class', 

		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) {
	
 echo $formHelper->formField('label', 'username'); 
 echo $formHelper->formField('text', 'username', array('maxlength' => 32)); 
 echo $formHelper->formField('error', 'username'); 
 echo $formHelper->formField('label', 'email'); 
 echo $formHelper->formField('text', 'email'); 
 echo $formHelper->formField('error', 'email'); 
 echo $formHelper->formField('label', 'newsletter'); 
 echo $formHelper->formField('checkbox', 'newsletter', 1); 
 echo $formHelper->formField('error', 'newsletter'); 
 echo $formHelper->formTag('submit', 'Submit'); 
 echo $formHelper->formTag('reset', 'Reset'); 

    }
    echo $formHelper->formEnd();


##### FormHelper with Smarty3 Templates

First we have to add the form helper smarty plugins to smarty.

	$smarty->setPluginsDir('vendor/Naucon/Form/Helper/view/smarty');

Next we have to assign the form instance to smarty.

	$smarty->assign('form', $form);

The form package contains the following blocks and function plugins:

Plugins                        | Tags              | Attributes
:----------------------------- | :---------------- | :----------
smarty_block_ncform            | {ncform}{/ncform} | from, method?, action?, enctype?, id?, class?, style?
smarty_function_ncform_field   | {ncform_field}    | type, field, value?, maxlength?, id?, class?, style? domain?
smarty_function_ncform_choice  | {ncform_choice}   | type, field, value?, choices?, id?, class?, style?
smarty_function_ncform_tag     | {ncform_tag}      | type, value?, id?, class?, style?
smarty_function_ncform_option  | {ncform_option}   | type, choice, id?, class?, style?


Example how to render a form in a smarty template:

	{ncform from=$form}
		{ncform_tag type='errors'}
		<fieldset>
			{ncform_field type='label' field='username'}
			{ncform_field type='text' field='username' maxlength=32}
			{ncform_field type='error' field='username'}
			<br/>
			{ncform_field type='label' field='email'}
			{ncform_field type='text' field='email'}
			{ncform_field type='error' field='email'}
			<br/>
			{ncform_field type='label' field='newsletter'}
			{ncform_choice type='radio' field='newsletter' value=1}
			{ncform_field type='error' field='newsletter'}
			<br/>
		</fieldset>
 		<fieldset>
 			{ncform_tag type='submit' value='Submit'}
 			{ncform_tag type='reset' value='Reset'}
 		</fieldset>
	{/ncform}

The example above renders the following html markup:

	<form method="post" id="yourform">
		<input type="hidden" name="_ncToken" value="67187c7e5abeb31b93339ab5cbb263b6" />
		<fieldset>
			<label for="yourform_username">username</label>
			<input type="text" name="yourform[username]" value="" id="yourform_username" maxlength="32" />
			<br/>
			<label for="yourform_email">email</label>
			<input type="text" name="yourform[email]" value="" id="yourform_email" />
			<br/>
			<label for="yourform_newsletter">newsletter</label>
			<input type="checkbox" name="yourform[newsletter]" value="1" id="yourform_newsletter" />
			<br/>
		</fieldset>
 		<fieldset>
 			<input type="submit" value="Submit" />
		 	<input type="reset" value="Reset" />
		</fieldset>
	</form>

It's also possible to use the `{ncform_field}` element to output only parts of an input field, for example id, name, value


    <input type="text" name="{ncform_field type='name' field='email'}" value="{ncform_field type='value' field='email'}" id="{ncform_field type='id' field='email'}" maxlength="32" />

##### FormHelper with Twig Templates

First you have to register the twig extension




Functions     | Tags               | Attributes
:------------ |:-------------------| :----------
ncform_start  | {{ncform_start()}} | form, method?, action?, enctype?, id?, class?, style?
ncform_field  | {{ncform_field()}} | form, type, field, value?, maxlength?, id?, class?, style?, data-attributes?, 
 echo $entityContainer->getFormValue('username'); 

        if ($formEntity->hasError('username')) {
            echo 'WRONG: ' . $formEntity->getError('username')->getMessage();
        }
    
 echo $entityContainer->getFormName('firstname'); 
 echo $entityContainer->getFormValue('firstname'); 

        if ($formEntity->hasError('firstname')) {
            echo 'WRONG: ' . $formEntity->getError('firstname')->getMessage();
        }
    
 echo $forms->getFormOptionName() 
 echo $entityContainer->getName() 
 if ($forms->isFormOptionSelected($entityContainer->getName())) echo ' checked="checked"' 
 echo $formHelper->formOption('radio', 'cc' );