0

I unsuccessfully tried https://www.drupal.org/project/rules_forms for forms validation so I am writing my own little validation module.

Basically, on node creation I want a validation against a custom user profile field (machine name: field_points). When the user enters on node creation more points then he has on his profile, an error message should appear.

Static validation against a number works just fine, but how can I access custom fields of the user object? Using devel, for some reason they don't show up in dpm($user).

dpm($user)

function custom2_form_contenttype_node_form_alter(&$form, &$form_state, &$form_id) {
drupal_set_message("Form ID is : " . $form_id);
dpm($form);
//$form['field_points']['und']['0']['value']['#default_value'] = 164;
$form['#validate'][] = 'custom2_form_contenttype_node_form_validate';
}

function custom2_form_contenttype_node_form_validate($form, $form_state) {
global $user;
dpm($user);
$profilePoints = $user->field_points;
$enteredPoints = $form_state['values']['field_points']['und']['0']['value'];
dpm($form['#validate']);
dpm($form_state);
if ($enteredPoints > $profilePoints) {
   form_set_error('field_points', '"You can only spend" .     $profilePoints . " //points."');
   }

}

EDIT: uploaded devel user screenshot

2 Answers 2

0

Try this in your validate function

global $user;
$user_fields = user_load($user->uid);

$profilePoints = $user_fields->field_points['und'][0]['value'];

I hope this will work

3
  • Thx for the suggestion. i get this message unfortunately: Notice: Undefined property: stdClass::$field_points in custom2_form_contenttype_node_form_validate() (line 25 of /Users/macmini/Sites/devdesktop/course/sites/all/modules/custom2/custom2.module). any idea why the custom fields of the user are not showing up? ive re-enabled devel, cleared cache...
    – user24957
    Nov 10, 2015 at 10:20
  • use dpm to properly index your value. I followed from your code. When you load your user object using user_load function all your fields should be available then.
    – Shabir A.
    Nov 10, 2015 at 10:24
  • thx Shabir! the $user_fields = user_load($user->uid) did the trick. after this when i dpm($user_fields) it shows up and the profile points are accessible with $profilePoints = $user_fields->field_points['und'][0]['value'];
    – user24957
    Nov 11, 2015 at 8:55
0

Try with this code it is helpful for you

<?php

function mymodule_form_alter(&$form, &$form_state, &$form_id) {
      if ($form_id == 'form_id') {
    $form['#validate'][] = 'my_validation';
                                      }
                                                                }


function my_validation($node, $form, $form_state){

    if (!isset($form_state['values']['field_text']['und'][0]['field_text']['und'][0]['value']))
    {
        form_set_error('Text', 'Select text'); 
        }

}
4
  • hey Adi! Thx for the suggestion. Static form validation works - but i can't validate against a user profile field. I've added a screenshot to illustrate better what I mean
    – user24957
    Nov 10, 2015 at 9:09
  • your field name??
    – Adi
    Nov 10, 2015 at 9:15
  • there is a custom field (machine name: field_points) on the user profile. it does not show up when using devels dpm($user). why?
    – user24957
    Nov 10, 2015 at 9:18
  • you use dsm against your from name or id then check it
    – Adi
    Nov 10, 2015 at 9:19

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.