0

I have a form to get some user input in Drupal. I want to add some prefix to the user input.

For example, the user enters "Laptop"; I want to save it in the database as resource_Laptop. I want to add the prefix before the validation is done, to avoid it fails.

How do I do this? Will hook_form_alter() help me here?

I have used this method, but not satisfied with this hack.

function mymodule_form_alter(){
  drupal_add_js("
    (function ($) {
      $(document).ready(function() {                    
        $('.field-suffix').hide();  
        $('#edit-submit').click(function(){
        var s = $('#edit-machine-name').val();

        if (s.indexOf('_resources') == −1) {
          $('#edit-machine-name').val($('#edit-machine-name').val() + '_resources');
        }
      });   
    })(jQuery);", 'inline');
}
7
  • Since your db table already has a column for resources, why do you need to prepend 'resource_' to the content?
    – paul-m
    Commented Mar 28, 2012 at 6:31
  • 2
    The validate handlers are the first to be called by FAPI after a submit. So it will be difficult to have the prefix added beforhand. May be you want explain why you want to validate something you are adding as a post process and is not entered by the user anyway. If you want to make sure your prefixing code works corrent it would be better to think about writing a simple test.
    – BetaRide
    Commented Mar 28, 2012 at 6:37
  • @paul-m... actually I am adding some data in taxonomy table and want to make my data unique by adding resources_ prefix to taxonomy vocabulary. It gives me error for machine-name already exist and which is actually added vocabulary by taxonomy so in my module as I am using the same taxonomy tables to save my data.
    – Umar
    Commented Mar 28, 2012 at 7:31
  • What form are you altering? Why are you altering an existing form, rather than using your own form?
    – avpaderno
    Commented Jun 30, 2012 at 7:44
  • This whole thing seems like a really bad idea and bad way of implementing. If your only worry is passing validation, then why does it matter whether a string has resource_ prepended or not? It should still be possible to write a validation for that. Best place to add this, and alter input is in a submit handler. Also don't use #edit-machine-name as this can become #edit-machine-name--2 or #edit-machine-name--3. Use a class selector, or add a class through that hook_form_alter
    – Beebee
    Commented Nov 28, 2014 at 14:13

2 Answers 2

0

Use the hook_form_FORM_ID_alter and add a custom submit handler.

function example_form_form_id_alter(&$form, &$form_state,$form_id){
  $form['#submit'][] = 'example_form_id_submit',
}

function example_form_id_submit(&$form,&$form_state){
  $form_state['values']['resource_name'] = 'resource_' . $form_state['values']['resource_name'];
}
5
  • 1
    This will come to late, since op want's to have the prefix added before validation. Validation handlers are called prior to the submit handlers.
    – BetaRide
    Commented Mar 28, 2012 at 6:34
  • @karthik... this will still fail my validation. I want to add prefix before validation call. does form_set_value help me here?
    – Umar
    Commented Mar 28, 2012 at 7:34
  • @Umar: how about doing the above in the validation instead of during submit, i.e. $form['#validate'][] = 'example_form_id_validate', you just have to make sure this validation runs before the rest ..
    – user2948
    Commented Mar 28, 2012 at 8:30
  • @Karthik... Nikhil suggested me same and I tried check my response on his comment.
    – Umar
    Commented Mar 28, 2012 at 8:37
  • can you confirm, the validate function does a call by reference and not by value? If you did a call by reference and doesn't work, you might have to stick to your js hack.
    – user2948
    Commented Mar 28, 2012 at 8:43
0

Add one more validation callback handler by giving it the priority(executed first) in $form['#validate'] and change the values in $form_state.

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.