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 :
- i have page /contacts and usual form coming from my module that was created within FAPI
- the form has some standard validators
- 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);