コード例 #1
0
 /** Valide les champs d'un formlaire
  * @param array $array : Champs a lire
  * @return boolean
  */
 function validateForm($array, $values = array(), $from = '', $partial = false, $user = 0)
 {
     $user_id = empty($user) ? get_current_user_id() : $user;
     foreach ($array as $attribute_id => $attribute_definition) {
         $values_array = !empty($values) ? $values : $_POST['attribute'];
         $value = !empty($values_array[$attribute_definition['data_type']][$attribute_definition['name']]) ? $values_array[$attribute_definition['data_type']][$attribute_definition['name']] : '';
         // Si le champ est obligatoire
         if (empty($value) && $attribute_definition['required'] == 'yes') {
             $this->add_error(sprintf(__('The field "%s" is required', 'wpshop'), __($attribute_definition['label'], 'wpshop')));
         }
         if ($partial == false && $attribute_definition['_need_verification'] == 'yes') {
             $value2 = $values_array[$attribute_definition['data_type']][$attribute_definition['name'] . '2'];
             if ($value != $value2) {
                 $this->add_error(sprintf(__('The  "%s" confirmation is incorrect', 'wpshop'), __($attribute_definition['label'], 'wpshop')));
             }
         }
         if (!empty($value) && !empty($attribute_definition['type'])) {
             switch ($attribute_definition['frontend_verification']) {
                 case 'email':
                     $email_exist = email_exists($value);
                     if (!is_email($value)) {
                         $this->add_error(sprintf(__('The field "%s" is incorrect', 'wpshop'), $attribute_definition['label']));
                     } elseif (empty($from) && ($user_id > 0 && !empty($email_exist) && $email_exist !== $user_id || !empty($email_exist) && $user_id <= 0)) {
                         $this->add_error(__('An account is already registered with your email address. Please login.', 'wpshop'));
                     }
                     break;
                 case 'postcode':
                     if (!wpshop_tools::is_postcode($value)) {
                         $this->add_error(sprintf(__('The field "%s" is incorrect', 'wpshop'), __($attribute_definition['label'], 'wpshop')));
                     }
                     break;
                 case 'phone':
                     if (!wpshop_tools::is_phone($value)) {
                         $this->add_error(sprintf(__('The field "%s" is incorrect', 'wpshop'), __($attribute_definition['label'], 'wpshop')));
                     }
                     break;
                 case 'username':
                     $username_exists = username_exists($value);
                     // On s'assure que le nom d'utilisateur est libre
                     if (!validate_username($value)) {
                         $this->add_error(__('Invalid email/username.', 'wpshop'));
                     } elseif ($user_id > 0 && !empty($username_exists) && $username_exists !== $user_id || !empty($username_exists) && $user_id <= 0) {
                         $this->add_error(__('An account is already registered with that username. Please choose another.', 'wpshop'));
                     }
                     break;
             }
         }
     }
     return $this->error_count() == 0;
 }