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
Related
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.
I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.
In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:
function vocabt_menu() {
$items['scores/admin'] = array(
'title' => 'Check Your Student\'s Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_admin_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
$items['scores/student'] = array(
'title' => 'Check My Scores',
'page callback' => 'drupal_get_form',
'page arguments' => array('vocabt_student_login_form'),
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
}
function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} else {
$values = array();
}
$schools = vocabt_get_school_options();
$form['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_option' => 'Select',
);
$form['username'] = array(
'#type' => 'textfield',
'#title' => 'User Name',
'#required' => TRUE,
);
$form['password'] = array(
'#type' => 'password',
'#title' => 'Password',
'#required' => TRUE,
);
$form['actions'] = array();
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Log In',
'#attributes' => array('class' => array('button blue')),
);
return $form;
}
function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
$values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
$values = $info;
} else {
$values = array();
}
$form['left'] = array(
'#prefix' => '<div class="lookup">',
'#suffix' => '</div>',
);
$form['right'] = array(
'#prefix' => '<div class="results">',
'#suffix' => '</div>',
);
$schools = vocabt_get_school_options();
$form['left']['school'] = array(
'#type' => 'select',
'#title' => 'Select Your School',
'#required' => TRUE,
'#options' => $schools,
'#default_value' => !empty($values['school']) ? $values['school'] : '',
'#empty_value' => 'Select',
);
$form['left']['id'] = array(
'#type' => 'textfield',
'#title' => 'Enter your student ID',
'#required' => TRUE,
'#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
'#type' => 'submit',
'#value' => 'Check',
'#attributes' => array('class' => array('button blue')),
);
if(!empty($form_state['storage']['error'])) {
$results = '<div class="blackbox">';
$results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
$results .= '</div>';
} elseif(empty($values['id'])) {
$results = '<div class="blackbox">';
$results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
$results .= '</div>';
} else {
$results = vocabt_get_student_results($values['school'], $values['id']);
}
$form['right']['results'] = array('#markup' => $results);
return $form;
}
My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?
I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you
It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.
Add a new item in your hook_menu() implementation like this:
$items['scores/check'] = array(
'title' => 'Check Scores',
'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
)
Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.
function vocabt_scores_page() {
$output = array();
$output['admin'] = drupal_get_form('vocabt_admin_login_form');
$output['student'] = drupal_get_form('vocabt_student_login_form');
return $output;
}
Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!
You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.
You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.
function modulename_theme() {
return array(
'page_name' => array(
'template' => 'page_name',
'arguments' => array('link' => NULL),
),
);
}
You could find more information here
http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7
Hope this helps you... :)
I have two fields
$form["field_num_males"]
$form["field_num_females"]
I need to dynamically populate the field $form["field_gender_total"] with the summation of them, before the submission (AJAX).
How can this be done with Drupal 7?
Thanks!
Yes it can be done with Ajax. The following code will automatic update the total when the text field is updated:
function gender_total_menu()
{
$items = array();
$items['test'] = array(
'title' => Gender total,
'page callback' => 'drupal_get_form',
'page arguments' => array('gender_total_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}
function gender_total_form($form, &$form_state)
{
$total = 0;
if (array_key_exists('values', $form_state) &&
array_key_exists('field_num_males', $form_state['values']) &&
array_key_exists('field_num_females', $form_state['values'])
) {
$total = $form_state['values']['field_num_males'] + $form_state['values']['field_num_females'];
}
$form = array();
$form["field_num_males"] = array(
'#type' => 'textfield',
'#title' => t("Number of males"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form["field_num_females"] = array(
'#type' => 'textfield',
'#title' => t("Number of females"),
'#default_value' => 0,
'#ajax' => array(
'callback' => 'ajax_update_callback',
'wrapper' => 'wrapper',
),
);
$form['total'] = array(
'#markup' => '<p> Total: ' . $total . '</p>',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_update_callback($form, $form_state)
{
return $form['total'];
}
Try the Computed Field module
Computed Field is a very powerful CCK field module that lets you add a
custom "computed fields" to your content types. These computed fields
are populated with values that you define via PHP code. You may draw
on anything available to Drupal, including other fields...
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...
}
I'm trying to use Drupal 7's entities and field API to correctly build a new module. What I have been unable to understand from the documentation is the correct way to use the new API to create a 'content type' (not a node type) with a number of set fields, such as Body.
I'm trying to set up the entity using hook_entity_info, then I believe I need to add the body field using field_create_instance, but I can't seem to get it to work.
In mycontenttype.module:
/**
* Implements hook_entity_info().
*/
function mycontenttype_entity_info() {
$return = array(
'mycontenttype' => array(
'label' => t('My Content Type'),
'controller class' => 'MyContentTypeEntityController',
'base table' => 'content_type',
'uri callback' => 'content_type_uri',
'entity keys' => array(
'id' => 'cid',
'label' => 'title',
),
'bundles' => array(
'mycontenttype' => array(
'label' => 'My Content Type',
'admin' => array(
'path' => 'admin/contenttype',
'access arguments' => array('administer contenttype'),
),
),
),
'fieldable' => true,
),
);
return $return;
}
/**
* Implements hook_field_extra_fields().
*/
function mycontenttype_field_extra_fields() {
$return['mycontenttype']['mycontenttype'] = array(
'form' => array(
'body' => array(
'label' => 'Body',
'description' => t('Body content'),
'weight' => 0,
),
),
);
return $return;
}
Then does this go in the .install file?
function mycontenttype_install() {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('survey'),
'translatable' => TRUE,
);
field_create_field($field);
$instance = array(
'entity_type' => 'mycontenttype',
'field_name' => 'body',
'bundle' => 'mycontenttype',
'label' => 'Body',
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
field_create_instance($instance);
}
I think your problem is that if node module is installed, there is already a field named 'body'. You should either re-name your field to something like 'mycontenttype_body' (comment.module uses comment_body), or re-use the 'body' field and skip the adding the field part and skip to adding the instance of it. The former is recommended over the latter.
Every field has an array property, entity_types, which limits the entities to which the field can be attached.
The best Drupal solution I can find, hook_field_create_field, can alter fields as they are created, but that's no good for the body field which is created on installation.
So my solution is just to edit the database directly in my hook_install
$data_col = db_query("SELECT data from field_config where field_name = 'body'")->fetchAssoc();
$data = unserialize($data_col['data']);
$data['entity_types'][] = 'MY_ENTITY_TYPE';
db_update('field_config')
->fields(array('data' => array('data' => serialize($data))))
->condition('field_name', 'body')
->execute();
just started down the same path here is a video from fago
Here's a nice repo to start: Lawmakers entity