PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Jonas Earendel  >  Simple Form Validator  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple Form Validator
Validate forms with rules defined in the HTML
Author: By
Last change:
Date: 5 years ago
Size: 2,048 bytes
 

Contents

Class file image Download
<?php
define
('LANGUAGE','en');
include
'class.SimpleFormValidator.php';

$form = new SimpleFormValidator(__DIR__.'/example.form.php');
if(!empty(
$_GET)){
    if(
$form->validate($_GET)){
       
$form->triggerCustomError('test[email][weird_name_just_to_prove_it_works]','Validation successful, custom error triggered.');
    }
}else{
   
$form->setValues(array(
       
'test' =>
            array (
               
'email' =>
                    array (
                       
'weird_name_just_to_prove_it_works' => 'validemail@test.com',
                       
'repeated' => 'validemail@test.com',
                    ),
               
'type_email' => 'validemail@test.com',
               
'password' => 'asdf',
               
'repeat_password' => 'asdf',
            ),
       
'a_radio_button' => 'något annat',
       
'foo' =>
            array (
               
'bar' => 'some_value',
            ),
       
'a_list' => 'ett konstigt värde',
       
'area' => 'ett konstigt värde',
    ));
}

?><!DOCTYPE html>
<html lang="en">
<head>
    <meta name="charset" content="UTF-8">
    <meta name="author" content="Jonas Earendel">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="PHP Form validation example.">
    <title>An example - SimpleFormValidator</title>
    <style>
    label{
        padding-top:1em;
        display: block;
        font-weight: bold;
    }
    .error {
        border:2px solid black;
    }
    .sfv{
        color:red;
    }
    </style>
</head>
<body>
    <h1>An example</h1>
    <p>
        In this example I use GET instead of POST. This makes it easier to manipulate values and trigger errors.
    </p>
    <p>
        I also have words in Swedish to test unicode characters, as well as a weird variable structure.
    </p>
    <p>
        (The form has the <b>novalidate</b> attribute, to ensure server side validation only.)
    </p>
    <form action="/example.php" method="get" novalidate>
<?php $form->html(); ?>

    </form>
</body>
</html>