[support] More D7 form woes

Metzler, David metzlerd at evergreen.edu
Wed Feb 18 16:13:28 UTC 2015


Sounds like you got it, but it looks like you were struggling with changing values on validation, which I’ve struggled with before so I thought I’d let you know the secret sauce.  It’s the $form_state[‘input’] that people usually miss.

When changing the values in a validation handler or form rebuilding function do the following:

1.)    Change the #default value in the form:

2.)    Change the corresponding $form_state[‘values’] element

3.)    Change the $form_state[‘input’] element for the same value.

I usually do this in one statement like:

$form[‘myfield’][‘#default_value’] = $form_state[‘values’][‘myfield’] = $form_state[‘input’][‘my_field’] = $foo;

Dave

From: support-bounces at drupal.org [mailto:support-bounces at drupal.org] On Behalf Of Jeff Greenberg
Sent: Tuesday, February 17, 2015 4:08 PM
To: support at drupal.org
Subject: Re: [support] More D7 form woes

Got it. I cheated in the most horrendous way, though. Doing the change of the body in hook_node_presave works. Of course, that hook has no view into the form structure, just the node, and getting the value into the node has been the issue. So, when the new body value is assembled, I store it in a session variable and retrieve it during presave.

On Tue, Feb 17, 2015 at 5:04 PM, Jeff Greenberg <listmail.ayendesigns at gmail.com<mailto:listmail.ayendesigns at gmail.com>> wrote:
I don't think he filter will do it. I'm thinking my description of how I'm changing the body was too generic. The body is fed through some logic that selects strings from it that are likely tag values for the given category, and then those become links within the body, as well as the checkboxes from the previous issue.

Seems that requesting rebuild at the end of validation causes the usual way of changing form values during validation to fail.


On Feb 17, 2015, at 16:31, Jamie Holly <hovercrafter at earthlink.net<mailto:hovercrafter at earthlink.net>> wrote:
Instead of doing that, why not just create a custom format filter and do the changes then? That also gives the benefit of being resuable on any other place you might need it in the future, like blocks, plus can easily be modified, undone, whatever by changing the code, since the final markup is cached and the original kept in tact.


Jamie Holly

http://hollyit.net
On 2/17/2015 4:06 PM, Jeff Greenberg wrote:
So, my last messages were about needing to create and populate checkboxes based on values determined during the form validation process. This ended up working by:
1. Creating the fields in hook_form_alter
2. Storing the option values as an array during validation in $form_state['storage']
3. Checking in hook_form_alter for $form_state[storage], and if present, setting the field's options to the stored array

I also need to massage the body value during validation, basically to convert some strings to links. I tried the same method as above, which didn't work, with the problem being that  $form[body][LANGUAGE_NONE][0] contains a large meta array, so I wasn't sure what to do with the value.

I tried setting the value in form_state[values] during validation, but at the end of validation at this point in the flow I set rebuild=true, which ends up using the values that were present when the form was submitted (thus overwriting my change) ... which is why I ended up doing the other fields in hook_form_alter instead.

I then tried using form_set_value in validate. Two issues there. One is that passing $form[body] as the element doesn't work, I think, because I end up with $form_state[values][body] having a value instead of it being in form_state[values][body][LANGUAGE_NONE][0], and, I think, because of rebuild=true being set, which causes the change by form_set_value to be overwritten with the value that was there when submit was clicked.

--
---
drupal.org/user/367108<http://drupal.org/user/367108>
linkedin.com/in/jeffrgreenberg<http://linkedin.com/in/jeffrgreenberg>
accidentalcoder.com<http://accidentalcoder.com> / ayendesigns.com<http://ayendesigns.com>
@accidentalcoder


--
[ Drupal support list | http://lists.drupal.org/ ]



--
---
drupal.org/user/367108<http://drupal.org/user/367108>
linkedin.com/in/jeffrgreenberg<http://linkedin.com/in/jeffrgreenberg>
accidentalcoder.com<http://accidentalcoder.com> / ayendesigns.com<http://ayendesigns.com>
@accidentalcoder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/support/attachments/20150218/b980c572/attachment-0001.html 


More information about the support mailing list