Commit ea1280d7 authored by webchick's avatar webchick

Issue #1805688 by fubhy: Added Support methods as #element_validate() callbacks.

parent 92a5b299
...@@ -1422,8 +1422,8 @@ function _form_validate(&$elements, &$form_state, $form_id = NULL) { ...@@ -1422,8 +1422,8 @@ function _form_validate(&$elements, &$form_state, $form_id = NULL) {
// Call any element-specific validators. These must act on the element // Call any element-specific validators. These must act on the element
// #value data. // #value data.
elseif (isset($elements['#element_validate'])) { elseif (isset($elements['#element_validate'])) {
foreach ($elements['#element_validate'] as $function) { foreach ($elements['#element_validate'] as $callback) {
$function($elements, $form_state, $form_state['complete_form']); call_user_func_array($callback, array(&$elements, &$form_state, &$form_state['complete_form']));
} }
} }
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
* Helper module for the form API tests. * Helper module for the form API tests.
*/ */
use Drupal\form_test\Callbacks;
/** /**
* Implements hook_menu(). * Implements hook_menu().
*/ */
...@@ -385,11 +387,13 @@ function system_form_form_test_alter_form_alter(&$form, &$form_state) { ...@@ -385,11 +387,13 @@ function system_form_form_test_alter_form_alter(&$form, &$form_state) {
* should be contained in the rebuilt form. * should be contained in the rebuilt form.
*/ */
function form_test_validate_form($form, &$form_state) { function form_test_validate_form($form, &$form_state) {
$object = new Callbacks();
$form['name'] = array( $form['name'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => 'Name', '#title' => 'Name',
'#default_value' => '', '#default_value' => '',
'#element_validate' => array('form_test_element_validate_name'), '#element_validate' => array(array($object, 'validateName')),
); );
$form['submit'] = array( $form['submit'] = array(
'#type' => 'submit', '#type' => 'submit',
...@@ -403,42 +407,6 @@ function form_test_validate_form($form, &$form_state) { ...@@ -403,42 +407,6 @@ function form_test_validate_form($form, &$form_state) {
return $form; return $form;
} }
/**
* Form element validation handler for 'name' in form_test_validate_form().
*/
function form_test_element_validate_name(&$element, &$form_state) {
$triggered = FALSE;
if ($form_state['values']['name'] == 'element_validate') {
// Alter the form element.
$element['#value'] = '#value changed by #element_validate';
// Alter the submitted value in $form_state.
form_set_value($element, 'value changed by form_set_value() in #element_validate', $form_state);
$triggered = TRUE;
}
if ($form_state['values']['name'] == 'element_validate_access') {
$form_state['storage']['form_test_name'] = $form_state['values']['name'];
// Alter the form element.
$element['#access'] = FALSE;
$triggered = TRUE;
}
elseif (!empty($form_state['storage']['form_test_name'])) {
// To simplify this test, just take over the element's value into $form_state.
form_set_value($element, $form_state['storage']['form_test_name'], $form_state);
$triggered = TRUE;
}
if ($triggered) {
// Output the element's value from $form_state.
drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state['values']['name'])));
// Trigger a form validation error to see our changes.
form_set_error('');
}
}
/** /**
* Form validation handler for form_test_validate_form(). * Form validation handler for form_test_validate_form().
*/ */
......
<?php
/**
* @file
* Definition of Drupal\form_test\Callbacks.
*/
namespace Drupal\form_test;
/**
* Simple class for testing methods as element validation callbacks.
*/
class Callbacks {
/**
* Form element validation handler for 'name' in form_test_validate_form().
*/
public function validateName(&$element, &$form_state) {
$triggered = FALSE;
if ($form_state['values']['name'] == 'element_validate') {
// Alter the form element.
$element['#value'] = '#value changed by #element_validate';
// Alter the submitted value in $form_state.
form_set_value($element, 'value changed by form_set_value() in #element_validate', $form_state);
$triggered = TRUE;
}
if ($form_state['values']['name'] == 'element_validate_access') {
$form_state['storage']['form_test_name'] = $form_state['values']['name'];
// Alter the form element.
$element['#access'] = FALSE;
$triggered = TRUE;
}
elseif (!empty($form_state['storage']['form_test_name'])) {
// To simplify this test, just take over the element's value into $form_state.
form_set_value($element, $form_state['storage']['form_test_name'], $form_state);
$triggered = TRUE;
}
if ($triggered) {
// Output the element's value from $form_state.
drupal_set_message(t('@label value: @value', array('@label' => $element['#title'], '@value' => $form_state['values']['name'])));
// Trigger a form validation error to see our changes.
form_set_error('');
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment