2015-11-20 78 views
0

我想驗證3個相關的字段。這3個字段的格式已經有幾十個字段,其中許多字段正在驗證中。PHP驗證 - 檢查是否有1個字段填充

對於所討論的3個相關領域,我需要編寫,檢查是否任何3的1被填充的代碼,然後將引發一個錯誤,如果任何其他的2都沒有。

  • 所以如果A被填充而B和C不是=錯誤。
  • 如果B填充並且A和C不是=錯誤。
  • 如果C被填充並且A和B不是=錯誤。

    再等兩個被填充;第三,沒有。

喜歡:如果A和B填充和C不是=錯誤。

這裏是我起步的地方,但不知道這是正確的驗證:

if(!empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_date'] = 'Enter Notify Date'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

} 

else if (empty($post['email_notify_subject']) 

    && !empty($post['email_notify_date']) 
    && empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

} 

else if (empty($post['email_notify_subject']) 

    && empty($post['email_notify_date']) 
    && !empty($post['email_notify_emails'])) 

{ 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_date'] = 'Enter Notify Date'; 

} 
+0

我們需要更多的代碼比一行... – Machavity

+0

後這些字段的HTML代碼,以及你的

一行。 –

+0

有什麼好運氣? –

回答

0

這是怎麼回事?

if (
    (
     ! empty($post['email_notify_subject']) && 
     empty($post['email_notify_date']) && 
     empty($post['email_notify_emails']) 
    ) || 
    (
     empty($post['email_notify_subject']) && 
     ! empty($post['email_notify_date']) && 
     empty($post['email_notify_emails']) 
    ) || 
    (
     empty($post['email_notify_subject']) && 
     empty($post['email_notify_date']) && 
     ! empty($post['email_notify_emails']) 
    ) 
) { 
    // only 1 field filled out 
} 

或者還有一種方法:

$num_filled_out = 0; 
if (! empty($post['email_notify_subject'])) $num_filled_out++; 
if (! empty($post['email_notify_date'])) $num_filled_out++; 
if (! empty($post['email_notify_emails']) $num_filled_out++; 
if ($num_filled_out <= 1) { 
    // 1 or 0 filled out 
} 
+0

謝謝你,我使用了++版本,它工作 – user3784436

0

沒有測試,但不妨一試:

<?php 
    if(empty(array(
    $_post['email_notify_subject'], 
    $_post['email_notify_date'], 
    $_post['email_notify_emails']))){ 
     echo "All Three filed required"; //or other instructions 
} 
    else{ 
    //Proceed with the form 
} 
0

這將是一個更好的辦法,因爲你可以有你的領域你想要檢查一個數組中的所有相應的錯誤消息。而且它更具可擴展性。

請注意,您必須測試您的表單method屬性是否設置爲post,並且您已向輸入字段提供了正確的名稱。

$data_to_check = array(
    'email_notify_subject'=>array(
     'error'=>'Enter Notify Subject' 
    ), 
    'email_notify_date'=>array(
     'error'=>'Enter Notify Date' 
    ), 
    'email_notify_emails'=>array(
     'error'=>'Enter Notify Emails' 
    ), 
); 

$errors = array(); 

foreach($data_to_check as $key => $value) { 
    if (empty($post[$key])) { 
     $errors[] = $value['error']; 
    } 
} 

if (!empty($errors)) { 
    foreach($errors as $key => $value) { 
     echo '<p class="error">'.$value.'</p>'; 
    } 
} 
0

如果你只關心在年底錯誤,請參閱以下內容作爲參考:

$post['email_notify_subject'] = array(); // same (== not ===) as null 
$post['email_notify_date']  = null;  // same (== not ===) as empty array 
$post['email_notify_emails'] = 'test string'; 

$errors['email_notify_subject'] = 'Enter Notify Subject'; 
$errors['email_notify_date']  = 'Enter Notify Date'; 
$errors['email_notify_emails'] = 'Enter Notify Emails'; 

foreach ($post as $key => $data) { 

    if (!empty($data)) { 

     unset($errors[$key]); // if $post data not empty, unset error msg 
    } 
} 

var_dump($errors); // test dump $errors