PHP code example of lastzero / php-input-validation

1. Go to this page and download the library: Download lastzero/php-input-validation 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/ */

    

lastzero / php-input-validation example snippets




class UserController
{
    protected $user;
    protected $form;

    public function __construct(UserModel $user, UserForm $form)
    {
        $this->user = $user;
        $this->form = $form;
    }
    
    // Update User
    public function putAction(int $id, Request $request): array 
    {
        // Find entity (throws exception, if not found)
        $this->user->find($id); 
        
        // Form initialization with current values
        $this->form->setDefinedValues($this->user->getValues()); 
        
        // Set input values
        $this->form->setDefinedWritableValues($request->request->all()); 
        
        // Validation
        $this->form->validate(); 

        if($this->form->hasErrors()) {
            throw new FormInvalidException($this->form->getFirstError());
        }
        
        // Update values in database
        $this->user->update($this->form->getValues()); 

        // Return updated values
        return $this->user->getValues(); 
    }
    
    // Return form fields incl current values for User
    public function optionsAction(int $id): array 
    {
        // Find entity (throws exception, if not found)
        $this->user->find($id); 
        
        // Form initialization with current values
        $this->form->setDefinedValues($this->user->getValues()); 
        
        // Returns form as JSON compatible array incl all values
        return $this->form->getAsArray(); 
    }
}

use InputValidation\Form;

class UserForm extends Form
{
    protected function init(array $params = array())
    {
        $definition = [
            'username' => [
                'type' => 'string',
                'caption' => $this->_('username'),
                '        'type' => 'string',
                'caption' => $this->_('gender'),
                '   ],
            'password' => [
                'type' => 'string',
                'caption' => $this->_('password'),
                'a' => 'North America',
                    'south_america' => 'South Amertica',
                    'europe' => 'Europe,
                    'asia' => 'Asia',
                    'australia' => 'Australia'
                ]
            ]
        ];

        $this->setDefinition($definition);
    }
}

use InputValidation\Form;
use InputValidation\Form\Validator;
use InputValidation\Form\Options\YamlOptions;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Loader\ArrayLoader;

$translator = new Translator('en', new MessageSelector);
$translator->addLoader('yaml', new YamlFileLoader);
$translator->addLoader('array', new ArrayLoader);

$validator = new Validator();

$options = new YamlOptions($translator);

$form = new Form($translator, $validator, $options);

$formFactory = new InputValidation\Form\Factory($translator, $validator, $options);
$formFactory->setFactoryNamespace('App\Form');
$formFactory->setFactoryPostfix('Form');
$formFactory->create('User'); // Returns instance of App\Form\UserForm

'country' => array(
    'type' => 'string',
    'caption' => 'Country',
    'default' => 'DE',
    'options' => $this->form->options('countries')
)

'country' => array(
    'type' => 'string',
    'caption' => 'Country',
    '

$form->setGroups(
    array(
        'name' => array('firstname', 'lastname'),
        'address' => array('street', 'housenr', 'zip', 'city')
    )
);

array (
  'name' => 'country',
  'caption' => 'Country',
  'default' => 'DE',
  'type' => 'string',
  'options' => array (
    array (
      'option' => 'US',
      'label' => 'United States',
    ),
    array (
      'option' => 'GB',
      'label' => 'United Kingdom',
    ),
    array (
      'option' => 'DE',
      'label' => 'Germany',
    ),
  ),
  'value' => 'DE',
  'uid' => 'id58a401f5a54e6',
),

array (
  'company' => array (
    'name' => 'company',
    'caption' => 'Company',
    'type' => 'string',
    'value' => 'IBM',
    'uid' => 'id58a401f5a54d6',
  ),
  'country' => array (
    'name' => 'country',
    'caption' => 'Country',
    'default' => 'DE',
    'type' => 'string',
    'options' => array (
      array (
        'option' => 'US',
        'label' => 'United States',
      ),
      array (
        'option' => 'GB',
        'label' => 'United Kingdom',
      ),
      array (
        'option' => 'DE',
        'label' => 'Germany',
      ),
    ),
    'value' => 'DE',
    'uid' => 'id58a401f5a54e6',
  ),
),

array(
  'person' => array (
    'group_name' => 'person',
    'group_caption' => 'Person',
    'fields' => array(
      'person' => array (
        'name' => 'firstname',
        'caption' => 'First Name',
        'type' => 'string',
        'readonly' => true,
        'value' => NULL,
        'uid' => 'id58a401f5a5267',
      ),
      'lastname' => array (
        'name' => 'lastname',
        'caption' => 'Last Name',
        'type' => 'string',
        'readonly' => false,
        'value' => 'Mander',
        'uid' => 'id58a401f5a5279',
      ),
    ),
  ),
  'location' => array (
    'group_name' => 'location',
    'group_caption' => 'Location',
    'fields' => array (
      'company' => array (
        'name' => 'company',
        'caption' => 'Company',
        'type' => 'string',
        'value' => 'IBM',
        'uid' => 'id58a401f5a54d6',
      ),
      'country' => array (
        'name' => 'country',
        'caption' => 'Country',
        'type' => 'string',
        'default' => 'DE',
        'options' => array (
          array (
            'option' => 'US',
            'label' => 'United States',
          ),
          array (
            'option' => 'GB',
            'label' => 'United Kingdom',
          ),
          array (
            'option' => 'DE',
            'label' => 'Germany',
          ),
        ),
        'value' => 'DE',
        'uid' => 'id58a401f5a54e6',
      ),
    ),
  ),
),