0

it's not the first time that i get into trouble using $_post in a node form alter, this time i'd like some assistance.

basically, i need to pass some parameters to a form (and i should not pass the appended to the url, as query parameters). but $_POST values don't work as expected (they work until reaching validation phase).

So.. in a page i pass as hidden value

<form method="post" action="/node/add/">
    <input type="hidden" name="mydesideredParameter" value="itsValue">
</form>

In the form_alter function of this form, at the very beginning, i try to assign this value to $form_state (and it works, I watchdog it later and it's correct)

if (array_key_exists('mydesideredParameter', $_POST)) {
    $form_state['mydesiredConsequence'] = 'dance';
}

Later, inside form_alter, at the very end, i state:

if ($form_state['mydesiredConsequence'] === 'dance'){
    $form['#validate'][] = 'myDancingFunction';
};

No complains happen on form building, but when trying to submit the form, the validation funcion doesn't get called and php complains about index that doesn't exist no more

Notice: Undefined index mydesiredConsequence

Nothing similar happens when i use this step and the values are in the url(via drupal_query_parameters) , but trying to make this work in $_POST seems a bit awkward. (i obviusly can imagine that the second form-rebuilding ignores $_POST, as it was a former request; but that should be the intended purpose of assigning the value to $form_state).

0

You should use $form_state['storage']. This will cache your data and make it available throughout the forms lifecycle.

  • I'll try it, but i suppose the index ['storage'] has no special behaviours compared to $form_state['callMeAsYouLikeIt']; it's just a standard naming convention (at least in D7) – urka_mazurka Apr 6 '18 at 9:37
  • I believe you may be right in it just being a convention but there is not enough context to know what else could be going wrong here. – Dylan Fontaine Apr 6 '18 at 18:21

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.