writing validating system

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

writing validating system

Post by staar2 »

I have no idea where to start :roll:. But there are some ideas.
Make it for PHP 5
It should be lightweight and you don't have to write much code
My idea is to make class Validate, where all checking methods, properties are.
1. Takes data from array POST/GET(REQUEST don't need i think :roll:).
2. Use __set and __get functions to but field properties like this $new->name.
3. Checks the data $new->name->isString()->empty(); so on.
3. make custom exception handler to get errors.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It sounds good. I'd start with #1 and work down your list. Post code and we can provide feedback.
(#10850)
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Post by staar2 »

Thought that I write code in strategy pattern. It should be easier to update code. What you think? Or if you know some other patterns that would be good for this solution and if you have some ideas, then you can post them here. :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I use separate Request and Validator classes -- passing the Request to the Validator. And I encapsulate the rules in individual classes. Given that you are using a fluent interface, you could use __call() to load rule classes on demand.
(#10850)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I use separate Request and Validator classes -- passing the Request to the Validator. And I encapsulate the rules in individual classes. Given that you are using a fluent interface, you could use __call() to load rule classes on demand.
Bullseye! Great advice there from arborint.
1. Takes data from array POST/GET(REQUEST don't need i think
Your creating a dependency where you don't need to. Why don't you just pass in an array? Then you can validate anything. You could assume $_POST as a default still if you like.
2. Use __set and __get functions to but field properties like this $new->name.
Are we validating data here or generating a form?

Read more...
ReDucTor
Forum Commoner
Posts: 90
Joined: Thu Aug 15, 2002 6:13 am

Post by ReDucTor »

http://darkvirus.org/includes/input.class.phps
(Yes I know, I should be using var, I should be using private)

You can use this if you want, and work on it upwards. Its just there to strip everything back, so its raw as its sent in.

Then you could if you wanted add methods

$in->int('G_id') - make integer
$in->quote('G_name') - add slashes, and put quotes around outside
$in->html('G_something') - escape html
$in->valid_email('G_email') - is valid email?
$in->valid_url('G_url') - is valid url?

or if you really wanna get into it..

2 classes:
A Validator
A Input Class which is a child of the Validator

The input class is the one which has __get and __set
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Fields array

Post by Gente »

A possible way to create validation system is to prepare a fields.inc file with following array

Code: Select all

<?php
  $fields = array (
                            'field1' => array (
                                                        'real_name' => 'E-Mail'
                                                        'is_required' => 'yes',
                                                        'regexp' => REGEXP_EMAIL
                                                         ....
                                                      )
                             'field2' => ....
                             ....
                          )
?>
Before this you should define file with REGEXP's for each data type.
After you can make a function CheckData() with foreach on this array, check if field is empty, check regexp etc. and collect the errors.
As you use real_name field you can create user frieldy templates like:
" Field _DELIM_field_name_DELIM_ is required"
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Your creating a dependency where you don't need to. Why don't you just pass in an array? Then you can validate anything. You could assume $_POST as a default still if you like.
You assume $_POST ever existed ;). Where possible I eliminate the superglobals quickly and early. This cages all input into a single object. After validation, the pre-existing object can then be discarded. You can do the same with $_POST of course, assuming you had the foresight not to build in dependencies on superglobals so they can be discarded quickly.

On the flipside, a good validation system is decoupled so you can create a quick subclass of a root class to handle arrays, objects or any other format (e.g. XML ;)).

I'd avoid exceptions for every error. Often getting a list of all problems is far more useful than failing immediately.

To add to the Strategy Pattern. The Composite Pattern can be useful - letting you group several rules under a single umbrella so there's less overall typing of code.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You assume $_POST ever existed
Not always a bad thing. You have to have some base line assumptions. Personally I don't assume the existence of POST or that the code is being executed via HTTP but to make that assumption will simplify code and will remain functional as long as that assumption is true.
Post Reply