1

Intro - i am trying to implement Sales Force form within Drupal FAPI but now it requires to add captcha so i need to send first request to internal page so i could validate captcha and only after this i can redrect user to Sales Force.

So I want to do such thing :

  1. i have page /contacts and usual form coming from my module that was created within FAPI
  2. the form has some standard validators
  3. when it is submitted to the same page where the form is, e.g. /contacts, the validation works fine i can get the errors, but i want to send first POST request to e.g. /forms URL and validate it there, but /forms knows nothing about the form and thus validation is not called

The question is :

Basically, i need to validate form separately without rendering it on a new page. Can i do something like this :

drupal_get_form($formId)->validate() ?

Update #1 :

I am already using Captcha module but Sales Force usually is implemented this way - there should be a form on my site with the fields which SF is aware of, when this form is submitted to SF page (external site) SF gathers needed data from the POST request and redirect user back to my site ... but in this case there will no be any validation because right after submit user will be redirected to SF site, Drupal will not process such requests.

Now i really need this validation before submit so i created new page (internal) and want to trigger validation on this page but the issue is that form is located on the page "Contacts" and i am trying to send and validate data to the empty page "Forms".

If i simply send POST to Contacts (where the form is) then validation works fine, Forms does not trigger validation at all.

And as i already said i have form array that describes all fields and validators for them, then i render this array as a form on a Contacts page so i cannot understand why do i need to add hook_form_alter and create some extra validation function on a Forms page if i already described all validation rules on Contacts page?

Update #2 :

Seems that i already got the answer from Wolwerine, i would like to explain why i so willfully wanted to have initial form on a Contacts page and validate it on a Forms page.

I have Parallax site, i.e. single-page-application, which means that eveything in this application should be done without page reloading including those two requests for the issuing form :

  • validation request
  • final request to Sales Force after validation

As a result all requests are done within AJAX.

Page Contacts contains main layout and HTML form and any request to this page will return me tons of HTML which i will not be able to analyze in JS and understand whether this request and validation were successful or not.

Of course i can parse these HTML results and try to find some validation errors in this HTML but IMO this is not the case.

So i created separate page that does not include main layout and neither content, i.e. some kind of page that will work only with AJAX request and return exact type of response that i can get in this case HTML, JSON and so on without messing them up.

Thanks, i appreciate your help and upvoted all answers that look helpful.

P.S. This is how i do validation on a Forms page now :

<?php

$data = $_POST;
$form = drupal_get_form($data['form_id']);
$errors = form_get_errors();

if ($errors) {
    print json_encode(array('errors' => $errors));
    die();
}

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $data['destination']);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($c);
curl_close($c);
1
  • updated the answer.
    – Ollie
    Jul 2, 2014 at 16:59

2 Answers 2

1

Calling drupal_get_form() itself will process your form, i.e. validate & submit, but for that your $_POST should contain the form_id and other data.

1
  • thanks, this works, simply calling drupal_get_form() really causes validation to fire, of course this is not the best imnplementation but it is the simplest among those that i can imagine
    – Anonymous
    Jul 2, 2014 at 22:50
2

You can add an extra validation function using hook_form_alter.

If all you need is to add a captcha to the contact form you could try simply using the captcha module, or the mollom module.

If you've created the form yourself using a form builder function, you can add a validation function (with all your validators) to that:

function YOURMODULE_YOURFORM_form_validate($form, &$form_state) {
// Your validation code.

}
5
  • ok, but not the case because mentioned form already has validators for each field and i do not want to create absolutely new validation function and duplicate validation code i just want to use those validation rules that were described in the form array, did i miss something in your suggestion?
    – Anonymous
    Jul 2, 2014 at 16:28
  • The forms come with an array of validators. If you add a new one, all the other ones will still work, so there's no need to duplicate them. If you need the new one to be called before the other ones, use the aray_unshift option in the link. If that's not required, just increment the validators array using form['#validate'][] = example_validate;
    – Ollie
    Jul 2, 2014 at 16:31
  • 1
    @Art You don't need to duplicate any validation code. Forms can have as many validation functions as needed (#validate is an array), and elements themselves can their own unlimited number of validators via #element_validate.
    – mpdonadio
    Jul 2, 2014 at 16:32
  • added more descriptive explanation of the issue in the first post
    – Anonymous
    Jul 2, 2014 at 16:33
  • 2
    I don't think you need to do validation "on a new page". Just add a validation function to the form itself, as described. Then, you can do your POST request in the submit handler for the form.
    – Ollie
    Jul 2, 2014 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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