現在状態
$errorsが空でもkeyもっているので表示されてしまう。
やりたいこと
$errorsが空だったらkyeを持たず$errorsが空ではなかったら表示させるようにする。
session_start(); $message = $_POST['message']; $mail = $_POST['mail']; $comment = $_POST['comment']; $upfile = $_FILES['upfile']; Class Errorchak { public $message; public $mail; public $comment; public $upfile; public $errors; //メッセージエラーチェック public function chakmes($message) { $errors = ""; if ((mb_strlen($message) >= 10 || mb_strlen($message) <= 5)) { $errors = "5文字以上10文字以下で記入してください。"; } } public function chakmessa($message) { $errors = ""; $pattern = "/^[a-zA-Z0-9]+$/"; if (preg_match($pattern, $message)) { $errors = "ひらがなで入力してください。"; } } //メールのエラーチェック public function chakmail($mail) { $errors = ""; $pattern = "/^[a-zA-Z0-9_¥.¥-]+@[a-zA-Z0-9¥-]+.[a-zA-Z0-9]{2,4}$/"; if (!preg_match($pattern, $mail)) { $errors = "メールアドレスを正しく入力してください<br>"; } } //画像のエラーチェック public function cheakfile($upfile) { $errors = ""; if (is_uploaded_file($_FILES['upfile']['tmp_name'])) { if (move_uploaded_file($_FILES['upfile']['tmp_name'], "./" . $_FILES['upfile']['name'])) { echo "uploaded<br>"; } else { $errors = "アップロードできませんでした。"; } } } public function cheakfiletype($upfile) { $errors = ""; $upfil = $_FILES['upfile']['type']; if (!preg_match("/.*.jpg|.*.png||.*.gif/i", $upfil)) { $errors = "対象ファイルはPNG,JPG,GIFのみです。<br>"; } } public function totalerror($errors){ if (!empty($errors)) { $this->errors = $errors; echo $errors; }else{ unset($errors); } return $errors; } } $errorchak = new Errorchak($errors); $errors = array(); $errors[] = $errorchak->chakmes($_POST['message']); $errors[] = $errorchak->chakmessa($_POST['message']); $errors[] = $errorchak->chakmail($_POST['mail']); $errors[] = $errorchak->cheakfile($_POST['upfile']); $errors[] = $errorchak->cheakfiletype($_FILES['upfile']); ?>
やってみたこと
public function totalerror($errors){ $errors = array(); $errors[] = chakmes($_POST['message']); $errors[] = chakmessa($_POST['message']); $errors[] = chakmail($_POST['mail']); $errors[] = cheakfile($_POST['upfile']); $errors[] = cheakfiletype($_FILES['upfile']); if (!empty($errors)) { $this->errors = $errors; echo $errors; }else{ unset($errors); } return $errors; } } $errorchak = new Errorchak($errors); $errorchak->totalerror(); ?>
これですとFatal error: Uncaught ArgumentCountErrorになってしまっていました。
よろしくお願いします
kye ではなく key です。
細かいですが決まっている文言についてはスペルミスは命取りになりますのでご注意を。
回答5件
あなたの回答
tips
プレビュー