/ / PHP validácia - skontrolujte, či je vyplnené 1 z 3 polí - php, validácia

Validácia PHP - skontrolujte, či je 1 z 3 polí vyplnených - php, validácia

Snažím sa overiť 3 súvisiace polia. Tieto 3 polia sú vo forme, ktorá už obsahuje niekoľko desiatok polí, z ktorých mnohé sa overujú.

Pre príslušné 3 súvisiace polia musím napísať kód, ktorý skontroluje, či je 1 z ktorýchkoľvek z 3 obsadených, a potom hodí chybu, ak niektorý z ďalších 2 nie je.

  • Takže ak je A naplnené a B a C nie sú = chyba.
  • Ak je B naplnené a A a C nie sú = chyba.
  • ak je C naplnené a A a B nie sú = chyba.

    A potom tak ďalej, pričom dve sú osídlené a tretí nie.

Rovnako ako: ak sú A a B naplnené a C nie je = chyba.

Tu som začal, ale nie som si istý, či je to správne overenie:

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";

}

odpovede:

0 pre odpoveď č. 1

Ako je to?

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
}

Alebo je tu iný spôsob:

$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 pre odpoveď č. 2

Toto nebolo testované, ale vyskúšajte:

<?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 pre odpoveď č. 3

Bol by to lepší prístup, pretože v jednom poli môžete mať všetky svoje polia, ktoré chcete skontrolovať, a všetky príslušné chybové správy. Tiež je to škálovateľnejšie.

Upozorňujeme, že musíte vyskúšať, či je váš formulár správny method atribút je nastavený na post a zadali ste správne mená do svojich vstupných polí.

$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 pre odpoveď č. 4

Ak vám na konci záleží iba na chybách, pozrite si nasledujúce informácie:

$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