PEAR Packages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPEAR Packages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old March 15th, 2007, 10:03 PM
lsolesen lsolesen is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 18 lsolesen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
HTML_QuickForm rules

I am trying to figure this out. I have som data which is entered using HTML_Quickform. I want to validate it, and it is easy with just adding rules.

However, I also need to validate the data just before it is saved - as I add some new values, and want to make sure that they are present. Now I can make another validation routine, but it would be somewhat smarter if the two validation routines had a knowledge of each other.

Anyone else have been struggling with this problem? Just a short example to show what I mean:

php Code:
Original - php Code
  1.  
  2.  
  3. $form = new HTML_QuickForm;
  4. $form->addElement('text', 'text', 'text');
  5. $form->addRule('text', 'cannot be empty', 'required');
  6.  
  7. if ($form->isSubmitted() AND $form->validate()) {
  8.   $data = $form->exportValues();
  9.   $data['perm_user_id'] = $auth->getProperty('perm_user_id');
  10.   $ship = new Ship($db, $form->exportValues('id'));
  11.   // either in the save method or before, I want
  12.   // to make sure that perm_user_id is filled in
  13.   if ($ship->save()) {
  14.     header('Location: ship_edit.php');
  15.     exit;
  16.   }
  17. }
  18.  
  19. echo $form->toHTML();
  20.  


I was thinking to have the Ship class implement an interface:

php Code:
Original - php Code
  1.  
  2. interface Validate {
  3.   function addRule();
  4. }
  5.  
  6. interface Rule {
  7.   function isValid();
  8. }
  9.  
  10. class NotEmptyRule implements Rule {
  11.  
  12.   private $field;
  13.   private $value;
  14.  
  15.   function __construct($field) {
  16.   }
  17.  
  18.   function isValid($value) {
  19.     if (empty($value)) return false;
  20.     else return true;
  21.   }
  22.  
  23. }


And then do something the lines of:

php Code:
Original - php Code
  1.  
  2. class Ship implements Validate {
  3.   private $rules = array();
  4.   function addRule($field, $rule) {
  5.     $this->rules[$field][] = $rule;
  6.   }
  7.   function validate($fields) {
  8.     $this->error = array();
  9.     foreach (array_keys($fields) AS $key) {
  10.        foreach ($this->rules[$key] AS $rule) {
  11.           if (!$rule->isValid($key[])) {
  12.             $this->error[$key] = $rule->error_msg;
  13.           }
  14.        }
  15.     }
  16.     if (count($this->error) > 0) {
  17.       return false;
  18.     }
  19.  
  20.   }
  21.  
  22.   function save($fields) {
  23.     if (!$this->validate()) return false;
  24.   }
  25. }


But that is quite a lot of code to put in every class, if I have more classes - and I can't quite think of how I can couple it to the HTML_QuickForm rules? But perhaps it is necessary to make two different validation systems?

Reply With Quote
  #2  
Old March 16th, 2007, 07:12 AM
wiesemann wiesemann is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 152 wiesemann User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 h 26 m 25 sec
Reputation Power: 2
RE: HTML_QuickForm rules

Without having read every single word of your post: addFormRule() should help for more complex cases than validating just a single field.

http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPEAR Packages > HTML_QuickForm rules


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway