I create a form like this in .module file:
function form_registration_form($form, &$form_state) {
$form['registration']['email'] = array(
'#title' => t('EMAIL ADDRESS'),
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 44,
'#maxlength' => '80',
'#rules' => array(
'email',
'length[10, 50]',
)
);
$form['registration']['password'] = array(
'#title' => t('PASSWORD'),
'#type' => 'password',
'#required' => TRUE,
'#size' => 44,
'#maxlength' => '80',
);
$form['registration']['submit'] = array(
'#value' => 'SIGN IN',
'#type' => 'submit',
'#submit' => array('form_registration_handler')
);
return $form;
}
function form_registration_handler($form, &$form_state){
$email = $form_state['registration']['email'];
drupal_set_message($email);
}
However drupal always say that "Undefined index: registration in form_registration_form_submit()". I really dont know what I am doing wrong here. Any suggestions will be very useful for me. Thank you very much.
You can always enable the Devel module and dsm($form) the submit function. For instance:
function form_registration_handler($form, &$form_state){
dsm($form);
dsm($form_state);
}
By DSMing, you can easily find the value you want to use in your submit function.
Add this lines to head of your form_registration_form function:
$form = array();
$form['registration'] = array();
The notice appear becuse $form['registration'] is not declared as a void array.
Related
I'm new to Drupal 7 enviroment. I want to create a custom module form and validate it. I'm facing problems in custom validating the form. Please help. I am providing the code below.
<?php
//implementing hook permissions
function userform_2_permission(){
return array(
'submit userform_2' => array(
'title' => t('Submit Userform_2'),
'description' => t('Submit username in the field'),
),
);
}
// implementing hook menu
function userform_2_menu(){
$items = array();
$items['userform_2'] = array(
'title' => 'Userform 2',
'description' => 'Input the username',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access userform_2'),
'page callback' => 'drupal_get_form',
'page arguments' => array('userform_2_form'),
);
return $items;
}
// implementing form
function userform_2_form($form,&$form_state){
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#description' => t('Please provide your username'),
'#size' => 60,
'#maxlength' => 128,
);
$form['password'] = array(
'#title' => t('Password'),
'#type' => 'password', // it provdes the password + password_confirm field
'#size' => 60,
'#description' => 'Please provide a password',
'#maxlength' => 128,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Login'),
);
return $form;
}
I want to add form validation here. Please provide the solution for this.
function useform_2_form_validate($form,&$form_state){
// please provide solution
}
// implementing submit handler
function userform_2_form_submit($form, &$form_state){
$u_id = db_insert('userform_2') -> fields(array(
'username' => $form_state['values']['username'],
'password' => $form_state['values']['password'],
)) ->execute();
drupal_set_message(t('the username has been added'));
}
?>
Attach custom validation :
$form['#validate'][] = 'useform_2_form_validate';
But I think if you search a little more you can do it alone ;)
https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x/#validation
I have created a form in drupal. I don't know how to handle the submission. I want to do some selection from database with the values i get from form. Here is my code to create form
function q_search_form() {
$form['qsearch']['category'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Automotive', 2 => 'Real Estate'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<table width="470"><tr><td width="170">Select Category</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['city'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Calicut', 2 => 'Kochi'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">City</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['property'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'House', 2 => 'Land'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Property</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['wanto'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Sell', 2 => 'Buy'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Want to</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('id' => 'Search', 'class' => 'srch-button'),
'#prefix' => '<tr><td><a class="adv-srch" href="#">Advance Search</a></td><td>',
'#suffix' => '</td></tr></table>'
);
return $form;
}
You can write following submit function,
function q_search_form_submit($form, &$form_state) {
// To get selected values from form
$values = $form_state['values'];
// do print_r($values); exit; to check the values
}
This is pretty simple. You just have to implement the form_submit function. From the function above, I assume the name of your module to be q_search. So, here is what the submit function would look like:
function q_search_form_submit($form, &$form_state) {
// you can get the values submitted by the users using
// `$form_state['values']`, and use the database functions
// to implement your logic.
}
If you also want to validate the user inputs before the actual submit, you should add a validation function which would look like:
function q_search_form_validate($form, &$form_state) {
// validate here
}
You can use form_set_error, in the validate function if the validation fails.
You can do pretty much anything you want in the form_submit function.
function q_search_form_submit($form, &$form_state) {
$category = $form_state['values']['category'];
$city = $form_state['values']['city'];
//etc..
//use these values to query your database tables
$query = db_select($category, 'c');
//narrow results to those only in the selected city:
$query->join($city, 'cy', 'c.city = cy.cid');
//narrow results further with more ->join statements
$results = $query
->fields('c', array(fields you want from the categories table))
//fields from other tables...
->execute();
//do whatever with the $results (print on a different page, print in a table, etc).
I'm using time_tracker_simple module in Drupal. When someone selects from the drop down list and hits submit it stays on the same page but the value is not retain. How can I retain the values for the drop down list when selected.
Below is the code used in the module.
function time_tracker_simple_timer_form($form_state) {
global $user;
$active_timer = _time_tracker_simple_get_timer();
//*** Both start and start buttons must actually be on the form for #submit to work correctly
$form['stop'] = array(
'#value' => t('Stop'),
'#type' => 'submit',
'#weight' => 20,
'#access' => FALSE,
'#button_type' => 'timer_control_stop',
'#submit' => array('time_tracker_simple_timer_stop'),
);
$form['start'] = array(
'#value' => t('Start'),
'#type' => 'submit',
'#access' => FALSE,
'#weight' => 20,
'#submit' => array('time_tracker_simple_timer_start'),
'#button_type' => 'timer_control_start',
);-----This part is the drop down list------
$activities = array();
$activities[] = '(none)';
$results = db_query("SELECT * FROM {time_tracker_activity} ORDER BY weight ASC");
while ($result = db_fetch_object($results)) {
$activities[$result->taid] = $result->name;
}
if (count($activities) > 1) {
// The activity choser
$form['activity'] = array(
'#title' => t('Activity'),
'#type' => 'select',
'#weight' => 5,
'#options' => $activities,
);
}
the drupal form api allows you to pass a default value to the form item like this:
$form['activity'] = array(
'#title' => t('Activity'),
'#type' => 'select',
'#weight' => 5,
'#options' => $activities,
'#default_value' => $form_state['post']['activity']
);
I am creating a wizard from the Drupal example file and would like to refactor the segments of code that are repeated when setting up items like options and radios.
I have already tried a simple function passing "ordinary" and "preferential" but can't find a way to make it work.
Can someone give me an idea of the best way to do this?
unfactored code is as below:
function services_wizard_share_capital_classes($form, &$form_state) {
$form['share_classes']['type_of_class'] = array(
'#type' => 'select',
'#title' => t('What type of share will this class be?'),
'#options' => array(
1 => t('Ordinary'),
2 => t('Preferential'),
),
);
$form['ordinary']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Ordinary Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '1'),
),
),
);
$form['preferential']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Preferential Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '2'),
),
),
);
return $form;
}
I'm not sure about this being the best way but it's certainly a way to refactor your code:
function _services_wizard_share_capital_classes_add_el(&$form, $name, $description, $index) {
$form[$name]['share_type'] = array(
'#type' => 'item',
'#description' => t($description),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => "$index"),
),
)
);
}
function services_wizard_share_capital_classes($form, &$form_state) {
// Other code
_services_wizard_share_capital_classes_add_el($form, 'ordinary', 'You chose Ordinary Shares', 1);
_services_wizard_share_capital_classes_add_el($form, 'preferential', 'You chose Preferential Shares', 2);
// etc...
}
In a Drupal custom module, I want to make $form['link_wrapper'] conditional on a successful submission of the form but this is not a very successful way of doing this. Can anyone suggest better approach.
function my_function_my_form($form_state){
//echo "-" . $form_state['post']['op'] ."-";
//die();
global $base_root;
$form = array();
$form ['query_type'] =array (
'#type' => 'radios',
'#title' => t('Select from available Queries'),
'#options' => array(
"e_commerce_orders" => t("Query1"),
"new_orders" => t("Query2"),
"cancelled_orders" => t("Query3")),
'#required' => TRUE,
);
// only show link when submitted
if($form_state['post']['op'] == 'Submit')
{
$form['link_wrapper'] = array(
'#prefix' => '<div>',
'#value' => l("Click to View file"),
'#suffix' => '</div><br><br>',
);
}
// add submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
return $form;
}
Have you tried setting your condition in the validate hook?
Something like:
function my_function_my_form_validate($form_state){
//some condition is true
$form_state['something'] = TRUE;
}
http://api.drupal.org/api/function/hook_validate/6
This is rough. I can't remember the args for hook_validate