Hi,

I am trying to integrate my module with views. Currently I am trying to make a custom filter for my module. This filter consists of an autocomplete textfield. But in the case that the user just enters in some text and submits the form without waiting for the autocomplete, I want to be able to run some validation on it and then once it is validated, input the correct value back into the textfield.

So i figured that default values are based on $form_state['input'][$identifier]. However my validation is done on exposed_validation($form, $form_state). My validation function is also responsible for cleaning up the input and outputting the best correct input. Is there anyway to modify the default values for the value_form / exposed_form elements from either the validation or submit functions?

Thank you in advance

Comments

merlinofchaos’s picture

Status: Active » Fixed

In your validation function you can use form_set_value() to set the cleaned up value. See the API documentation for this on http://api.drupal.org.

n4964918’s picture

awesome thanks merlin :-D

n4964918’s picture

Status: Fixed » Active

hi again,

sorry mate, but i tried that function which it works and updates my form_state['values']. However this still does not change the form_state['input'][$identifier]. Maybe this variable is based on the $_GET value?

Thanks for your help again.

merlinofchaos’s picture

You may need to set both to be safe.

n4964918’s picture

Hi merlin,

I am able to change the initial value by changing my form_state['input'][$identifier] in my value_form function. However, I only know what value to change it to from my exposed_validate function as this is where I run most of my logic. If I change my form_state['input'][$identifier] in my validation function, it seems to be too late to alter the form element value.

One option for me is to run the validation in the value_form function, however I would ideally like to run it in the validation function, with which alters the initial value of the exposed form.

Not sure if this is possible.

Thanks

rschwab’s picture

Title: Changing default value of value form » Changing form value with autocomplete after submission

Any progress on this?

"If I change my form_state['input'][$identifier] in my validation function, it seems to be too late to alter the form element value."

Can you move the logic to find your needed value to a separate function called from your form function before validation?

iamjon’s picture

Status: Active » Closed (works as designed)

I'm closing this issue from lack of activity.
please feel free to reopen with more information.

joetsuihk’s picture

Status: Closed (works as designed) » Active

sorry to reopen, I have a similar situation.

User case: adding a currency dropdown via form_alter to before a textfield, use form_alter to add a new validation call to calculate user selected currency into USD, and pass to a existing exposed form

<?php
//re #6, go through value change before it goes through filtering
function hook_form_alter(&$form, $form_state){
  //dropdown alter and other alter above..
  $form['#validate'] = array('exposed_field_currency_validate','views_exposed_form_validate');
}

function exposed_field_currency_validate(&$form, &$form_state) {
  //re #4, #1, set both values to play safe
  form_set_value($form['field_min_usd_rate_value'], $converted, &$form_state);
  $form_state['input']['field_min_usd_rate_value'] = $converted;
}
?>

But the above code still cannot change the submitted value in exposed form

merlinofchaos’s picture

What about setting $form_state['values']...?

joetsuihk’s picture

re #9 no
I discovered that in #validate, you can only change $form_state values (it is by reference), but not $form
and you can change values in $form in #submit instead.

I even tried to override every single instance of that value, but still no luck, below the ugly code:

<?php

function hook_form_alter() {
  //sth above
  //reorder to make sure my custom alter processed before view's processing
  $form['#validate'] = array('exposed_field_currency_validate','views_exposed_form_validate');
  $form['#submit'] = array('exposed_field_currency_submit','views_exposed_form_submit');
}

function exposed_field_currency_validate(&$form, &$form_state) {
  
  if($form_state['values']['field_currency'] != 'usd') {
    $rate = variable_get($form_state['values']['field_currency'].'_usd', 1);
    $converted = $form_state['values']['field_min_usd_rate_value']/$rate;
  
    $form_state['values']['field_min_usd_rate_value'] = $converted;
    $form_state['view']->exposed_input['field_min_usd_rate_value'] = $converted;
  
    form_set_value($form['field_min_usd_rate_value'], $converted, &$form_state);

    $_GET['field_min_usd_rate_value'] = $converted;
    $form_state['input']['field_min_usd_rate_value'] = $converted;
  }
}

function exposed_field_currency_submit(&$form, &$form_state) {
  //$form_state had been changed in hok_validate()
  $converted = $form_state['values']['field_min_usd_rate_value'];
  $form['field_min_usd_rate_value']['#value'] = $converted;
  $form['field_min_usd_rate_value']['#post']['field_min_usd_rate_value'] = $converted;
  $form['submit']['#post']['field_min_usd_rate_value'] = $converted;
  $form['#post']['field_min_usd_rate_value'] = $converted;
}
?>

i dig deep to the field handler part, but that confuse me, no idea where to go next.

joetsuihk’s picture

Sorry guys, I just make a simple test case that, confirmed:

<?php
function hook_form_alter() {
  $form['#validate'][] = 'exposed_field_currency_validate';
}

function exposed_field_currency_validate(&$form, &$form_state) {
  $converted = 'wo0o';
  
  $form_state['values']['title'] = $converted;
}
?>

will be enough to change the exposed submit value if the field is NOT a cck field.

But my case, which still not resolved, because the field_min_usd_rate_value is a computed field. Similarly, a simple textfield from cck fails too. Does it mean I need to move this to CCK? where should I explore now?

merlinofchaos’s picture

Not sure what to say. With numeric values, check to see if there are multiple values; if the operator is exposed I think it has possibilities for a max/min which means there is a $form_state['values'][$identifier]['value'] or something like that as well?

Otherwise...maybe it's CCK but I suspect when it comes to exposed filters they're not going to know much there. :/

joetsuihk’s picture

Thanks for the input. No, the field operator is not exposed.

Can you guide me where exactly the comparsion take place? I will test more on it next week.

merlinofchaos’s picture

It's pretty tough to guide you; the exposed stuff in Views is pretty spread out across the filter handlers. I guess what you should do is dig into the CCK filter handler and follow the 'value' that it gets.

R.Hendel’s picture

Status: Active » Fixed
Issue tags: +dvcs11

Hallo,
is your request still open?

It is for quite a long time undiscussed.So I will set it on fixed to clean the queue.
Please feel free to reopen it, if you have any further questions. Otherwise it would be closed automatically after two weeks.

joetsuihk’s picture

re #15
it is not fixed in my case. I used js work around that only work specific to my case. And I have provided all the info/test cases I have, so I agree just mark as fixed and leave this to the greater web.

Status: Fixed » Closed (fixed)
Issue tags: -dvcs11

Automatically closed -- issue fixed for 2 weeks with no activity.