i wrote a custom field and i want to do mini validation just in the custom field.

The only input data are the cellphone textfield. The other form fields all are used for validation.

However, i can't pass the value of the field.

Is there something wrong?

 function cellphonefield_field_widget_form(&$form,&$form_state,$field,$instance,$langcode,$items,$delta,$element)


             '#type' => 'textfield',
         '#title'=>t('Enter your Cell phone Number!'),
         '#description'=>t('System will send you a message '),
         '#default_value'=>isset($items[$delta]['cellphone'])?$items[$delta]['cellphone']:'test for this field',

             '#type' => 'submit',
             '#name' => 'send_sms',
             '#title' => t('validate your cellphone number'),
           '#value'=>t('Send Me Validate Code'),

             '#type' => 'textfield',
         '#name' => 'validate_code',
         '#size' => 20,
         '#title'=> t('Enter the validation code !'),
         '#description'=>t('Please check your cell phone sms message and then enter the code in the sms.'),


             '#type' => 'submit',
             '#name' => 'validate_button',
             '#title' => t('validate your cellphone number'),
         '#submit' => array('send_sms_function'),

        '#title'=>t('Cell Phone'),
        '#description'=>t('Your phone is been validated!'),

        return $element;

    function send_sms_function(&$form,&$form_state)


For validating the content of a field there is hook_field_validate(). As example of implementation of that hook in a module, you can look at taxonomy_field_validate(), or number_field_validate().

function number_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if ($item['value'] != '') {
      if (is_numeric($instance['settings']['min']) && $item['value'] < $instance['settings']['min']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'number_min', 
          'message' => t('%name: the value may be no less than %min.', array('%name' => $instance['label'], '%min' => $instance['settings']['min'])),
      if (is_numeric($instance['settings']['max']) && $item['value'] > $instance['settings']['max']) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'number_max', 
          'message' => t('%name: the value may be no greater than %max.', array('%name' => $instance['label'], '%max' => $instance['settings']['max'])),

As far as I can see, fields don't add submission buttons. That is not the purpose of fields, which is to add fields to enter data for an entity (e.g. nodes, users, taxonomy terms).

In your case, the workflow is similar to the following one:

  • The user enters a cell phone number
  • The module tells to the remote service to send a verification code to that cell phone number
  • The user enters the verification code
  • The module checks the verification code is the expected one

Such workflow can work if it is done in two steps:

  • When the user first enters the cell phone number, the validation hook can just send the cell phone number to the remote service. It cannot wait the user receives the verification code, and enters it.
  • The second time, the user edits the verification code, and the validation hook will check the verification code is the right one.

You have to see if checking the verification code can be done synchronously, or asynchronously.

| improve this answer | |
  • Thank you for answering my question. The workflow you provide can be done by using the ajax type of the form-api,right? Because, i don't have any experience of writing the ajax. Can you have some example or some ideas? Thank you! – cobenash Jul 29 '12 at 23:10

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.