0

I'm attempting to implement one of the answers to this question on validating whether there no double-byte characters present.

Here is what I've done so far in my custom module:

function CUSTOMMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'profile_gen_node_form') {
    $form['#validate'] = 'CUSTOMMODULE_location_doublebyte_validation_handler';
  }
}

And then:

function CUSTOMMODULE_location_doublebyte_validation_handler($form, $form_state) {

This is where I'm stuck. Addressfields include much more information than normal fields; they seem to be like a collection of fields. Here is a screenshot of the output of dpm($form):

locality_block output

I want to check strlen() and drupal_strlen() of the value of the "locality" piece of my addressfield but I can't figure out how to do that.

I tried something like this:

if(strlen("field_gen_loc_current['und']['0']['locality_block']['locality']") !== '0') {
    form_set_error('locality', t('No double-byte input.'));
}
}

But, while this doesn't produce an error, it also never causes the validation to fail.

1

I've just chucked a validation function into a dev site with an addressfield and you're right, it's a bit strange. It might be that the address field is marked as #tree => TRUE, and that's what's causing the long element key string.

Either way, it looks like you're checking the $form variable in your validate function, when the submitted data will actually be inside $form_state['values']. A couple of other minor alterations and you should be able to get it working:

function validation_function(&$form, &$form_state) {
  // Let's at least TRY to keep lines to 80 chars...
  $key = 'commerce_customer_profile|billing|commerce_customer_address|und|0';

  // Get the length of the 
  $length = strlen($form_state['values']['addressfield'][$key]['locality']);

  if ($length != 0) {
    form_set_error('addressfield', t('No double-byte input.'));
  }
}

That example is based on the Commerce add customer form, so you'll need to inspect the $form_state['values'] array in your own form to find out exactly what the $key variable should be but hopefully that'll give you somewhere to start.

Just a note on your use of !== above...be careful with that, it will force PHP to make sure the types of the variables are equal, as well as the value.

strlen() returns an integer, and '0' is a string, so strlen('string') !== '0' will always return TRUE, regardless of whether the values are the same.

6
  • I'm having trouble determining the value for $key. When I insert a $print_r($form_state['values']['field_gen_loc_current']), I get the following: Array ( [und] => Array ( [0] => Array ( [element_key] => node|profile_gen|field_gen_loc_current|und|0 [locality] => adsklfjaだからF [administrative_area] => HK [country] => JP ) ) ). When I tried using node|profile_gen|field_gen_loc_current|und|0 as the value for $key, the returned strlen was 0, but the actual value for locality is adsklfjaだからF, so something is wrong. Any ideas? Jun 8 '12 at 0:39
  • Might be a stupid question but did you change the ['addressfield'] part to the name of your field?
    – Clive
    Jun 8 '12 at 17:49
  • Yes. When I do $print_r($form_state['values']['field_gen_loc_current']), some information is output (as shown in the above comment). However, when I add the [$key]['locality'] part, I don't get anything. Jun 9 '12 at 1:57
  • I solved it by modifying the array like this: $length1 = strlen($form_state['values']['field_gen_loc_current']['und']['0']['locality']); Jun 10 '12 at 11:59
  • That actually would have made a lot more sense but it didn't work on the form I tried it on for some reason. Sorry if I led you down the wrong path
    – Clive
    Jun 10 '12 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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