Drupal 7 Hook for Webform Pagebreak Submission - forms

When inside a webform, that was created with the module webform - I need to validate / check the entries of a page when submit/"next step" is clicked- So before the user moves to the next page(pagebreak).
I want to check if the email address was entered already - if so show this as an error without moving to the next page.
I need a hook .. or a way to tell drupal "go to step 1" instead of showing step 2.

You can use a hook to check the value of the form. In hook form alter you can check your field values.
$form['#validate'][] = 'function_to_validate_field';
function check_for_company_validate($form, &$form_state) {
//do stuff
}
You can see this for drupal 7 to create an error:
https://api.drupal.org/api/drupal/includes!form.inc/function/form_set_error/7

Related

How to cross-validate contact form 7 form fields?

I want to make two sperate check box groupe. Lets say
Front end courses
-Bootstrap
-React Js
-Angular js
Back end courses
-C#
-Node
-PHP
on submitting form I want to make sure at least one course is selected by the user. Lets say a student selects ReactJs.
I have tried below code but it makes one option mandatory for all checkbox groups
[Checkbox* checkbox-11 ",Bootstrap" "React Js" "Angular JS"]
[Checkbox* checkbox-12 ",C#" "Node" "PHP"]
How can I make at least one option selected mandatory from any of the checkbox groups
field validation takes place post form submission. To solve this problem you need to make neither field mandatory, and then do a custom validation on the server side. CF7 plugin allows you to add custom validation to individual field but does not allow you to validate a field wrt to the value of other fields in the form.
To do this you need an extension such as the Smart Grid-layout plugin which has a validation hook that gives you access to the entire form's submitted data and therefore cross-validate your fields,
add_filter('cf7sg_validate_submission', 'cross_validate_submission',10,3);
function cross_validate_submission($validation, $submission, $form_key){
if(empty($submission['fe-courses']) && empty($submission['be-courses'])){
$validation['fe-courses']='You need to select at least 1 course!';
}
return $validation;
}

How to hide certain fields on the User Edit form in Drupal?

So I have three types of users - admin, LA admin and users. I am trying to set it up so that admins and LA admins cannot edit the username, password and timezone for users. I am talking about the default user edit form for admins and the form ID is "user-profile-form".
I have created a custom module but this doesn't seem to be working. Any idea what I might be doing wrong?
Even the var_dump does not seem to be outputting. I have cleared the cache and verified that the module is enabled.
function profile_change_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'user-profile-form') {
var_dump ($form);
hide($form['account']['pass']);
hide($form['account']['current_pass_required_values']);
hide($form['account']['current_pass']);
}
}
If you use hide() you will remove the field, but hide is more for "delaying" field rendering... Like you hide it, but then (in template file) you print it at some other place.
Because, if you don't print it later it won't be rendered in the page, won't be saved, if it's mandatory you'll get validation error and so on.
If you want to hide it, so user can not see it but you want the form to keep previous value of the field use something like:
$form['field_yourfield']['#access'] = FALSE;
and if you want it to be visible, but disabled (user can't change it's value) then:
$form['field_yourfield']['#disabled'] = TRUE;
I am assuming you module name is PROFILE_CHANGE & so that you have used it in the format of hook_form_alter(), where you have replaced hook with your module name profile_change.
You have put 3 '=' sign where you are giving condition to check form id, which is user-profile-form. I have put simple equal sign which is '==' & it's working.
function profile_change_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'user-profile-form') {
hide($form['account']['pass']);
hide($form['account']['current_pass_required_values']);
hide($form['account']['current_pass']);
}
}
Don't use var_dump(), You should always use DEVEL & check the output of $form like dpm($form) just after your hook function for form alter. This will give you all info about form, where ever you have a form on your page.
function profile_change_form_alter(&$form, $form_state, $form_id) {
dpm($form);
}
Actually I just had to change user-profile-form to user_profile_form in my code for it to work. For some reason, drupal requires underscores.

Without submit button validate form elements in Magento

To add multiple steps before submit form. like:
Registration page:
Step 1 - Enter first, last name and email (validate on click of next button and show next step)
Step 2 - Enter Password and Confirm password and submit
Step 1 and step 2 are separate DIV's (Show/hide on Next Click)
You can give a try of this JS snippet:
var formToValidate = $('place the id of the form, that I want to validate');
var validator = new Validation(formToValidate);
if(validator.validate()) {
/* The logic, that you want to execute if you pass the validation. */
......
......
}
Good luck!

Unique field in user registration form in Drupal 7

I am working in drupal 7. I need a field in the user registration form which needs to be unique. It is an integer field which stores the ID of the user. I have been searching around for a few hours now without any development. Can someone guide me through this?
Thank You.
You can add a custom "staff id" field to the user entity type from admin/config/people/account/fields (configuration->people->account settings). You can add a new integer field, and mark it to display in the registration form, and/or required.
To check that the field value is unqiue you will need to use a custom module. In your custom module use the form_id_form_alter hook to add a custom validation to the registration form. Then during validation you can check that the value does not already exist in the database and return a form error.
Example of the custom module:
<?php
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id){
$form['#validate'][] = 'mymodule_user_registration_validate';
}
function mymodule_user_registration_validate(&$form,&$form_state){
$staff_id = $form_state['values']['staff_id_field'];
$check_unique_query = 'SELECT field_staff_id_value FROM {field_data_staff_id} WHERE field_staff_id_value = :staff_id LIMIT 1';
//if a result is returned, that means the $staff_id exists
if (db_query($check_unique_query,array(':staff_id'=>$staff_id))->fetchField()){
form_set_error('staff_id_field','This Staff ID is already in use');
}
}
Have you tried the Unique field module? Pretty sure it does what you need here.
Field validation does the trick https://drupal.org/project/field_validation. You can set any rules per field even on the registration form

How to override default user profile fields in drupal 7 ising form API?

I'd like to know how to override/change the default fields in user profile such as: button Save, name, timezone etc.
I'd like to alter, delete('cause i don't need them) some of them.
To alter the user profile i use the hook: hook_form_alter using which i managed to add my own fieldds to user profile. But now i want to alter the default fields. How can i do it?
It is possible with the hook_form_alter, though it is better to use hook_form_FORM_ID_alter!
To be able to alter the form you need to know the structure of the arrays, the easiest way to get to know this is by installing the Devel module. Then you can view the structure by placing dpm($form); inside your alter function.
You can use this function on your custom module or in your theme (in template.php file).
Usually user profile form_id is user_profile_form. A simple example would be:
function mymodule_form_user_profile_form_alter(&$form,$form_state,$form_id){
$form['timezone']['#access'] = FALSE; //remove the "timezone" field from the form (default value is still saved)
$form['field_somefield']['#weight'] = -50; //move the field up
$form['actions']['submit']['#value'] = t('Add this content now'); //change the submit button text
}
For a good tutorial see the Lullabot tutorial here (is for drupal 6 but works the same way for d7!).
API: hook_form_FORM_ID_alter