Creating a form having two file type element, i want to check that if user uploaded file any file, then user must upload both file, so is there any way to add a validate function in system_settings_form.
1 Answer
The system_settings_form
should be used to set simple variables for your module, see the approved answer and related comment here:
Is "system_settings_form" voodoo?
If you want to be sure both form fields are filled out during submission you don't need a validate function -- you can set both fields as being required when you create your form array:
$form['SOMETHING']['#required'] = TRUE;
where something is your form field key.
If you have complex logic and your form isn't simply setting a configuration variable you will call with variable_get
or variable_set
later -- then implement a custom form_validate
and form_submit
function for your form as your form is not simply a variable settings form.
See the Form API Handbook. Also of use is this drupal $form
lifecycle or workflow illustration. Lastly form validation helpers and submit handlers are touched on briefly on this page of the handbook.
EDIT:
Just to complete my answer, hook_form_alter can change many aspects of a form used by drupal.
EDIT 2:
You can add a custom validator on the form you use to build return system_settings_form($form);
for an example see the Commerce Admin include file here: http://cgit.drupalcode.org/commerce/tree/includes/commerce_ui.admin.inc
They set:
$form['#validate'][] = 'commerce_currency_settings_form_validate';
in their form builder function then they provide a validation function:
/**
* Form validate handler for the currency settings form.
*/
function commerce_currency_settings_form_validate($form, &$form_state) {
// Ensure the default currency is always enabled.
$default = $form_state['values']['commerce_default_currency'];
$form_state['values']['commerce_enabled_currencies'][$default] = $default;
}
-
Thanks for your answer,I can't make that element a required field because form also having other element but i can do it with form alter or i just check my condition in submit handler, i Just wanna know that there is no such term (validate) in system_settings_form Commented Jul 8, 2014 at 6:30
-
please see my second Edit which I hope answers your question.– tenkenCommented Jul 8, 2014 at 15:44