I used profile2 module and create some profile types,in one of them I have a custom field require custom validation . I did use drupal validation with custom validation function . Lets suppose I have a field with name 'nationalcode' if it was form element just need this in my custom module hook_form_alter(&$form,&$form_state)

$form['field_nationalcode']['#validation'][] = 'my_custom_validation_function';

And determine my_custom_validation_function function with my custom validation

function my_custom_validation_function(&$form,&$form_state) {
     form_set_error('field_nationalcode','my custom message');
        // I am not sure mayebe this one
     form_set_error('nationalcode','my custom message');

But really what I should do in profile type cusotm validation?

I set #validation but How can i set form_set_error with profile type? .I means what i should use in first parameter in form_set_error ?

1 Answer 1


I found The good solution for validate nested form elements according to drupal 7 form_set_error api need to use

form_set_error('myporifletypename][myfieldname', 'my error message');   

As example

 form_set_error('user_info][field_nationalcode', 'Your National code not valid');

Your Answer

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

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