Form Validation

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

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

How's this looking?

Form.inc.php

Code: Select all

<?php
require('FormField.inc.php');
class Form{
	
	private
	$fields = array(),
	$rules  = array(),
	$filters = array(),
	$errors = array();
	
	public function __construct(){
	}
	
	public function addField(FormField $field){
		$name = $field->getName();
		$this->fields[$name] = $field;
	}
	
	public function addRule($rule){
		$this->rules[] = $rule;
	}
	
	public function addFilter($filter){
		//TODO I might use this later, I don't know
		$this->filters[] = $filter;
	}
	
	public function validate(){
		foreach($this->rules as $rule){
			$rule->check() or $this->errors['form'][] = $rule->getError();
		}
		foreach($this->fields as $field){
			$field->validate() or $this->errors[$field->getName()] = $field->getErrors();
		}
		return empty($this->errors);
	}
	
	public function getErrors(){
		return $this->errors;
	}
	
}
?>
Formfield.inc.php

Code: Select all

<?php
class FormField{
	
	private
	$name,					// Name of field
	$value, 				// Value of field
	$filters = array(),		// Array of filter (objects) to be performed on value before validation
	$rules = array(),		// Array of validation rules (objects) to be checked before validation
	$errors = array();		// Array of corresponding errors to output to the user
	
	public function __construct($name, $value){
		$this->name  = $name;
		$this->value = $value;
	}
	
	public function addRule($rule){
		$this->rules[] = $rule;
	}
	
	public function addFilter($filter){
		$this->filters[] = $filter;
	}
	
	public function validate(){
		foreach($this->filters as $filter){
			$this->value = $filter->run($this->value);
		}
		foreach($this->rules as $rule){
			$rule->check($this->value) or $this->errors[] = $rule->getError();
		}
		return !$this->getErrors();
	}
	
	public function getName(){
		return $this->name;
	}
	
	public function getValue(){
		return $this->value;
	}
	
	public function getErrors(){
		return $this->errors;
	}
}
?>
Usage:

Code: Select all

<?php
		$Registry->register('Form', null, $request->getData());
		$Form = $Registry->get('Form');

		$title = new FormField('title', $request->get('title'));
		$title->addRule(new RuleRange($title_max_length, 1, 'Must be between 1 and ' . $title_max_length . ' characters in length.'));

		$body = new FormField('body', $request->get('body'));
		$body->addRule(new RuleRange($body_max_length, 1, 'Must be between 1 and ' . $body_max_length . ' characters in length.'));
		
		$Form->addField($title);
		$Form->addField($body);
		
		$Form->addRule(new RuleMatch(array($request->get('title'), $request->get('body')), 'Title must match body'));
		
		if($Form->validate()){
			echo "Valid Form Submission. ";
		}
		else{
			$errors = $Form->getErrors();
			print_r($errors);
		}
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The Ninja Space Goat wrote:Arborint... if your code was commented and PHP5, I would love you. hehe... it's great and I borrowed about 100 things from it, but it takes me a long time to figure stuff out because it isn't commented. :(
Well as you figure it out add comments and send it to me! ;)
(#10850)
Post Reply