我正在尝试用PHP实现错误检查系统(字符太少,空白输入等等)。
现在,如果我留下一个空白的标题,则会捕获错误,但它不会显示该消息。这是实际检查输入的第一部分('ll只显示事件标题)
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
}
答案 0 :(得分:4)
请勿使用var
,请勿使用global
并定义所有属性。还要考虑@ harke的指针以获得更好的代码设计。
class check_errors
{
protected $errors;
function __construct(display_errors $errors) {
$this->errors = $errors;
}
function create_event_errors($event_title){
if($event_title == ""){
$field = "event_title";
$this->errors->setError($field,"Title cannot be blank");
}
elseif(strlen($event_title) < 5){
$field = "event_title";
$this->errors->setError($field,"Title must be longer than 5 characters");
}
elseif(strlen($event_title) > 75){
$field = "title";
$this->errors->generalError($field,"Title must be less than 40 characters");
}
}
}
class display_errors
{
protected $values = array(); //Holds submitted form field values
protected $errors = array(); //Holds submitted form error messages
public $num_errors = 0;
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 "";
}
}
}
运行代码
$display_errors = new display_errors();
$check_errors = new check_errors($display_errors);
$check_errors->create_event_errors($event_title); //submit the input to be checked
echo $display_errors->num_errors;
if($display_errors->num_errors == 0){
//submit event
}
答案 1 :(得分:1)
不知道这是否会给您带来麻烦:
$field == "title";
这句话和写作一样有用;
true or false;
你可能意味着:
$field = "title";
(单等号)
只是给你一些指示:
完成后,您可以独立验证并决定如何处理结果。编写代码也更容易,因为你不能引入这么多错误,即使你这样做,也很容易修复。
同时保持具体字段名与验证无关。