2

I'm using form_alter to play with the node form for a specific content type. This content type has many different fields and I'm also using Field Collection module to organize all these fields & logic, if that makes any difference.

Apparently, there are some incompatibilities between the Field Collection & Field Dependencies modules so I thought I might be able to build a similar field dependency functionality with my own module.

I've created a validation function for the form. My problem is that I'm trying to access $form_state values that users insert in the form in order to validate these values but I think I'm doing something totally wrong here:

function _mymodule_my_validation_function($form, &$form_state){
    $short_field_name = $form_state['values']['long_field_name'];
    if (empty($short_field_name) || $$short_field_name == '') {
        form_set_error('error','That field cannot be empty!');
    }
}

Unfortunately this validation script doesn't work. To be more exact, I

dpm($form_state);

in my form_alter and $form_state doesn't even contain 'values' (shouldn't it be present but empty?).

Is there a different best practice for validation node form fields instead of dealing with the $form_state['values'] array?

Also, I need to be able to see the values submitted by users each time the node form is submitted. Currently, I dpm($form) in my form_alter function and everything is returned on the node/%/edit page as expected. But how can I catch submitted values on the node/% page after submitting the node form??

Update: When I call dpm($form_state) within my validation function I get what I need if the form doesn't validate (as we still are on the same node edit page). But how can I receive the (new/changed) values that users entered in the form fields in case the form is submitted successfully and user is directed to content list page?

1
  • It depends where you've put dpm. If you've put it in the main form it will call on page load and be empty. You have put dpm() in the validate or submit function to see the data you've entered. Oct 31, 2013 at 16:05

1 Answer 1

0

For custom forms
The preferred way is to call hook_form_validate for hook_form but you can use $form['#validate'][] = 'custom_validate_function' to call validate function

function hook_form(&$form,&$form_state,$form_id) {

$form = array();

// write your custom fields here..........
return $form;

}

function hook_form_validate($form, &$form_state) {
 $short_field_name = $form_state['values']['long_field_name']['und'][0]['value'];
    if (empty($short_field_name) || $short_field_name == '') {
        form_set_error('long_field_name','That field cannot be empty!');
    }

}

For built in forms by creating content type

function hook_form_alter($form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    $form_state['#validate'][] = 'form_1_validate';
    $form_state['#submit'][] = 'form_1_submit';
  }
}

function form_1_validate($form, &$form_state) {

$short_field_name = $form_state['values']['long_field_name']['und'][0]['value'];
    if (empty($short_field_name) || $short_field_name == '') {
        form_set_error('long_field_name','That field cannot be empty!');

}
}

function form_1_submit($form, &$form_state) {

//To get the user selected or user submitted values you should look into code below
$field_name_values = $form_state['values']['field_name'];



}
3
  • The omitted "['long_field_name']['und'][0]['value']" was causing the problem, thank you. When I call dpm($form_state) within my validation function I get what I need if the form doesn't validate (as we still are on the same node edit page). But how can I receive the (new/changed) values that users entered in the form fields in case the form is submitted successfully and user is directed to content list page?
    – webmaniac
    Oct 31, 2013 at 16:23
  • check my updated answer for that and please add this comment to your question.
    – harshal
    Oct 31, 2013 at 18:53
  • 3
    $form_state['#validate'][] should be: $form['#validate'][] Apr 12, 2016 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.