By marcus.follrud on
heya!
I've been trying to find solution on the web and trying myself for almost 4 hours now but I can't get it working.
I need help with form validation.
I started off to add a menu item containing:
$items['sponsor'] = array(
'title' => t('Sponsor site with payson'),
'description' => 'Blah blah',
'page callback' => 'drupal_get_form',
'page arguments' => array('payment_view_payson_page'),
'access arguments' => array('access payment'),
'type'=> MENU_NORMAL_ITEM
);
The menu shows up, and the page is displayed correctly. But when i try to submit it wont execute the _validate function.
I read the chapter in the documentation about hook_admin() (which works very well with my module) and thought it would be slightly the same as hook_admin() is not a core function?
But well. It won't work. Probably my code is messed up now but I can't get it straigt..
Here how it looks:
function payment_view_payson_page(&$form_state) {
//Fetch user information
global $user;
//Collect the information we need for our buyer
$buyer_email = $user->mail;
//The form..
$form['payment_value'] = array(
'#type' => 'textfield',
'#title' => t('Payment value'),
'#size' => 6,
'#maxlength' => 6,
'#description' => t('Enter the amount of money your willing to sponsor with. Current currency is <strong>' . variable_get('payment_currency','SEK') . '</strong>'),
'#required' => TRUE,
'#default_value' => '20'
);
$form['payment_buyer_email'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail'),
'#description' => t('Please confirm that this is your <i>real</i> email.'),
'#required' => TRUE,
'#default_value' => $buyer_email
);
$form['controls']['submit'] = array(
'#type' => 'submit',
'#value' => 'Sponsor',
'#submit' => array('payment_view_payson_page_validate')
);
$form['#validate'][] = 'payment_view_payson_page_validate';
return $form;
}
function payment_view_payson_page_validate($form, &$form_state)
{
echo "Test";
}
Does anyone know. Any tips would be great!
Comments
You actually do not need the
You actually do not need the
$form['#validate'][] = 'payment_view_payson_page_validate';Drupal will know to send it to your validate function.
See if that works.
Baldwin Louie,
BitSprout LLC
http://www.bitsprout.net
Hello baldwinlouie, Thank
Hello baldwinlouie,
Thank you for your prompt reply :).
Sadly, it still doesn't work. Nothing happens pressing the submit button. Well. The page gets refreshed and print_r($_POST); just gives an Array( )
How does..
drupal know to automatically call this function? It's fine to accept that it "just does" but i'd like to know more specifically what part of the api is responsible for making that "glue" "happen".
i've searched online for a long time now and can't find any of the "behind the magic" important stuff.
in theory, if an api is designed well, you don't necessarily need to know what's happening under the hood, but i just spend 24 hours trying to debug a silly form api question that should've taken 5 minutes.
for instance, say you're on a page (menu path) that happens to be a form itself. on that same page, the sidebar hypothetically contains 4 separate form blocks with submit buttons. lets say you choose a submit on one of the form blocks. how does drupal (the framework / api) route the submit on the block to the appropriate form handler on the backend? how does it know to tie the form id presented to the form id submitted? is is using a session var for this? if so where is it set (in what module)?
thanks
Use the Devel module
Install the devel module and use it to check the form id just in case something is messing with it. It also gives dprint_r() which is good for printing debug output.
Do you have a hook_submit() implementation for this form? Is that working?