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.
// Title Valitation
$title = $Filter->stripTags($request->get('title'));
$errors['title'] = $Filter->checkLength($title, $title_max_length, 1) ? false : "Title Must be between 1 and " . $title_max_length . " characters in length.";
$Template->assign('title_error', $errors['title']);
$Template->assign('title', $title);
// Body Valitation
$body = $request->get('body');
$errors['body'] = $Filter->checkLength($body, $body_max_length, 1) ? false : "Body Must be between 1 and " . $body_max_length . " characters in length.";
$Template->assign('body_error', $errors['body']);
$Template->assign('body', $body);
Any advice on validating forms and reporting errors back to the user would be GREATLY appreciated, as this is what I always do (I haven't used objects in the past, but this method really is just procedural code with objects)
public function addValidator($name, $rule, $error){
if(array_key_exists($name, $this->data)){
$validator = new Validator($name, $rule, $error);
$this->validators[$name] = $validator;
}
}
Personally wouldn't do that. What if you have validations dependent on other validations, or the database, or the user permissions? You can't hope to successfully abstract validation logic without making things really complicated.
public function addValidator($name, $rule, $error){
if(array_key_exists($name, $this->data)){
$validator = new Validator($name, $rule, $error);
$this->validators[$name] = $validator;
}
}
Personally wouldn't do that. What if you have validations dependent on other validations, or the database, or the user permissions? You can't hope to successfully abstract validation logic without making things really complicated.
don't follow you
But... I would like to know how I could send the validator a rule to follow... how could I do that?
Well say you have a form for booking what you are going to eat at a resturant.
You can choose a starter, main and dessert from 3 <select> boxes. Above these selects are a tick boxes which you can tick or untick to specify whether you are going to have that particular the course. Because some people may not want a starter or dessert. Obviously if a user ticks that they want a dessert you have to validate that they have choosen from one of the available dessert options in the select and not just given you any old value so that is a 'Validation Rule' but the existance of that rule is conditional. You only need to do that check if they have choosen they want a dessert in the first place with the tick box.
That was a really simple example, these things can actually get quite a lot more complex if you are building web applications.
In OsisForms I solve this by providing a simple set of methods for validation but not by abstracting the validation itself. This way you still use logic to make the validations, which means you can achieve conditional validations and in fact pretty much anything but much the validation easier and more readible.
if ($f->submitted()) {
if ($chkDessert->getInput()) {
if (!$selectDessert->isOneOf()) {
$selectDessert->addError('You must choose from one of the available options');
}
} else {
// don't bother =xD
}
}
I am not sure if you have looked at the Skeleton code, but there are examples of Validator/Rule and FilterChain/Filter classes for this. There are also examples of a generic Input Controller class and state/transition based Application Controller -- and an example Form Controller built on top of it. There are code examples using these as well showing how to do form validation and display error messages for field errors.
oh yea... I forgot about those. I'll take a look in a minute.
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.