Centralizing the Validation of Data with the Observer Pattern in PHP - Notifying the application's core level of validation errors: defining a form observer class (
Page 3 of 4 )
Once all the data checking classes have been appropriately created, defining a form observer class is indeed a no-brainer process which can be performed quite easily.
To put it simply, the form observer class I'm just about to create will perform the following tasks: first, it will check for all the notifications received from the respective form validation classes, and then it will show either a confirmation message (if the data entered on the form doesn't contain errors) or redisplay the form in question. In the latter case, the user can correct the information and resubmit it.
Based on the previous explanation, you can understand how the Observer pattern is applied here. First, I have a set of independent classes, where their scope is clearly delimited by the type of data that they have to validate, and second, there is a form observer class which takes all the notices sent by these classes and determines the best action to take. Are you beginning to see how an observer object works in the context of this particular application?
Okay, that's a bit of theory. Now, take a look at the definition of the "FormObserver" class:
// class FormObserver
class FormObserver{
private $notifications;
private $formVar;
public function __construct($formVar){
$this->formVar=$_POST[$formVar];
$this->notifications=array();
}
public function addNotification($errorMessage){
$this->notifications[]=$errorMessage;
}
public function checkNotifications(){
if(!$this->formVar){
$this->displayForm();
}
else{
if(count($this->notifications)>0){
// form is not OK
$this->displayErrors();
$this->displayForm();
}
else{
// form is OK
echo '<p>The form has been submitted
successfully!</p>';
}
}
}
// display errors
private function displayErrors(){
$errstr='<p>Please correct the following fields and
resubmit the form:</p>';
foreach($this->notifications as $notification){
$errstr.='<p>'.$notification.'</p>';
}
echo $errstr;
}
// display form
public function displayForm(){
$formstr='<form action="'.$_SERVER['PHP_SELF'].'"
method="post">';
$formstr.='First Name <input type="text"
name="firstname" value="'.$_POST['firstname'].'"/><br />';
$formstr.='Last Name <input type="text"
name="lastname" value="'.$_POST['lastname'].'" /><br />';
$formstr.='Email <input type="text" name="email"
value="'.$_POST['email'].'" /><br />';
$formstr.='Age <input type="text" size="1" name="age"
value="'.$_POST['age'].'" /><br />';
$formstr.='<input type="submit" value="Send"
name="send" /></form>';
echo $formstr;
}
}
As you can see, the "FormObserver" class defined above is extremely understandable. Undoubtedly, the most important method worth examining here is "checkNotifications()," which checks for the existence of error messages submitted by the respective validation classes and decides what course of action should be taken.
If the data entered into the form is correct, then a confirmation message is presented to the user. If any errors were found during the validation process, the form is redisplayed by calling the "displayForm()" method. In this specific example, I decided to use a form composed of only three text fields, but you can modify this condition by coding a new form, or even using a completely independent class for generating all the form elements. Due to the native extensibility of this form observer class, it can be expanded rapidly.
Fine, at this point I hope that you've grasped how a form observer class can be created with minor hassles, which leads directly to decoupling the corresponding data validation classes from the rest of the application. At the same time, this class allows you to implement a centralized mechanism for reflecting all the errors that happened when validating the form in question. This is how an observer works, according to the theory you learned in the course of the previous articles.
Having illustrated the corresponding definition for the previous "FormObserver" class, the next step rest on demonstrating how a simple form can be validated by using this class. Regarding this topic, in the next section I'll set up a simple example, which will teach you how the Observer pattern's theory can be translated into fully functional code.
Please click on the link below and keep reading to find out how this will be done.
| | Discuss Centralizing the Validation of Data with the Observer Pattern in PHP | | | | | | |
This last article of the series is focused on getting the form validation system... | | | | | | I have enjoyed all your articles and continue to read them. I tried to implement... | | | | | | Hello Cranium,
Thank you for your commenting on this PHP article. Concerning your... | | | | | | Thank you for replying back so quickly. I changed line 94 to what you showed, but... | | | | | | Hello again Cranium,
The error you're getting it's because the $_POST[$formVar]... | | | | | | Hi Alejandro,
I appreciate all you help on this. I changed the line to the... | | | | | | Hello again Cranium,
It seems that you're having additional problems with the... | | | | | | I have done some testing by watching the flow of the output and printing to screen. ... | | | | | | Hi Cranium again,
As I posted above, it's hard to see what's wrong with your... | | | | | | I sent to the e-mail that is listed when clicking on your name by an article; it is... | | | | | | Hi Scot,
I emailed you the solution.
Regards. | | | | | | Very good series again. Thanks! | | | | | | Hi friend,
Thank you for the kind comments om my PHP article.
Regards. | | | | | | In regards to the problem anonymous had before and your suggestion:
If you... | | | | | | Hello Matthijs agin,
Thank you for your feedback concerning this PHP article. The... | | | | | | Alejandro,
Thank you for authoring content that I would expect to see in a... | | | | | | Hello Larry,
I'd like to thank you for the compliments on my PHP article, as well... | | | | | | >>> Post your comment now! | | | | | |
|
 |