PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
quickform custom rule problem

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML
View previous topic :: View next topic  
Author Message
movielady



Joined: 17 Mar 2008
Posts: 4

PostPosted: Tue Mar 18, 2008 1:56 am    Post subject: quickform custom rule problem Reply with quote

I'm working on creating a custom rule in HTML_Quickform, and it's just not working for me. I know it's got to be something minor I've overlooked, but I'd really appreciate it if someone else could take a look at the relevant bits and let me know if you see anything out of place. Thanks! :) (I've looked all over for possible solutions, but am not finding *anything* that will help me fix this bug.)

The form itself works correctly, except for this one rule that's throwing it off, so I'll only include those elements.

The custom validation function is supposed to grab the info from the db table and compare what the user entered in the field to make sure that name isn't already in use. Where it seems (to me) to be hanging up is that it sends back "false" to say that the value failed the test (that name's already in use), but then it doesn't *do* anything with that value in the form. Non-duplicates are correctly entered into the database, but duplicates just give me the error message "Error 1062: Duplicate entry 'genres' for key 2" (which, yes, thank you, I *know* that, that's the point!), from the add_meta_type function, rather than correctly displaying the value of $er_exists ("That URL already exists. Please choose a different one.") on the form and letting the user re-enter a different name. *sigh*

Form file:
Code:

[...]

$form->addElement('text', 'short', 'Short version of the name for the URL:<br />(Must be unique)', array('size' => 50, 'maxlength' => 255));

[...]

$form->registerRule('checkurl', 'callback', 'check_short_url');

[...]

$form->addRule('short', $er_required, 'required');
$form->addRule('short', $er_exists, 'checkurl');



Custom function:

Code:

// checks to make sure the short URL is unique
function check_short_url($mtshort) {
   include('db.php');
   $mysqli = new mysqli($host, $user, $pwd, $db);

   if (mysqli_connect_errno()) {
      printf("<p>Connect failed: %s\n</p>", mysqli_connect_error());
      exit();
   }

   $sql = $mysqli->prepare("SELECT COUNT(*) FROM meta_types WHERE mtshort = ?");
   $sql->bind_param('s', $mtshort);
   $mtshort = filter_var($mtshort, FILTER_SANITIZE_STRING, 'FILTER_ENCODE_HIGH');
   $sql->execute();
   $sql->bind_result($counted);

   if (mysqli_errno($mysqli)) {
      printf("<p>Error %s: %s</p>\n", mysqli_errno($mysqli), mysqli_error($mysqli));
   } else {
      if ($counted > 0) {
         return false;
      } elseif ($counted == 0) {
         return true;
      }
      return false;
   }

   $sql->close();
   $mysqli->close();
}



Related "add record to the table" function:
Code:

function add_meta_type($mttitle, $mttype, $mtdesc, $mtswitch, $mtshort) {

   include('db.php');
   $mysqli = new mysqli($host, $user, $pwd, $db);

   if (mysqli_connect_errno()) {
      printf("<p>Connect failed: %s\n</p>", mysqli_connect_error());
      exit();
   }

   $datepost = 'now()';
   $sql = $mysqli->prepare("INSERT INTO meta_types (mttitle, mttype, mtdesc, mtswitch, mtshort) VALUES (?, ?, ?, ?, ?)");
   $sql->bind_param('sssss', $mttitle, $mttype, $mtdesc, $mtswitch, $mtshort);

   $mttitle = filter_var($mttitle, FILTER_SANITIZE_STRING, 'FILTER_ENCODE_HIGH');
   $mtdesc = filter_var($mtdesc, FILTER_SANITIZE_STRING, 'FILTER_ENCODE_HIGH');
   $mtshort = filter_var($mtshort, FILTER_SANITIZE_STRING, 'FILTER_ENCODE_HIGH');

   $sql->execute();

   if (mysqli_errno($mysqli)) {
      printf("<p>Error %s: %s</p>\n", mysqli_errno($mysqli), mysqli_error($mysqli));
   } else {
      echo '<p>Success! ';
      printf ("The meta type was added as ID #%d.</p>\n", $mysqli->insert_id);
      echo '<p><a href="add-meta-type.html" title="Add a new tag">Add a new meta type</a>, <a href="edit-delete-meta-type.html" title="edit or delete meta types">edit or delete meta types</a>, or <a href="index.html" title="return to the administration menu">return to the administration menu</a>.</p>';
   }

   $sql->close();
   $mysqli->close();
}



Thanks. :)
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Tue Mar 18, 2008 12:14 pm    Post subject: Reply with quote

Just to make sure that the problem is not your own callback function: Did you have called this function with both existing and not existing values yourself, and did you have check the returned value?
Back to top
View user's profile Send private message
movielady



Joined: 17 Mar 2008
Posts: 4

PostPosted: Tue Mar 18, 2008 12:19 pm    Post subject: Reply with quote

I did (several times and several different ways), but let me do it again just in case. *g*
Back to top
View user's profile Send private message
movielady



Joined: 17 Mar 2008
Posts: 4

PostPosted: Tue Mar 18, 2008 12:43 pm    Post subject: Reply with quote

I determined that the version of quickform I have installed was fine by trying a different custom rule on a completely different form, and it worked fine, so you're right, it did have to be the function. I tested it again and again (again *g*), and it turns out I was missing this line:

Code:

       while ($sql->fetch()) {


just before the function checked which value to return. So that part of the function now reads:

Code:

   if (mysqli_errno($mysqli)) {
      printf("<p>Error %s: %s</p>\n", mysqli_errno($mysqli), mysqli_error($mysqli));
   } else {
       while ($sql->fetch()) {
         if ($counted == 0) {
            return true;
         } else {
            return false;
         }
      }
   }


It all works correctly now, yay!
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Tue Mar 18, 2008 12:50 pm    Post subject: Reply with quote

I'd say that a while loop is pretty useless here. Wink

How about this?

Code:

   if (mysqli_errno($mysqli)) {
      printf("<p>Error %s: %s</p>\n", mysqli_errno($mysqli), mysqli_error($mysqli));
   } else {
       $sql->fetch();
       if ($counted == 0) {
          return true;
       }
       return false;
   }
Back to top
View user's profile Send private message
movielady



Joined: 17 Mar 2008
Posts: 4

PostPosted: Tue Mar 18, 2008 12:55 pm    Post subject: Reply with quote

Heh, yeah, good point, thanks. *g* (I should really go to bed and not try to program well at 5 in the morning. *g*)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software