开发者

Drupal CCK field does not validate using hook_nodeapi

开发者 https://www.devze.com 2023-02-24 02:23 出处:网络
I am trying to do a custom validation over link field in Drupal CCk. I am currently using Link Contributed module.

I am trying to do a custom validation over link field in Drupal CCk. I am currently using Link Contributed module. So in my hook_nodeapi and case validate Notice that this value accept unlimited values this there is a foreach.

case 'validate':  
  if ($node->type == 'flow')  
  {  
      foreach ($node->field_post_twitter as $twitter_v)  
      {  
        $pattern = '/(http开发者_如何转开发|https|ftp):[\/]{2}twitter\.com/i';  
          if (preg_match_all($pattern, $twitter_v['url']))  
          {  
            form_set_error('not working');  
          }  
       }  
  }  
break; 

But its not validating, while if i put it in a normal php file it DOES work. Any ideas of what am i missing ?

Thanks


You should to use hook_form_alter to altering validate array of current form and add into this array custom validate function.

function mymodule_form_alter(&$form, &$form_state, $form_id){
  switch($node->type){
    case 'test_node_form':
      $form['#validate'][] = 'my_custom_validate';
      break;
  }
}

function my_custom_validate($form, &$form_state){
  $values = $form_state['values'];
  if(/* conditions */){
    $message = t('Oops!');
    form_set_error('', $message);
  }
}


Try setting the weight of your module from the system table in your database to the highest, so your hook is invoked last and all CCK fields -including (field_post_twitter) which is likely not yet populated in your case- are already populated.

Don't forget to clear your cache (drush cc) after all.

0

精彩评论

暂无评论...
验证码 换一张
取 消