Drupal #limit_validation_errors does not work - drupal

I am constructing a form with Drupal FAPI and is a little bit complex one. What I want to do is to put a button and add some information when the user clicks it, so I need to skip validations with that button. I'm trying to use the #limit_validation_errors property but doesn't seem to work and is executing all validations.
I've noticed that when I put the element at the root level of the form tree it does work. This is what I have:
$form['application']['education']['add_education'] = array(
'#type' => 'submit',
'#value' => 'Add',
'#submit' => array('_education_submit'),
'#limit_validation_errors' => array(),
);
The code above doesn't work, the code below works though:
$form['add_education'] = array(
'#type' => 'submit',
'#value' => 'Add',
'#submit' => array('_education_submit'),
'#limit_validation_errors' => array(),
);

It looks like Drupal is looking for the triggering_element using the value attribute to compare; as I have another button with the same value, the system is messing up the values and taking the other button as the clicked one.
To fix the code, I only have had to change the #value property of the button. It was working when I changed the position on the tree because in that case Drupal took the right button.

Here is the example of using this property:
$form['add_education'] = array(
'#type' => 'submit',
'#value' => 'Add',
'#submit' => array('submit_function'),
'#limit_validation_errors' => array(array('_education_submit')),
);

For Drupal 8, I had to switch from a type="submit" to type="button" in order to disable errors + use Ajax.
public function buildForm(array $form, FormStateInterface $form_state) {
// ...
// Create Draft Button
$form['#prefix'] = '<div id="send-emails-manual-send-by-role-wrapper">';
$form['#suffix'] = '</div>';
$form['actions']['save_draft'] = [
'#type' => 'button',
'#value' => $this->t('Save Draft'),
'#name' => 'submit__save_draft', // TODO: Make constant
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => [$this, 'ajaxSubmitForm'],
'wrapper' => 'send-emails-manual-send-by-role-wrapper',
'method' => 'replace',
'effect' => 'fade',
],
];
return $form;
}
/**
* Submit the form using ajax
*/
public function ajaxSubmitForm(array &$form, FormStateInterface $form_state) {
$this->submitForm($form, $form_state);
return $form;
}

Related

Identify form control id in Drupal 7 Forms API

Does form controls in Forms API have id's? Below is my sample code:
function myid_user_page_form(){
$form = array();
$form['id'] = array(
'#type' => 'fieldset',
'#title' => t('ID Information'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['id']['myphoto_button'] = array(
'#type' => 'button',
'#value' => '...',
'#attributes' => array(
'onclick' => "myphoto_options();",),
);
return $form;
}
Sorry for this very simple beginner's question but how to identify my button's id in the sample above(e.g) $form['id']['myphoto_button']?
The #attributes property is used to set html attributes for the element. (Like, id, class, style, onclick, etc.)
I can see that you are using it to bind onclick handler. So, it order to give your button an id:
$form['id']['myphoto_button'] = array(
'#type' => 'button',
'#value' => '...',
'#attributes' => array(
'onclick' => "myphoto_options();",
'id' => 'YOUR-BUTTON-ID',
),
);

Drupal Commerce custom checkout pane state not working as expected

I'm attempting to add a conditionally displayed fieldset in a custom checkout pane. The fieldset is always displayed regardless of the state, unlike on a standard form.
For example, in the code below I want the "hungry_fields" fieldset displayed based on the value of the "hungry" radio button.
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['hungry'] = array(
'#type' => 'radios',
'#options' => array('yes' => t('Yes'), 'no' => t('No')),
'#required' => TRUE,
'#title' => t('I am hungry')
);
$pane_form['hungry_fields'] = array(
'#title' => 'Hungry',
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name="hungry"]' => array('value' => 'yes'),
),
),
);
return $pane_form;
}
I'm new to Drupal Commerce so it's entirely possible I'm missing something.
I fixed this by using a form callback with drupal_get_form() instead of adding the fields to the array directly.
For example:
function hungry_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
$pane_form['form'] = drupal_get_form('hungry_form');
}
function hungry_form($form, &$form_state) {
...
}
Using this method the forms work properly.

Drupal 7 form state undefined index

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.

My Form Submit Function Is Not Working

I'm developing a custom module for Drupal 6, that creates a simple form. My problem is that the submit function is not being called/processed!!! Here's my code:
function listgroups_menu(){
$items['user/%/groups-settings'] = array(
'title' => 'Groups Settings',
'page callback' => 'listgroups_groups_list',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function listgroups_groups_list ($uid){
/*
* Couple lines here to access the DB & get the user's $groups.
*/
variable_set('listgroups_database_result', $groups );
$output = drupal_get_form('listgroups_settiongs_form');
return $output;
}
/**
* Form Builder
*/
function listgroups_settiongs_form(){
$groups = variable_get('database_result', array());
//Building the form
$form['display_option'] = array(
'#type' => 'checkbox',
'#title' => t('Show my group.'),
);
$form['groups_selection'] = array(
'#type' => 'radios',
'#title' => 'Please select your group',
'#options' => $groups,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return system_settings_form($form);
}
/**
* Submition
*/
function listgroups_settiongs_form_submit($form, &$form_state){
echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('Your settings have been saved! YES!!!');
}
Now, the form rendering & the data retrival of the Db is just perfect. It's when I click the submit button, I get nothing at all!! Only the page refreshes & the messages doesn't appear!!
Any idea why?!!!!
use
return $form;
instead of
return system_settings_form($form);
and also
function xyz_form_submit($form, &$form_state){
//echo "<pre>I'm heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>";
drupal_set_message('<pre>I\'m heeeeeeeeeeeeeeeeeeeeeerr!!!</pre>Your settings have been saved! YES!!!');
}
the problem was if you use system_setting_form then it start behaving as a system setting page that is generally used to store some information in database. So making it normal form you need to return only $form.
Include a submit handler and then assign it a function
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#submit' => array('my_module_function_submit'),
);
my_module_function_submit($form, $form_state){
.
.
.
.
.
}
Refer this link https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property

Drupal - add form element on successful submission

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

Resources