PHP code example of kodhe / validation

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

    

kodhe / validation example snippets




declare(strict_types=1);

FormValidation;

$validation = new FormValidation();

$validation->set_rules('username', 'Username', '8]|matches[password_confirm]');

if ($validation->run() === false) {
    // Ada error — tampilkan per field atau sekaligus
    echo $validation->error('email');        // <p>The Email field must contain a valid email address.</p>
    echo $validation->error_string();        // semua error tergabung
} else {
    // Valid — lanjutkan proses
}

$validation->set_data([
    'nama' => 'Budi',
    'umur' => '17',
]);
$validation->set_rules('nama', 'Nama', '/ lolos
}

// Error satu field (pakai delimiters default <p>...</p>)
echo $validation->error('nama');

// Error satu field dengan delimiter custom
echo $validation->error('nama', '<span class="err">', '</span>');

// Semua error sebagai array: ['field' => 'pesan', ...]
$errors = $validation->error_array();

// Semua error sebagai satu string
echo $validation->error_string('<div>', '</div>');

// Ganti delimiters global
$validation->set_error_delimiters('<em>', '</em>');

$validation->set_rules('email', 'Email', ''ormat %s tidak valid.');

// Per field (prioritas lebih tinggi): format key = {rules}_{field}
$validation->set_message('

<input type="text" name="username" value=" echo set_value('username'); 

class Buku_model
{
    // Dipakai pada rules sebagai 'callback_cek_judul'
    public function cek_judul($str)
    {
        if ($str === strip_tags($str)) {
            return true;
        }
        $this->form_validation->set_message('cek_judul', 'Judul tidak boleh mengandung HTML.');
        return false;
    }
}

$validation->has_rule('email');   // bool
$validation->reset_validation();  // bersihkan seluruh state

$rules = [
    ['field' => 'email', 'label' => 'Email', 'rules' => '

src/
├── FormValidation.php          # Kelas utama (API kompatibel CI3)
├── Exceptions/                 # ValidationException, RuleNotFoundException
├── Factory/ValidatorFactory.php
├── Filters/BaseFilter.php
├── Messages/MessageManager.php
├── Contracts/                  # ValidatorInterface, FilterInterface, RuleInterface
├── Support/                    # RuleParser, RuleCache
├── Validators/                 # RequiredValidator, EmailValidator, RegexValidator, ...
├── ValueObjects/               # ValidationError, FieldConfig
└── helpers/form.php            # Helper HTML form (form_open, form_input, ...)