PHP code example of severak / forms

1. Go to this page and download the library: Download severak/forms 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/ */

    

severak / forms example snippets



// registration form
$form = new severak\forms\form(['method'=>'POST']);
$form->field('username', ['label'=>'User name / URL', '>'password', 'th terms of service', 'type'=>'checkbox']);
$form->field('register', ['label'=>'Register new account', 'type'=>'submit']);

$form->rule('username', function($name) {
	return preg_match('~^[a-z]([a-z0-9]{2,})$~', $name)===1;
}, 'Bad username format: 3 or more lower case letters and numbers allowed, must start with letter.');

$form->rule('password_again', function($password, $fields) {
	return $password==$fields['password'];
}, 'Must match previous password.');

$form->rule('terms_agreement', function($agreed){
	return !empty($agreed);
}, 'You cannot use our service without terms agreement.');

if ($form->fill($_POST) && $form->validate()) {
    // form was successfully sent and data were valid
    if ($databaseInsertFailed) {
        // you can also manually error messages AFTER validation, e.g. when something went wrong with databse
        $form->error('register', 'Something went wrong while adding you. Please try again later.');
    }
}

// display form as HTML
echo $form;