PHP form validation functions not working -
i following video tutorial series far has been great. stumped @ part gives form validation functions. actual form located here @ moment: http://leegster.com/leegster_test/public/manage_content.php
here how functions called in create_subject.php:
if (isset($_post['submit'])) { // process form //$menu_name = $_post["menu_name"]; $menu_name = mysql_prep($_post["menu_name"]); $position = (int) $_post["position"]; $visible = (int) $_post["visible"]; // validations $required_fields = array("menu_name", "position", "visible"); validate_presences($required_fields); $fields_with_max_lengths = array("menu_name" => 30); validate_max_lengths($fields_with_max_lengths); if (!empty($errors)) { $_session["errors"] = $errors; redirect_to("new_subject.php"); } and here functions shown in validation_functions.php:
// * presence // use trim() empty spaces don't count // use === avoid false positives // empty() consider "0" empty function has_presence($value) { return isset($value) && value != ""; } function validate_presences($required_fields) { global $errors; foreach($required_fields $field) { $value = trim($_post[$field]); if (!has_presence($value)) { $errors[$field] = fieldname_as_text($field) . " can't blank"; } } } // * string length // max length function has_max_length($value, $max) { return strlen($value) <= $max; } function validate_max_lengths($fields_with_max_lengths) { global $errors; // expects assoc. array foreach($fields_with_max_lengths $field => $max) { $value = trim($_post[$field]); if (!has_max_length($value, $max)) { $errors[$field] = fieldname_as_text($field) . " long"; } } } when form submitted, validate_presences , validate_max_lengths called in order ensure nothing left blank , menu_name no more 30 characters. page should refresh , errors should shown (i can include code if needed).
however, when make blank entry on form, lets me it. blank entry submitted database success message. can't understand why happening these new validation functions supposed block that. far can see, did step step video tutorial.
any appreciated. thanks!
just tested code , seems should work now:
function has_presence($value) { return isset($value) && $value != ""; // forgot $, propably typo } function validate_presences($required_fields, $fields) { $errors = []; // not use globals, should pass required variables arguments foreach($required_fields $field) { $value = trim($fields[$field]); if (!has_presence($value)) { $errors[$field] = $field." can't blank"; } } return $errors; } function has_max_length($value, $max) { return strlen($value) <= $max; } function validate_max_lengths($fields_with_max_lengths, $fields) { $errors = []; // expects assoc. array foreach($fields_with_max_lengths $field => $max) { $value = trim($fields[$field]); if (!has_max_length($value, $max)) { $errors[$field] = $field." long"; } } return $errors; } $required_fields = array("menu_name", "position", "visible"); $errors = validate_presences($required_fields, $post); $fields_with_max_lengths = array("menu_name" => 30); $errors = array_merge($errors, validate_max_lengths($fields_with_max_lengths, $post)); print_r($errors); however, should check existence of fields. $value = trim($fields[$field]); should changed to: $value = isset($fields[$field]) ? trim($fields[$field]) : '';
oh, , don't either: mysql_prep($_post["menu_name"]); should escape post fields first.
Comments
Post a Comment