0

So, I'm trying to implement a Salesforce generated Web-to-Case form on my Drupal site. I have implemented the form correctly so that it updates the Salesforce application cases, but for some reason the form doesn't get validated before submission. I have placed the form in a block with a custom module for portability and versatility. Here is the code for the form. Neither the required fields, nor my attempt to validate the form are enforced before submission. I'm pasting my code below stripped of extraneous fields to for demonstration.

function web_to_case_form($form, &$form_state){
  $form = array(
'#action' => 'https://webto.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8',
);
  $form['lastname'] = array(
  '#id' => '00N6A000009vWYv',
  '#name' => '00N6A000009vWYv',
  '#required' => TRUE,
  '#title' => t('Last Name'),
  '#type' => 'textfield',
  '#maxlength' => 30,
  '#size' => 20,
  );

   $form['maidenname'] = array(
  '#id' => '00N6A000009vWYM',
  '#name' => '00N6A000009vWYM',
  '#title' => t('Maiden Name'),
  '#required' => TRUE,
  '#type' => 'textfield',
  '#maxlength' => 30,
  '#size' => 20,
  );

  $form['email'] = array(
  '#id' => 'email',
  '#title' => t('Email'),
  '#name' => 'email',
  '#required' => TRUE,
  '#type' => 'textfield',
  '#maxlength' => 80,
  '#size' => 20,
  );

   $form['phone'] = array(
  '#id' => 'phone',
  '#title' => t('Phone'),
  '#name' => 'phone',
  '#required' => TRUE,
  '#type' => 'textfield',
  '#maxlength' => 80,
  '#size' => 20,
  '#element_validate' => array('element_validate_number '),
   );

  $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Submit'),
   );
return $form;
}

function web_to_case_form_validate($form, &$form_state) {
  $email = $form_state['values']['email'];
  // Verify that the email address is valid.
  if (!valid_email_address($email)) {
    form_set_error('email', t('You must provide a valid email address.'));
  }
}

Is this because the form is being submitted to an external URL? If that's the case, how to I validate a form before submission? Otherwise, not sure what's going on here. I've even tried to add a validate element to one of the form elements, but that doesn't seem to work either.

  • Sry if wrong...is there two underscore in web_to_case__form_validate but not in form name web_to_case_form – Kstack Oct 17 '17 at 15:42
  • good catch, but that didn't fix it :( – nizz0k Oct 17 '17 at 15:44
1

Is this because the form is being submitted to an external URL?

Yes, the submit handler for a form in your website won't be executed unless that form is submitted back to your website.

If that's the case, how to I validate a form before submission?

If you mean server-side (non-JS) validation, and you want the user to continue on to the remote URL as if they'd submitted the form as normal, you can't. There's simply no way to do that.

If you don't need the user to be forwarded on to the remote URL after submission, and the remote website allows it, you could just send a POST request from the submit handler with the data you've received from the Drupal form submission.

If you're even luckier, they'll have an API you can use instead.

  • Thanks Clive, so the form doesn't need to proceed to the external URL, in fact the users should be sent back to the home page on submission. Is this something I can handle with adding ajax? – nizz0k Oct 17 '17 at 16:04
  • You mean submit directly to the remote via Js? Definitely possible but you’d need to check that works from the other side. Otherwise a normal POST in the submit handler should be fine – Clive Oct 17 '17 at 16:40
  • I found your question from 2015 which addressed this, I'm trying to come up with a custom submit handler with POST. – nizz0k Oct 17 '17 at 16:41
0

If anyone else runs into this problem Webform Remote Post does this painlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.