onPHP

Input validation example.

 

en / ru

 

Criteria
OSQL
DAOs
Form
Cache
Application
Metaconfiguration

 

core/Form

    // user's unput emulation
    $_GET = array(
        'icqNumber'       => 'LICQ 1.3.2',
        'shortOne'        => 'i am loooong',
        'quiteLongText'   =>
            "onPHP >is< the GPL'ed"
            ." multi-purpose object-oriented"
            ." \"PHP\" <b>framework</b>.",
        'firstPassword'   => 'security is',
        'secondPassword'  => 'a good thing'
    );

    $form =
        Form::create()->
        add(
            // only integers accepted here
            Primitive::integer('icqNumber')
        )->
        add(
            // non-optional element
            Primitive::date('requiredElement')->required()
        )->
        add(
            // strlen($thisOne) should be equal or lower 2
            Primitive::string('shortOne')->setMax(2)
        )->
        add(
            // arbitrary text
            Primitive::string('quiteLongText')->
            addImportFilter(
                Filter::textImport()
            )->
            addDisplayFilter(
                Filter::chain()->
                add(
                    Filter::htmlSpecialChars()
                )->
                add(
                	... more filters here ...
                )
            )
        )->
        add(
            // required password
            Primitive::string('firstPassword')->required()->
            // hash it upon importing
            addImportFilter(
                Filter::hash()
            )
        )->
        add(
            // second one for verification
            Primitive::string('secondPassword')->required()->
            // hash it too
            addImportFilter(
                Filter::hash()
            )
        )->
        // import our emulation
        import($_GET)->
        // add optional rules by OSQL-like expressions
        addRule(
            'equalPasswords',
            Expression::eq(
                new FormField('firstPassword'),
                new FormField('secondPassword')
            )
        )->
        // check 'em
        checkRules();

    // of course, we have errors    
    if ($errors = $form->getErrors()) {

        // define error labels
        $form->
            addWrongLabel(
                'icqNumber', 'non-integer given'
            )->
            addWrongLabel(
                'shortOne', 'way too long value given'
            )->
            addWrongLabel(
                'equalPasswords', "'security is' != 'a good thing'"
            )->
            addWrongLabel('quiteLongText', "i'm ok!")->
            addMissingLabel(
                'requiredElement', 'i defined, therefore i am'
            )->
            addMissingLabel('requiredElement', 'where is my value?');

        // and give some feedback to user
        foreach ($form->getTexturalErrors() as $label)
            echo $label."\n";
    }

==

    non-integer given
    where is my value?
    way too long value given
    'security is' != 'a good thing'

PS:

    // onPHP >is< the GPL'ed
    // multi-purpose object-oriented
    // "PHP" <b>framework</b>.
    echo $form->getDisplayValue('quiteLongText');

==

    onPHP &gt;is&lt; the GPL'ed
    multi-purpose object-oriented
    &quot;PHP&quot; framework.

powered by `tar xfj /dev/tty`
$Id: examples.Form.en.html 3205 2007-04-30 20:14:42Z voxus $