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
Related
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;
}
I have to make a huge form in a drupal website, it's for a travel agency and it's a satisfaction questionnaire.
At the beggining of the form the user choose the type of travel he do, and after this all the fields depend of the first answer, for each type of travel there is different fields to complete.
So my question is what's the best way to do this form (there is 12 type of travel, so 12 different set of fields (~50 field / type)).
I think webform could be usable with the webform conditionnal fields, but the problem I think i'll have with webform is when I want to export the data, in the CSV I'll have ALL the fields of ALL the travel type, and this will be illisible.
I search for it but doesn't find if this is possible to hide fields of the csv programmatically.
A second way I found in internet is the CCK, but I have no experience with this.
Do you have any suggestion ?
you can use hook provided by webform module :
/**
* Alter a Webform submission's data when exported.
*/
function hook_webform_csv_data_alter(&$data, $component, $submission) {
// If a value of a field was left blank, use the value from another
// field.
if ($component['cid'] == 1 && empty($data)) {
$data = $submission->data[2]['value'][0];
}
}
Found in webform.api.php into webform module folder
Also you can modify submit function by own altering form process :
function hook_form_alter(&$form, &$form_state , $form_id){
if ($form_id == 'myformid'){
$form['#submit']= array('mycustomsubmit'); // or loop on existing to suppress only unwanted submit callbacks
}
}
function mycustomsubmit($form , &$form_state){
// do your stuffs csv exports
}
I am using moodle 2.8.1
My query is regarding Moodle registration form:
I want to add a confirm password field which would be required same as Email(again) field.
And remove the Email(again) field.
And also it is not showing the phone no. fields in registration form while they are unlocked.
If you want to add fields to the registration form, you can do it using user profile fields in Site administration > Users > Accounts > User profile fields.
You have to set 'Display on signup page', which will show the field in the signup form, and 'Who is this field visible to' .
If you need more details refer: http://docs.moodle.org/26/en/User_profile_fields
Hope this helps.
We made a small open source module covering this use case.
It adds the field on the fly in the registration form, along with the necessary validation, using addRule and the compare rule.
In order to add confirm password field, you can custom login/signup_form.php file by adding one more field right after password field with compare rule like:
$mform->addElement('password', 'confirmPassword', get_string('password_confirm'), 'maxlength="32" size="12"');
$mform->setType('password', core_user::get_property_type('password'));
$mform->addRule('confirmPassword', get_string('missingpassword'), 'required', null, 'client');
$mform->addRule(array('confirmPassword', 'password'), get_string('passwords_must_match'), 'compare', 'eq', 'client');
I'm trying to setup a form where some fields needs to be hidden depending on the user role. I'm doing this in my own module using hook_form_FORM_ID_alter. No problem with common text, email or link fields (e.g. $form['field_companyname']['und'][0]['value']['#type']='hidden'). But for an image field or a multiple value file field the usual way won't work.
Anybody can give me a clue?
I think your going about this the wrong way. Since your limitation is based on roles you can just use the permissions system. Check out field permissions module.
I recommend you to use #access for the element instead of just hiding the field.
For field company name it will go like this:
$form['field_companyname']['#access'] = FALSE;
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