By saysilence on
I need to pass an additional var into my custom validation function, I came up with something like this:
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => $node->title,
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#element_validate' => array('my_validation_function'),
'#my_var' => 'var_i_want_passed',
);
It works but it feels DIRTY...
Is this safe or is there a better way?
(I'd like to avoid passing vars through $form_state['my_arrrrgh_and_whatnot'].)
Comments
Passing a variable through
Passing a variable through with the $form like that is fine.
Contact me to contract me for D7 -> D10/11 migrations.
Although that would work, I'd
Although that would work, I agree that's a dirty way of doing it.
I believe it's the Drupal way to pass variables through $form_state. All elements of $form_state are persisted, and people usually like to use $form_state['storage']. So in your case it would be:
The docs say:
Can I ask why you want to avoid this way of doing it?
Thank you all for your
Thank you all for your answers.
as why i would want to avoid using $form_state is simplicty. I can define my vars in the same place where i define the function (no scroling back and forth needed between the form element and $form_state['def..'] )
plus in the validation function i can quickly access my var by doing $element['#my_var'] without the need of going through $form_state['something_something']['nested']['nested']['nested'][...]
call it the rule of lazyness.