8722 sujets

Développement web côté serveur, CMS

Bonjour,

Si mon site nous avons un formulaire de contact. La plupart des champs sont obligatoire afin de facilité la communication avec le client.

Cependant, nous aimerions que ces champs ne soient obligatoires que pour certains pays, pas tous.

Est-ce possible de faire ça?
Modifié par juliesunset (08 Apr 2013 - 22:37)
Oui c'est possible.

Une manière de le mettre en oeuvre:

- dans ton formulaire tu rajoutes un champ hidden "country" ayant pour valeur le code pays (récupéré en analysant les données de l'utilisateur).

- dans ton code métier de vérifications (et/ou infos d'affichage JS), tu peux (dés)activer ces vérifs en fonction de la valeur de $_POST["country"] (si ton form est en post)...
ok, merci pour les pistes.

Comme ce n'est pas moi qui est construit le code du formulaire, ça complique un peu les choses.

Alors, dans mon fichier de vérification J'ai cette ligne qui se rapporte au champs "pays":
$this->error['pays'] = $this->pays == "null"?FIELD_REQUIRED [decu]$this->error['pays'] !== false?$this->error['pays']: false);


Mon réflex serais d'ajouté un if/else à ce niveau...
if($_POST["pays"]='Canada' OR $_POST["pays"]='État-Unis'){
	$this->error['pays'] = $this->pays == "null"?FIELD_REQUIRED [decu]$this->error['pays'] !== false?$this->error['pays']: false);
}

Modifié par juliesunset (08 Apr 2013 - 22:04)
Avec juste une ligne de code et aucune info sur la structure de ton objet ($this) ça va être dur de t'aider.
Bon je vous donne le code complet, mais c'est assez conséquent!
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/phpmailer/class.phpmailer.php");

class Soumission {
		public $prenom;
		public $nom;
		public $adresse;
		public $ville;
		public $province;
		public $cp;
		public $pays;
		public $tel;
		public $courriel;
		public $departement;
		public $produit;
		public $sujet;
		public $message;
		public $upload;
		
		/* CONFIG */
		public $allowedType = array(
			".jpg" => "image/jpeg",
			".png" => "image/png",
			".gif" => "image/gif",
			".pdf" => "application/pdf"
		);
		
		private $upload_path;
		private $maxFilesize = 2097152;
		private $validDepartements = array("vente" => "Vente",
						"infos" => "Informations",
						"commentaires" => "Commentaires",
						"support" => "Support Technique",
						"photo" => "Photo/Video");
		private $smtpEmail = "noreply@spypoint.com";
		private $smtpUsername = "noreply@spypoint.com";
		private $smtpPassword = "qwc157nr";
		private $smtpHost = "mail.spypoint.com";
		private $smtpPort = 25;
		private $toEmail = array("vente" => "sales@spypoint.com",
					"infos" => "info@spypoint.com",
					"commentaires" => "info@spypoint.com",
					"support" => "tech@spypoint.com",
					"photo" => "pcharriere@spypoint.com");
		
		//false = pas d'erreur
		private $error = array(
			"prenom" => false,
			"nom" => false,
			"adresse" => false,
			"ville" => false,
			"provinve" => false,
			"cp" => false,
			"pays" => false,
			"tel" => false,
			"courriel" => false,
			"departement" => false,
			"sujet" => false,
			"produit" => false,
			"message" => false,
			"upload" => false
		);
		
		public function getError($fieldName) {
			if($this->error[$fieldName] != false) {
				echo '<span class="alert">'.$this->error[$fieldName].'</span>';
			}
		}
		
		public function __construct(){
			if(isset($_POST['send'])){
				//prenom
				$this->prenom = htmlspecialchars($_POST["prenom"]);
				//nom
				$this->nom = htmlspecialchars($_POST["nom"]);
				//adresse
				$this->adresse = htmlspecialchars($_POST["adresse"]);
				//ville
				$this->ville = htmlspecialchars($_POST["ville"]);
				//province
				$this->province = htmlspecialchars($_POST["province"]);
				//code postal
				$this->cp = htmlspecialchars($_POST["cp"]);
				//pays
				$this->pays = htmlspecialchars($_POST["pays"]);
				//telephone
				$this->tel = htmlspecialchars($_POST["tel"]);
				//courriel
				$this->courriel = htmlspecialchars($_POST["courriel"]);
				//departement
				$this->departement = htmlspecialchars($_POST["departement"]);
				//produit
				$this->produit = htmlspecialchars($_POST["produit"]);
				//sujet
				$this->sujet = htmlspecialchars($_POST["sujet"]);
				//message
				$this->message = stripslashes($_POST["message"]);
				$this->message = htmlspecialchars($this->message);
				$this->message = nl2br($this->message);
				//upload
				$this->upload = !empty($_FILES['upload']['name']) ? $_FILES['upload'] : FALSE;

			}
		}
		
		public function ErrorCount() {
			$errorCount = 0;
			foreach($this->error as $e) {
				if($e != false) {
					$errorCount++;
				}
			}
			
			return $errorCount;
		}
		
		public function validation(){
			
			//verification champs invalide
			$this->error['courriel'] = !preg_match("/^[-\w.]+@([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $this->courriel)?FIELD_INVALID [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
			$this->error['cp'] = !preg_match("#(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)#i", $this->cp)?FIELD_INVALID [decu]$this->error['cp'] !== false?$this->error['cp']:false);
			$this->error['tel'] = !preg_match("/^[+]?[01]?[- .]?(\\([2-9]\\d{2}\\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$/", $this->tel)?FIELD_INVALID [decu]$this->error['tel'] !== false?$this->error['tel']:false);
			$this->error['departement'] = !isset($this->validDepartements[$this->departement])?FIELD_INVALID [decu]$this->error['departement'] !== false?$this->error['departement']:false); //validation des departement
			$this->error['produit'] = ($this->departement == "support" || $this->departement == "photo") && strlen($this->produit) >= 25?FIELD_INVALID [decu]$this->error['produit'] !== false?$this->error['produit']:false); 
			
			//field length
			$this->error['prenom']= (strlen($this->prenom) > 20)?FIELD_LENGTH [decu]$this->error['prenom'] !== false?$this->error['prenom']:false);
			$this->error['nom']= (strlen($this->nom) > 20)?FIELD_LENGTH [decu]$this->error['nom'] !== false?$this->error['nom']:false);
			$this->error['sujet']= (strlen($this->sujet) > 50)?FIELD_LENGTH [decu]$this->error['sujet'] !== false?$this->error['sujet']:false);
			$this->error['courriel'] = strlen($this->courriel) > 30?FIELD_LENGTH [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
			$this->error['adresse']= strlen($this->adresse) > 50?FIELD_LENGTH [decu]$this->error['adresse'] !== false?$this->error['adresse']:false);
			$this->error['ville']= strlen($this->ville) > 20?FIELD_LENGTH [decu]$this->error['ville'] !== false?$this->error['ville']:false);
			$this->error['cp']= strlen($this->cp) > 7?FIELD_LENGTH [decu]$this->error['cp'] !== false?$this->error['cp']:false);
			$this->error['province'] = strlen($this->province) > 20?FIELD_LENGTH [decu]$this->error['province'] !== false?$this->error['province']:false);
			$this->error['message'] = strlen($this->message) > 500?FIELD_LENGTH [decu]$this->error['message'] !== false?$this->error['message']:false);
				
			//vérification des champs requis
			$this->error['prenom'] = empty($this->prenom)?FIELD_REQUIRED [decu]$this->error['prenom'] !== false?$this->error['prenom']:false);
			$this->error['nom'] = empty($this->nom)?FIELD_REQUIRED [decu]$this->error['nom'] !== false?$this->error['nom']:false);
			$this->error['courriel'] = empty($this->courriel)?FIELD_REQUIRED [decu]$this->error['courriel'] !== false?$this->error['courriel']:false);
			$this->error['departement'] = $this->departement == "null"?FIELD_REQUIRED [decu]$this->error['departement'] !== false?$this->error['departement']:false);
			$this->error['sujet'] = empty($this->sujet)?FIELD_REQUIRED [decu]$this->error['sujet'] !== false?$this->error['sujet']:false);
			$this->error['message'] = empty($this->message)?FIELD_REQUIRED [decu]$this->error['message'] !== false?$this->error['message']:false);
			$this->error['tel'] = empty($this->tel)?FIELD_REQUIRED [decu]$this->error['tel'] !== false?$this->error['tel']:false);
			$this->error['ville'] = empty($this->ville)?FIELD_REQUIRED [decu]$this->error['ville'] !== false?$this->error['ville']:false);
			$this->error['pays'] = $this->pays == "null"?FIELD_REQUIRED [decu]$this->error['pays'] !== false?$this->error['pays']:false);
			$this->error['produit'] = ($this->departement == "support" || $this->departement == "photo") && empty($this->produit)?FIELD_REQUIRED [decu]$this->error['produit'] !== false?$this->error['produit']:false);
			$this->error['upload'] = ($this->departement == "photo" && $this->upload == FALSE) ? PLEASE_UPLOAD : ($this->error['upload'] !== false?$this->error['upload']:false);
			
			if($this->upload !== FALSE) {
				//vérification du type par extension
				$ext = strrpos($this->upload['name'], ".");
				$ext = strtolower(substr($this->upload['name'], $ext, strlen($this->upload['name'])));
				
				$this->error['upload'] = !isset($this->allowedType[$ext])?UPLOAD_WRONG_TYPE:false;
				if($this->ErrorCount() > 0)
					return;
				
				//vérification du type par MIME
				$this->error['upload'] = !$this->allowedType[$ext] == $this->upload['type']?UPLOAD_WRONG_TYPE:false;
				if($this->ErrorCount() > 0)
					return;
					
				//vérification du poids du fichier
				$this->error['upload'] = filesize($this->upload['tmp_name']) > $this->maxFilesize?UPLOAD_MAX_FILESIZE:false;
				if($this->ErrorCount() > 0)
					return;
				
				//move le fichier
				$this->upload_path = "/tmp/mail_" . time() . $ext;
				$this->error['upload'] = !move_uploaded_file($this->upload['tmp_name'], $this->upload_path)?UPLOAD_MOVE:false;
				if($this->ErrorCount() > 0)
					return;
			}
		}
		
		public function sendMail() {
		
			if($this->ErrorCount() == 0) {
				//strip slashes
				$this->prenom = stripslashes($this->prenom);
				$this->nom = stripslashes($this->nom);
				$this->adresse = stripslashes($this->adresse);
				$this->ville = stripslashes($this->ville);
				$this->province = stripslashes($this->province);
				$this->cp = stripslashes($this->cp);
				$this->pays = stripslashes($this->pays);
				$this->tel = stripslashes($this->tel);
				$this->courriel = stripslashes($this->courriel);
				$this->produit = stripslashes($this->produit);
				$this->sujet = stripslashes($this->sujet);
				$this->message = stripslashes($this->message);
				//end strip slashes
				
				$to = $this->toEmail[$this->departement];
				
				$subject = "[" . $this->validDepartements[$this->departement] . "] Demande de contact sur Site Web";
				
				$body = "<div><strong>Nom complet : </strong> " . ($this->prenom) . " " . $this->nom . "<div>";
				if(!empty($this->adresse)) { $body .= "<div><strong>Adresse : </strong> " . ($this->adresse) . "<div>"; }
				if(!empty($this->ville)) { $body .= "<div><strong>Ville : </strong> " . ($this->ville) . "<div>"; }
				if(!empty($this->province)) {$body .= "<div><strong>Province/état : </strong> " . ($this->province) . "<div>"; }
				if(!empty($this->cp)) {$body .= "<div><strong>Code Postal : </strong> " . ($this->cp) . "<div>"; }
				if(!empty($this->pays)) {$body .= "<div><strong>Pays : </strong> " . ($this->pays) . "<div>"; }
				$body .= "<div><strong>Téléphone : </strong> " . ($this->tel) . "<div>";
				$body .= "<div><strong>Courriel : </strong> " . ($this->courriel) . "<div>";
				if(!empty($this->produit)) { $body .= "<div><strong>Nom produit : </strong> " . ($this->produit) . "<div><br /><hr />"; }
				$body .= "<div><strong>Sujet : </strong> " . ($this->sujet) . "</div><hr />";
				$body .= "<br /><font face=\"courrier\">".($this->message)."</font>";
				
				$body = $body;
				
				$mail = new PHPMailer();  // create a new object
				
				$mail->IsSMTP(); // enable SMTP
				$mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
				$mail->SMTPAuth = true;  // authentication enabled
				$mail->Host = $this->smtpHost;
				$mail->Port = $this->smtpPort; 
				$mail->Username = $this->smtpUsername;  
				$mail->Password = $this->smtpPassword;  
				
				$mail->AddReplyTo($this->courriel, $this->prenom . " " . $this->nom);         
				$mail->SetFrom("noreply@spypoint.com", "Formulaire Contact");
				$mail->Subject = $subject;
				$mail->Body = $body;
				$mail->CharSet = 'utf-8';
				$mail->AddAddress($to);
				$mail->IsHTML(true);
				
				if($this->upload !== FALSE) {
					$mail->AddAttachment($this->upload_path, $this->upload['name']);	
				}
				
				if(!$mail->Send()) {
					$this->error['upload'] = UPLOAD_SEND;
					return false;
				} else { 
					return true;
				}
				
			}
			
			$this->DeleteFile();
		}
		
		public function DeleteFile() {
			if($this->upload !== FALSE && file_exists($this->upload_path)) {
				@unlink($this->upload_path); //delete le fichier envoyé	
			}
		}
	}
	
	
	
?>