 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
movielady
Joined: 17 Mar 2008 Posts: 4
|
Posted: Tue Mar 18, 2008 1:56 am Post subject: quickform custom rule problem |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Tue Mar 18, 2008 12:14 pm Post subject: |
|
|
| 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 |
|
 |
movielady
Joined: 17 Mar 2008 Posts: 4
|
Posted: Tue Mar 18, 2008 12:19 pm Post subject: |
|
|
| I did (several times and several different ways), but let me do it again just in case. *g* |
|
| Back to top |
|
 |
movielady
Joined: 17 Mar 2008 Posts: 4
|
Posted: Tue Mar 18, 2008 12:43 pm Post subject: |
|
|
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 |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1053
|
Posted: Tue Mar 18, 2008 12:50 pm Post subject: |
|
|
I'd say that a while loop is pretty useless here.
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 |
|
 |
movielady
Joined: 17 Mar 2008 Posts: 4
|
Posted: Tue Mar 18, 2008 12:55 pm Post subject: |
|
|
| 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 |
|
 |
|
|
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
|
|