我正在尝试使用 PHP 实现错误检查系统(字符太少、空白输入等......)。
现在,如果我留下空白标题,则会捕获错误,但不会显示消息。这是实际检查输入的第一部分(仅显示事件标题)
Class check_errors{
function create_event_errors($event_title){
global $submit_error;
if($event_title == ""){
$field = "event_title";
$submit_error->setError($field,"Title cannot be blank");
}
elseif(strlen($event_title) < 5){
$field = "event_title";
$submit_error->setError($field,"Title must be longer than 5 characters");
}
elseif(strlen($event_title) > 75){
$field = "title";
$submit_error->generalError($field,"Title must be less than 40 characters");
}
}
因此,在同一页面上,这是 display_errors 类
Class display_errors{
var $values = array(); //Holds submitted form field values
var $errors = array(); //Holds submitted form error messages
function setValue($field, $value){
$this->values[$field] = $value;
}
function setError($field, $errmsg){
$this->errors[$field] = $errmsg; //if I echo this, the error message displays
$this->num_errors = count($this->errors); //if I echo this, the # of errors displays
}
function value($field){
if(array_key_exists($field,$this->values)){
return htmlspecialchars(stripslashes($this->values[$field]));
}else{
return "";
}
}
function error($field){
if(array_key_exists($field,$this->errors)){
return $this->errors[$field];
}else{
return "";
}
}
}
现在讨论这个问题。当我尝试检查是否检测到错误时,它没有找到任何东西
$check_errors->create_event_errors($event_title); //submit the input to be checked
echo $display_errors->num_errors; //this does not display the # of errors. It does when I echo from within the function in the class though
if($display_errors->num_errors == 0){
//submit event
}