0

ページ上のすべてのフォーム処理を処理するために開発した処理クラスを構築するフォームがあります。

クラスのインスタンスを使用して各フォームを作成しているページに複数のフォームがあると、問題が発生します。

    $form2 = new newForm('submitform2', 'post.contact.php', '100%');
$form2->setHandler('#canWe');
$form2->html('<div class="half">');
$form2->addText('name', 'Name:');
$form2->html('</div>');
$form2->html('<div class="halfNoMarg">');
$form2->addEmail('email_address', 'E-Mail Address:');
$form2->html('</div>');
$form2->html('<div class="half">');
$form2->addUrl('website', 'Website URL:');
$form2->html('</div>');
$form2->html('<div class="halfNoMarg">');
$form2->addText('phone', 'Phone Number:');
$form2->html('</div>');
$form2->addTextArea('about_business', 'Tell Us About Your Business:');

$form2->addSubmitButton('Find Out Now!');

$form2->getPost();
$form2->outputForm();

    $form = new newForm('submitform', 'post.contact.php', '100%');
$form->addText('name', 'Name:');
$form->addEmail('email_address', 'E-Mail Address:');
$form->addUrl('website', 'Website URL:');
$form->addText('phone', 'Phone Number:');
$form->addTextArea('about_business', 'Tell Us About Your Business:');

$form->addSubmitButton('Find Out Now!');

$form->getPost();
$form->outputForm();

クラスがフォームを処理し、不足しているフィールドなどのエラーを吐き出すと、処理中のフォーム オブジェクトだけでなく、各フォームにエラーが表示されます。

基本的に、出力は処理中のインスタンスだけでなく、クラスの各インスタンスに影響を与えます。

クラスファイルが必要な場合は、クラスファイルを含めることができます (650 行あるため、投稿に含めたくありませんでした)。

提供されたヘルプに事前に感謝します。

~M

POST 関数に追加する編集

//投稿データを取得し、投稿が事前チェックに合格した場合は、クリーンな情報の配列を返します public function getPost() {

//if post tests all passed then lets create the array of elements and load the processing file.
if ($this->checkPost() === true) {

    $cleanedPost = $this->cleanPost;

    //get the post processing file
    if (file_exists($this->processFile)) {
        require_once($this->processFile);
        //$this->postMessage($this->successMessage, 'confirm');
    } else {
        $this->postMessage('Processing File Not Found:<em>' . $this->processFile . '</em>', 'error');
    }

    return $this->cleanPost;

} else {

    return false;
}

}

//フォームを画面に出力 public function outputForm() { //echo spl_object_hash($this);

if($this->handler !== null){
    $this->handler = 'action="'.$this->handler.'"';
}

$return = '<form id="' . $this->formName . '" method="' . $this->method . '"'. $this->handler .' enctype="multipart/form-data" style="width:' . $this->width . '" >';
if(!empty($this->message)){
    $return .= '<div class="errorMessage">' . $this->message . '</div>';
}
$return .= $this->output;
$return .= '</form>';

echo $return;

}

その他のポスト処理コード

//投稿を処理する前にチェックする public function checkPost() {

//go through the entire post and process information as needed
if (!empty($_POST)) {

    foreach ($this->postCheck AS $postVar => $item) {

        if (is_array($item)) {

            //addItemTo cleanPost
            if(isset($_POST[$postVar])){
                $this->addToCleanedPost($postVar, $_POST[$postVar]);
            }else{
                $this->cleanPost[$postVar] = null;
            }

            switch ($item['type']) {

                //validate emails
                case 'email':
                    //check valid email
                    if (isset($this->cleanPost[$postVar]) && !$this->validEmail($this->cleanPost[$postVar])) {
                        $this->postMessage('Email Address is invalid');
                    }
                    break;

                //validate and verify password
                case 'password':
                    if ($item['verify'] === true) {
                        //validate password against other
                        if (!$this->verifyPassword($this->cleanPost['password'], $this->cleanPost['password2'])) {
                            $this->postMessage('Passwords do not match');
                        }
                    } else {
                        if (empty($this->cleanPost[$postVar]) && $item['req'] === 1) {
                            $this->postMessage($postVar . ': Please enter a password');
                        }
                    }
                    break;

                //validate checkboxes
                case 'checkbox':

                    if ($item['req'] === 1 && $this->validateCheckBox($postVar) === 0) {
                        $this->postMessage($postVar . ' must be checked');
                    } else {
                        $this->cleanPost[$postVar] = $this->validateCheckBox($postVar);
                    }
                    break;


                //handle url
                case 'url':
                    //first we need to handle making the url format properly
                    if(isset($this->cleanPost[$postVar])){
                        $url = $this->validUrl($this->cleanPost[$postVar]);

                        //now we do the final checking of the url and make sure it is a valid url before we move forwards
                        if ($item['req'] === 1 && $url === null) {
                            $this->postMessage($postVar . ' must be a valid URL');
                        }else{
                            $this->cleanPost[$postVar] = $url;
                        }
                    }

                    break;

                //handle text and text areas
                case 'text':
                case 'textarea':

                    if (isset($_POST[$postVar])) {

                        //check for a bypass on the min required characters
                        if (isset($item['bypass']) && $item['bypass'] === true || $this->minBypass === true) {

                            $bypassMinCheck = true;

                        } else {

                            $bypassMinCheck = false;
                        }

                        //process the item
                        if ($item['req'] === 1 && $bypassMinCheck === false && strlen($this->cleanPost[$postVar]) < 2) {

                            $this->postMessage($postVar . ' is too short');

                        } else {

                            if (!empty($this->cleanPost[$postVar]) && $bypassMinCheck === false) {

                                if (strlen($_POST[$postVar]) < 2) {

                                    $this->postMessage($postVar . ' is too short');

                                }

                            }
                        }
                    }
                    break;

                //handle other inputs
                default:
                    if (empty($_POST[$postVar]) && $item['req'] === 1) {
                        $this->postMessage($postVar . ' cannot be left empty');
                    }
                    break;
            }

        }
    }
} else {
    return false;
}

//return the success or failure of the prepost test
if (empty($this->message)) {
    return true;
} else {
    return false;
}

}

4

0 に答える 0