0

I have the form which is created through Webform module and now I need to add one more text field but its value has to be unique and can be only the value from some predefined list of allowed values.

It's not the problem that value of the field be unique (I can enable it in text field settings) but I don't have idea how to limit the value of the field on one from predefined list of values.

Those strings are voucher codes that are sent to the clients by email, also there are a 1500 different codes.

If the user enters the code that was already used or enters the code that doesn't exist in the list he should get the notification that the code is not valid right away (Ajax) or on the form submit.

Can anyone can help me with this or direct me? I'll appreciate any suggestion...

  • any reason you do not want to add validtion handler where you can check the submit value is one from the predefined list and then throwing the validation error with a message? – arpitr Feb 12 '15 at 20:44
  • Thanks @arpitr, I never does something like this before. I'll try for sure. Can you please give me some directions, how and where to start, or some links where I can find out more about this? – mixerowsky Feb 12 '15 at 21:30
0

In your custom module implement hook_form_alter, this will let you add your custom validation handler to the form, something like

  my_module_name_form_alter(&$form, &$form_state, $form_id) {
    if($form_id == 'your_form_id') {
      // Add one more validation.
      $form['#validate'][] = '_my_module_name_custom_form_valition';
    }
  }

  function _my_module_name_custom_form_valition($form, $form_state) {
   // $form_state holds value for submitted fields.
   if(!in_array($allowed_values,$form_state['values']['your_form_field']) {
      form_set_error('your_form_field',t('Entered value is not allowed.'));
   }
  }
  • Thank you very much. Can you just tell me, is it possible that I have performance issues on form submit because there is 1500 codes? – mixerowsky Feb 13 '15 at 12:07

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.