Answers.Body: <p>Does applying this patch solve your problem? <a href="https://github.com/civicrm/civicrm-core/pull/25823" rel="nofollow noreferrer">https://github.com/civicrm/civicrm-core/pull/25823</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-24T03:38:35.823Z
Answers.Text: I tried adding that code but only seeing this error default popup with default message: `Sorry an error occurred and your information was not saved`
Answers.UserDisplayName: Shubham Belwal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-08-23T16:57:30.860Z
Answers.LastActivityDate: 2024-08-23T16:57:30.860Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I have created a form using the CiviCRM form builder. and Added custom validation code. The goal of this validation is to check if both the email and phone number provided in the form already exist in the system and display an error message( alert box ) to user on frontend if they do.</p>
  <p>here is code</p>
  <pre><code>&lt;?php
  
  namespace Civi;
  
  use CRM_custom_ExtensionUtil as E;
  use Civi\Afform\Event\AfformValidateEvent;
  use Civi\Core\Service\AutoSubscriber;
  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  
  class UniqueUserValidation extends AutoSubscriber implements EventSubscriberInterface
  {
      /**
       * @return array
       */
      public static function getSubscribedEvents()
      {
          return [
              'civi.afform.validate' =&gt; ['onAfformValidate'],
          ];
      }
  
      public static function onAfformValidate(AfformValidateEvent $event)
      {
          $formName = $event-&gt;getAfform()['name'] ?? '';
          if ($formName !== 'afformIndividualRegistration1') {
              return;
          }
      
          $entityValues = $event-&gt;getEntityValues();
          $phone = $entityValues['Individual1'][0]['joins']['Phone'][0]['phone'] ?? '';
          $email = $entityValues['Individual1'][0]['joins']['Email'][0]['email'] ?? '';
      
          $contactResult = \Civi\Api4\Contact::get()
              -&gt;addSelect('id', 'email_primary.email', 'phone_primary.phone')
              -&gt;addWhere('email_primary.email', '=', $email)
              -&gt;addWhere('phone_primary.phone', '=', $phone)
              -&gt;addWhere('contact_type', '=', 'Individual')
              -&gt;addWhere('is_deleted', '=', 0)
              -&gt;setLimit(1)
              -&gt;execute();
      
          if (empty($contactResult)) {
              return;
          }
      
          $contact = $contactResult-&gt;first();
          $errorMessages = [];
      
          if ($contact['email_primary.email'] === $email &amp;&amp; $contact['phone_primary.phone'] === $phone) {
              $errorMessages[] = &quot;Both the email address '$email' and the phone number '$phone' are already in use.&quot;;
          }
      
  if (!empty($errorMessages)) {
              $alertMessage = implode(&quot;\n&quot;, $errorMessages);
              $script = &quot;&lt;script type='text/javascript'&gt;
                  alert('&quot;.addslashes($alertMessage).&quot;');
              &lt;/script&gt;&quot;;
              
              \CRM_Core_Session::setStatus($script, 'Form Error', 'error');
              $event-&gt;set('errors', $errorMessages); // This line may be necessary to trigger validation errors
          }
      }
  }
  
  </code></pre>
  <p>The custom validation code correctly finds duplicate emails and phone numbers and sets the error message. However, I can't get the error message to show up as an alert box on the frontend. How can I make the error message appear as an alert to the user?
  I add javascript but its not working.</p>
  <p>I got this popup in crm only , not in frontend
  <a href="https://i.sstatic.net/K5Ob0nGy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/K5Ob0nGy.png" alt="enter image description here" /></a></p>
  
Tags: <formbuilder><validation>
Title: Issue with Custom Validation Error Message Display in alert box
ThreadText: # Issue with Custom Validation Error Message Display in alert box
  
  (Asked by: username_0 on 2024-08-23)
  
  I have created a form using the CiviCRM form builder. and Added custom validation code. The goal of this validation is to check if both the email and phone number provided in the form already exist in the system and display an error message( alert box ) to user on frontend if they do.
  here is code
   ['onAfformValidate'],
          ];
      }
  
      public static function onAfformValidate(AfformValidateEvent $event)
      {
          $formName = $event->getAfform()['name'] ?? '';
          if ($formName !== 'afformIndividualRegistration1') {
              return;
          }
      
          $entityValues = $event->getEntityValues();
          $phone = $entityValues['Individual1'][0]['joins']['Phone'][0]['phone'] ?? '';
          $email = $entityValues['Individual1'][0]['joins']['Email'][0]['email'] ?? '';
      
          $contactResult = \Civi\Api4\Contact::get()
              ->addSelect('id', 'email_primary.email', 'phone_primary.phone')
              ->addWhere('email_primary.email', '=', $email)
              ->addWhere('phone_primary.phone', '=', $phone)
              ->addWhere('contact_type', '=', 'Individual')
              ->addWhere('is_deleted', '=', 0)
              ->setLimit(1)
              ->execute();
      
          if (empty($contactResult)) {
              return;
          }
      
          $contact = $contactResult->first();
          $errorMessages = [];
      
          if ($contact['email_primary.email'] === $email && $contact['phone_primary.phone'] === $phone) {
              $errorMessages[] = "Both the email address '$email' and the phone number '$phone' are already in use.";
          }
      
  if (!empty($errorMessages)) {
              $alertMessage = implode("\n", $errorMessages);
              $script = "";
              
              \CRM_Core_Session::setStatus($script, 'Form Error', 'error');
              $event->set('errors', $errorMessages); // This line may be necessary to trigger validation errors
          }
      }
  }
  
  
  The custom validation code correctly finds duplicate emails and phone numbers and sets the error message. However, I can't get the error message to show up as an alert box on the frontend. How can I make the error message appear as an alert to the user?
  I add javascript but its not working.
  I got this popup in crm only , not in frontend
  
  **Tags:** formbuilder, validation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-08-23. Score: 0
  
  Does applying this patch solve your problem? <https://github.com/civicrm/civicrm-core/pull/25823>
  
  > **Comments:**
  > **username_0**: I tried adding that code but only seeing this error default popup with default message: `Sorry an error occurred and your information was not saved`  
  
  ---
  
  