Re: Changing checkboxes options during hook_node_validate

Jeff Greenberg <[email protected]> Wed, 11 Feb 2015 13:23:19 -0500
Newsgroups gmane.comp.php.drupal.support
Message-ID <CAPpCgRb8yLiWF5ku8OHkR-f0yNCZLcM+J1N+9ZYxxVAHfu1B=w@mail.gmail.com>
--===============9078961609151276396==
Content-Type: multipart/alternative; boundary=001a113e74e40fd792050ed4193e

--001a113e74e40fd792050ed4193e
Content-Type: text/plain; charset=UTF-8

Ok, did that , and the #validate function is firing. I might get this
wrong, because my confusion level is high, but here is what I think I
observe in xdebug.

When I was setting a form error during validation, when it eventual made
its way to drupal_process_form it wasn't doing a rebuild because of that.
So I tried not setting a form error, since I'm setting
$form_state[rebuild]=true anyway. Now drupal_process_form does a rebuild,
but what happens is that just prior to the rebuild, the #options are what I
just set them to with
   $form[my_field][LANGUAGE_NONE][0][#options] = my_options_array;
in validation, but right after the rebuild, the original value is back. Do
I need to set #options in more than one section of $form or somewhere in
$form_state?

On Wed, Feb 11, 2015 at 12:01 PM, Jamie Holly <[email protected]>
wrote:

>  There could be something else overriding that in validation. Instead of
> using the default hook_node_validate, trying adding $form['#validate'][] =
> 'my_custom_validation_function' in hook_form or hook_form_alter. That way
> it's the last validation hook called.
>
> Jamie Hollyhttp://hollyit.net
>
> On 2/11/2015 11:45 AM, Jeff Greenberg wrote:
>
> Ok, I've tried that. The last thing I do in hook_node_validate is
> $form_state['rebuild'] = true; What I notice is that
> hook_form_node_form_alter does not fire between then and the form being
> redisplayed, so my pulling the options from $form_state['storage'] never
> happens.
>
> On Wed, Feb 11, 2015 at 10:57 AM, Jamie Holly <[email protected]>
> wrote:
>
>>  The checkboxes have to be added in the form creation stage, so in
>> either hook_form or hook_form_alter. Then in the hook_validate, set a
>> variable on $form_state['storage'] and set $form_state['rebuild'] = TRUE;
>> and check in your hook_form or hook_form_alter for your
>> $form_state['storage'] setting, and if set then add your checkboxes.
>>
>> I suggest looking at the form_example_wizard in the examples module, as
>> that's basically what you are after, even though it's not a typical
>> multi-step wizard you are after, the logic is still the same.
>>
>> Jamie Hollyhttp://hollyit.net
>>
>> On 2/11/2015 10:43 AM, Jeff Greenberg wrote:
>>
>> One thing that is either part of the issue, or a separate issue that
>> impacts this, is that the checkboxes field is added via hook_form_alter. I
>> notice that although the field appears in $form_state[values] it does not
>> appear in $form_state[field], so I'm wondering where $form_state gets the
>> information for the field when rebuilding the form.
>>
>> On Wed, Feb 11, 2015 at 9:31 AM, Jeff Greenberg <
>> [email protected]> wrote:
>>
>>>  I have a node form that contains a checkboxes field with options NULL
>>> at the time the form is displayed.
>>>
>>> During node_validate, if certain criteria is met, an array of options
>>> (checkboxes) is created, and the form needs to be redisplayed with the
>>> checkboxes as part of it.
>>>
>>> I've tried setting #options = the new array in the $form, in
>>> $form_state[original_form], etc all to no avail. The field doesn't change.
>>>
>>> I also set one checkbox in the field at the start, so that I could find
>>> it in the $form and $form_state structures, changed where I found it to the
>>> new options array, and again, no change. I am setting $form_state[rebuild]
>>> to true, and even tried explicitly calling for the form to be rebuilt.
>>>
>>>
>>>  --
>>> ---
>>> drupal.org/user/367108
>>> linkedin.com/in/jeffrgreenberg
>>> accidentalcoder.com / ayendesigns.com
>>> @accidentalcoder
>>>
>>
>>
>>
>>  --
>> ---
>> drupal.org/user/367108
>> linkedin.com/in/jeffrgreenberg
>> accidentalcoder.com / ayendesigns.com
>> @accidentalcoder
>>
>>
>>
>>
>> --
>> [ Drupal support list | http://lists.drupal.org/ ]
>>
>
>
>
>  --
> ---
> drupal.org/user/367108
> linkedin.com/in/jeffrgreenberg
> accidentalcoder.com / ayendesigns.com
> @accidentalcoder
>
>
>
>
> --
> [ Drupal support list | http://lists.drupal.org/ ]
>



-- 
---
drupal.org/user/367108
linkedin.com/in/jeffrgreenberg
accidentalcoder.com / ayendesigns.com
@accidentalcoder

--001a113e74e40fd792050ed4193e
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Ok, did that , and the #validate function is firing. I mig=
ht get this wrong, because my confusion level is high, but here is what I t=
hink I observe in xdebug.<div><br></div><div>When I was setting a form erro=
r during validation, when it eventual made its way to drupal_process_form i=
t wasn&#39;t doing a rebuild because of that. So I tried not setting a form=
 error, since I&#39;m setting $form_state[rebuild]=3Dtrue anyway. Now drupa=
l_process_form does a rebuild, but what happens is that just prior to the r=
ebuild, the #options are what I just set them to with</div><div>=C2=A0 =C2=
=A0$form[my_field][LANGUAGE_NONE][0][#options] =3D my_options_array;</div><=
div>in validation, but right after the rebuild, the original value is back.=
 Do I need to set #options in more than one section of $form or somewhere i=
n $form_state?</div></div><div class=3D"gmail_extra"><br><div class=3D"gmai=
l_quote">On Wed, Feb 11, 2015 at 12:01 PM, Jamie Holly <span dir=3D"ltr">&l=
t;<a href=3D"mailto:[email protected]" target=3D"_blank">hovercraf=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
 =20
   =20
 =20
  <div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div>There could be something else
      overriding that in validation. Instead of using the default
      hook_node_validate, trying adding $form[&#39;#validate&#39;][] =3D
      &#39;my_custom_validation_function&#39; in hook_form or hook_form_alt=
er.
      That way it&#39;s the last validation hook called. <br>
      <pre cols=3D"72">Jamie Holly
<a href=3D"http://hollyit.net" target=3D"_blank">http://hollyit.net</a></pr=
e>
      On 2/11/2015 11:45 AM, Jeff Greenberg wrote:<br>
    </div>
    <blockquote type=3D"cite">
      <div dir=3D"ltr">Ok, I&#39;ve tried that. The last thing I do in
        hook_node_validate is $form_state[&#39;rebuild&#39;] =3D true; What=
 I
        notice is that hook_form_node_form_alter does not fire between
        then and the form being redisplayed, so my pulling the options
        from $form_state[&#39;storage&#39;] never happens.</div>
      <div class=3D"gmail_extra"><br>
        <div class=3D"gmail_quote">On Wed, Feb 11, 2015 at 10:57 AM, Jamie
          Holly <span dir=3D"ltr">&lt;<a href=3D"mailto:hovercrafter@earthl=
ink.net" target=3D"_blank">[email protected]</a>&gt;</span>
          wrote:<br>
          <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;bord=
er-left:1px #ccc solid;padding-left:1ex">
            <div bgcolor=3D"#FFFFFF" text=3D"#000000">
              <div>The checkboxes have to be added in the form creation
                stage, so in either hook_form or hook_form_alter. Then
                in the hook_validate, set a variable on
                $form_state[&#39;storage&#39;] and set $form_state[&#39;reb=
uild&#39;] =3D
                TRUE; and check in your hook_form or hook_form_alter for
                your $form_state[&#39;storage&#39;] setting, and if set the=
n add
                your checkboxes. <br>
                <br>
                I suggest looking at the form_example_wizard in the
                examples module, as that&#39;s basically what you are after=
,
                even though it&#39;s not a typical multi-step wizard you ar=
e
                after, the logic is still the same. <br>
                <pre cols=3D"72">Jamie Holly
<a href=3D"http://hollyit.net" target=3D"_blank">http://hollyit.net</a></pr=
e>
                On 2/11/2015 10:43 AM, Jeff Greenberg wrote:<br>
              </div>
              <blockquote type=3D"cite">
                <div dir=3D"ltr">One thing that is either part of the
                  issue, or a separate issue that impacts this, is that
                  the checkboxes field is added via hook_form_alter. I
                  notice that although the field appears in
                  $form_state[values] it does not appear in
                  $form_state[field], so I&#39;m wondering where $form_stat=
e
                  gets the information for the field when rebuilding the
                  form.</div>
                <div class=3D"gmail_extra"><br>
                  <div class=3D"gmail_quote">On Wed, Feb 11, 2015 at 9:31
                    AM, Jeff Greenberg <span dir=3D"ltr">&lt;<a href=3D"mai=
lto:[email protected]" target=3D"_blank">listmail.ayendesigns@=
gmail.com</a>&gt;</span>
                    wrote:<br>
                    <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0=
 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      <div dir=3D"ltr">
                        <div>
                          <p style=3D"margin:0px 0px 1em;padding:0px;border=
:0px;vertical-align:baseline;clear:both">I
                            have a node form that contains a checkboxes
                            field with options NULL at the time the form
                            is displayed.</p>
                          <p style=3D"margin:0px 0px 1em;padding:0px;border=
:0px;vertical-align:baseline;clear:both">During

                            node_validate, if certain criteria is met,
                            an array of options (checkboxes) is created,
                            and the form needs to be redisplayed with
                            the checkboxes as part of it.</p>
                          <p style=3D"margin:0px 0px 1em;padding:0px;border=
:0px;vertical-align:baseline;clear:both">I&#39;ve

                            tried setting #options =3D the new array in
                            the $form, in $form_state[original_form],
                            etc all to no avail. The field doesn&#39;t
                            change.</p>
                          <p style=3D"margin:0px 0px 1em;padding:0px;border=
:0px;vertical-align:baseline;clear:both">I
                            also set one checkbox in the field at the
                            start, so that I could find it in the $form
                            and $form_state structures, changed where I
                            found it to the new options array, and
                            again, no change. I am setting
                            $form_state[rebuild] to true, and even tried
                            explicitly calling for the form to be
                            rebuilt.</p>
                          <span><font color=3D"#888888">
                              <div><br>
                                <span><font color=3D"#888888"> </font></spa=
n></div>
                              <span><font color=3D"#888888">
                                </font></span></font></span></div>
                        <span><font color=3D"#888888"> <span><font color=3D=
"#888888">
                                <div><br>
                                </div>
                                -- <br>
                                <div>---
                                  <div><a href=3D"http://drupal.org/user/36=
7108" target=3D"_blank">drupal.org/user/367108</a></div>
                                  <div><a href=3D"http://linkedin.com/in/je=
ffrgreenberg" target=3D"_blank">linkedin.com/in/jeffrgreenberg</a></div>
                                  <div><a href=3D"http://accidentalcoder.co=
m" target=3D"_blank">accidentalcoder.com</a>
                                    / <a href=3D"http://ayendesigns.com" ta=
rget=3D"_blank">ayendesigns.com</a></div>
                                  <div>@accidentalcoder</div>
                                </div>
                              </font></span></font></span></div>
                      <span><font color=3D"#888888"> </font></span></blockq=
uote>
                    <span><font color=3D"#888888"> </font></span></div>
                  <span><font color=3D"#888888"> <br>
                      <br clear=3D"all">
                      <div><br>
                      </div>
                      -- <br>
                      <div>---
                        <div><a href=3D"http://drupal.org/user/367108" targ=
et=3D"_blank">drupal.org/user/367108</a></div>
                        <div><a href=3D"http://linkedin.com/in/jeffrgreenbe=
rg" target=3D"_blank">linkedin.com/in/jeffrgreenberg</a></div>
                        <div><a href=3D"http://accidentalcoder.com" target=
=3D"_blank">accidentalcoder.com</a> / <a href=3D"http://ayendesigns.com" ta=
rget=3D"_blank">ayendesigns.com</a></div>
                        <div>@accidentalcoder</div>
                      </div>
                    </font></span></div>
                <span><font color=3D"#888888"> <br>
                    <fieldset></fieldset>
                    <br>
                  </font></span></blockquote>
              <br><span class=3D"HOEnZb"><font color=3D"#888888">
            </font></span></div><span class=3D"HOEnZb"><font color=3D"#8888=
88">
            <br>
            --<br>
            [ Drupal support list | <a href=3D"http://lists.drupal.org/" ta=
rget=3D"_blank">http://lists.drupal.org/</a>
            ]<br>
          </font></span></blockquote><span class=3D"HOEnZb"><font color=3D"=
#888888">
        </font></span></div><span class=3D"HOEnZb"><font color=3D"#888888">
        <br>
        <br clear=3D"all">
        <div><br>
        </div>
        -- <br>
        <div>---
          <div><a href=3D"http://drupal.org/user/367108" target=3D"_blank">=
drupal.org/user/367108</a></div>
          <div><a href=3D"http://linkedin.com/in/jeffrgreenberg" target=3D"=
_blank">linkedin.com/in/jeffrgreenberg</a></div>
          <div><a href=3D"http://accidentalcoder.com" target=3D"_blank">acc=
identalcoder.com</a>
            / <a href=3D"http://ayendesigns.com" target=3D"_blank">ayendesi=
gns.com</a></div>
          <div>@accidentalcoder</div>
        </div>
      </font></span></div><span class=3D"HOEnZb"><font color=3D"#888888">
      <br>
      <fieldset></fieldset>
      <br>
    </font></span></blockquote>
    <br>
  </div>

<br>--<br>
[ Drupal support list | <a href=3D"http://lists.drupal.org/" target=3D"_bla=
nk">http://lists.drupal.org/</a> ]<br></blockquote></div><br><br clear=3D"a=
ll"><div><br></div>-- <br><div class=3D"gmail_signature">---<div><a href=3D=
"http://drupal.org/user/367108" target=3D"_blank">drupal.org/user/367108</a=
></div><div><a href=3D"http://linkedin.com/in/jeffrgreenberg" target=3D"_bl=
ank">linkedin.com/in/jeffrgreenberg</a></div><div><a href=3D"http://acciden=
talcoder.com" target=3D"_blank">accidentalcoder.com</a> / <a href=3D"http:/=
/ayendesigns.com" target=3D"_blank">ayendesigns.com</a></div><div>@accident=
alcoder</div></div>
</div>

--001a113e74e40fd792050ed4193e--

--===============9078961609151276396==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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