0

I am using the adress field module and like to make only the postal code field required. The rest of the fields i like to hide.

The hiding of the fields are no problem however i am having problems with making only the postalcode field required. I can hide the whole field with:

<?php
 function my_form_user_profile_form_alter(&$form, $form_state,  $form_id) {                

  if ($form_id == 'user_profile_form') {

  $form['field_adress']['und'][0]['#required'] = TRUE;
      }
    }
      ?>

but then this is not working

$form['field_adress']['und'][0]['locality_block']['postal_code']['#required'] = TRUE;

What am i missing? Without succes i have tried solutions as: In here and here

2
  • Did you spell "address" in field_adress incorrectly intentionally? Commented May 16, 2014 at 1:32
  • Yes sorry this might be confusing but i double checked for type errors and i couldn't find any. thanks for asking
    – Byron
    Commented May 21, 2014 at 8:17

1 Answer 1

1

You can make the address field not-required and write a custom validate function to check for the postal code. Like this:

<?php
function my_form_user_profile_form_alter(&$form, $form_state,  $form_id) {                
  if ($form_id == 'user_profile_form') {
      $form['field_adress']['und'][0]['#required'] = FALSE;
      array_unshift($form['#validate'], '_my_postal_validate');
  } 
}

function _my_postal_validate(&$form, &$form_state) {
    if (strlen($form_state['values']['field_adress']['und'][0]['postal_code']) < 2) {
        form_set_error('field_address', t('You must enter a postal code'));
    }
}
?>

See form_set_error to set better form errors.

If you support non US addresses (without a postal code), make sure the postal_code is available or you'll get warnings.

13
  • Thanks for your reply. I have tried your code however my netbeans and drupal seem to stumble over print_r. Drupal 7 returns a parse error in this line....
    – Byron
    Commented May 15, 2014 at 19:24
  • print_r is a php function, it should not give any errors. I'll update my answer with the entire validate function, that should work for you
    – Jesse
    Commented May 15, 2014 at 19:44
  • You're right i did not place you're first code right. Thanks for you're update. However with the first and second version i can still leave my fields empty. It does not give the error...I have looked in previous examples on d.org to make only postal code field required. In there the field was specified as ($form['field_adress']['und'][0]['locality_block']['postal_code']); Hiding a field for example city is not a problem using this, making one field required until now is...
    – Byron
    Commented May 15, 2014 at 20:04
  • The idea is to make the address field NOT required and do a custom validation for the postal code. The code above works in a clean drupal 7.28 with addressfield 7.x-1.0-beta5. What does not work for you?
    – Jesse
    Commented May 15, 2014 at 20:19
  • I understand. I have tried your code with my adress_field on 'required' and 'not required'. If my field is required the code, for some reason doesn't become 'not required'. If let my adress_field unrequired i can leave all fields empty and still save my user profile. So now i have to find out why youre code is not working....I'll be posting.
    – Byron
    Commented May 15, 2014 at 21:39

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.