0
votes

I am trying to get value in code from drupal form. When it submit the form. I will do the validation on the data.

First in

**
 * Implements hook_form_FORM_ID_alter() for user_register_form().
 */
function username_generator_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['account']['#type'] = 'fieldset';
    $form['account']['name']['#suffix'] = '</div>';
  }

 drupal_add_js('function random_area_wrapper(name) { jQuery("#random-area-wrapper").html(name).slideDown(); }', 'inline');


  $form['Personal Information']['profile_date']['#default_value'] =  array(
    'day' => date('j'),
    'month' => date('n'),
    'year' => date('Y')
  );
  $form['Personal Information']['profile_date']['#after_build'] = array('__set_year_range');

  if (!user_access('administer users')) {
    $form['Personal Information']['profile_date']['#element_validate'] = array('age_validate');
  }
}

Then in the function age_validate, I try to print out the value, but find that in $form_state , the profile_date data is exist, However, I cannot find the data in $element.

Here is the function:

function age_validate($element, &$form_state) {
  $rid = autoassignrole_get_active_path_rid();
  $rid = array_diff($rid, array(0));
  $rid = reset ($rid);

  if (_role_access($rid, 'allow kids registration' ) || empty($rid)) {

  print_r($element); 
  print_r($form_state);

And this is the extract of the print out of $form_state.

[values] => Array
        (
          [profile_date] => Array
                (
                    [day] => 31
                    [month] => 8
                    [year] => 2001
                )
)

[input] => Array
        (

            [profile_date] => Array
                (
                    [day] => 31
                    [month] => 8
                    [year] => 2001
                )

        )

You can see the values of profile_date is in input and values is here

How ever when I try to

 print_r($element); 

The result is here

Array
(
    [#default_value] => Array
        (
            [day] => 31
            [month] => 8
            [year] => 2017
        )

    [#after_build] => Array
        (
            [0] => __set_year_range
        )

    [#element_validate] => Array
        (
            [0] => age_validate
        )

    [#tree] => 
    [#parents] => Array
        (
            [0] => profile_date
        )

    [#array_parents] => Array
        (
            [0] => Personal Information
            [1] => profile_date
        )

    [#weight] => 0
    [#processed] => 
    [#required] => 
    [#attributes] => Array
        (
        )

    [#title_display] => before
    [#id] => edit-profile-date--2
    [#sorted] => 1
            [#validated] => 1
        )

    [#after_build_done] => 1
)
Array
(
    [#default_value] => Array
        (
            [day] => 31
            [month] => 8
            [year] => 2017
        )

    [#after_build] => Array
        (
            [0] => __set_year_range
        )

    [#element_validate] => Array
        (
            [0] => age_validate
        )

    [#tree] => 
    [#parents] => Array
        (
            [0] => profile_date
        )

    [#array_parents] => Array
        (
            [0] => Personal Information
            [1] => profile_date
        )

    [#weight] => 0
    [#processed] => 
    [#required] => 
    [#attributes] => Array
        (
        )

    [#title_display] => before
    [#id] => edit-profile-date--2
    [#sorted] => 1

            [#validated] => 1
        )

    [#after_build_done] => 1
)    

I CANNOT see the profile_date in $element but can see it in $form_state, and the $element data is duplicated, what is the possible reason that can cause the missing of data?

1

1 Answers

0
votes

I don't know if I have understood your problem. However I think there is a misunderstanding.

In the form api reference about the #element_validate property is written is written:

A validation function for an element takes $element, $form_state, and $form as parameters:
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#element_validate

So the first argument of age_validate is the profile_date itself. From your output you can see that $element['#default_value'] and $form_state['value']['profile_date'] are equals, so you have submitted the form without changing the value of the profile_date input.

Notice: if you want the entire form inside the age_validate you can add a third argument:

function age_validate($element, &$form_state, $form) {...}

inside $form you can see the profile_date element