Re: Changing checkboxes options during hook_node_validate

Jeff Greenberg <[email protected]> Thu, 12 Feb 2015 10:18:34 -0500
Newsgroups gmane.comp.php.drupal.support
Message-ID <CAPpCgRZ9QNaVEXwAs1x8w9UX0iPeWa4z_bUO45T5HJoYOCa=wA@mail.gmail.com>
--===============2905058357156927762==
Content-Type: multipart/alternative; boundary=bcaec5196c2531d2f4050ee5a291

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

Got it to work after all using the method of storing the new options in
$form_state[storage] and then, if it isset during the subsequent hook,
force-feeding the field with the values from the form state.

Thanks very much Dave and Jamie!!

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

>  Actually you never really know what is going to happen in validation. So
> many modules can do funky things. A lot of times the multi-step type forms
> handle the rebuild through a submit callback.
>
> There's another option here as well, depending upon the criteria that has
> to be met, and that is using the #states system. You can see an example
> here:
>
>
> http://cgit.drupalcode.org/examples/tree/form_example/form_example_states.inc
>
> Jamie Hollyhttp://hollyit.net
>
> On 2/11/2015 11:58 AM, Metzler, David wrote:
>
>  Actually you should be able to alter the form in the validation function
> yes?  Just change the function signature to pass $form by reference and
> make the changes to the form that you need rather than going through the
> later hook?
>
>
> Dave
>
>
>
> *From:* [email protected] [mailto:[email protected]
> <[email protected]>] *On Behalf Of *Jeff Greenberg
> *Sent:* Wednesday, February 11, 2015 8:45 AM
> *To:* [email protected]
> *Subject:* Re: [support] Changing checkboxes options during
> hook_node_validate
>
>
>
> 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 Holly
>
> http://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

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

<div dir=3D"ltr">Got it to work after all using the method of storing the n=
ew options in $form_state[storage] and then, if it isset during the subsequ=
ent hook, force-feeding the field with the values from the form state.<div>=
<br></div><div>Thanks very much Dave and Jamie!!</div></div><div class=3D"g=
mail_extra"><br><div class=3D"gmail_quote">On Wed, Feb 11, 2015 at 2:25 PM,=
 Jamie Holly <span dir=3D"ltr">&lt;<a href=3D"mailto:hovercrafter@earthlink=
.net" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<br=
><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1=
px #ccc solid;padding-left:1ex">
 =20
   =20
 =20
  <div bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div>Actually you never really know what is
      going to happen in validation. So many modules can do funky
      things. A lot of times the multi-step type forms handle the
      rebuild through a submit callback. <br>
      <br>
      There&#39;s another option here as well, depending upon the criteria
      that has to be met, and that is using the #states system. You can
      see an example here:<br>
      <br>
<a href=3D"http://cgit.drupalcode.org/examples/tree/form_example/form_examp=
le_states.inc" target=3D"_blank">http://cgit.drupalcode.org/examples/tree/f=
orm_example/form_example_states.inc</a><br>
      <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:58 AM, Metzler, David wrote:<br>
    </div>
    <blockquote type=3D"cite">
     =20
     =20
     =20
      <div>
        <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:=
&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d">Actually
            you should be able to alter the form in the validation
            function yes?=C2=A0 Just change the function signature to pass
            $form by reference and make the changes to the form that you
            need rather than going through the later hook? =C2=A0=C2=A0<u><=
/u><u></u></span></p>
        <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:=
&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><br>
            Dave<u></u><u></u></span></p>
        <p class=3D"MsoNormal"><span style=3D"font-size:11.0pt;font-family:=
&quot;Calibri&quot;,&quot;sans-serif&quot;;color:#1f497d"><u></u>=C2=A0<u><=
/u></span></p>
        <p class=3D"MsoNormal"><b><span style=3D"font-size:10.0pt;font-fami=
ly:&quot;Tahoma&quot;,&quot;sans-serif&quot;">From:</span></b><span style=
=3D"font-size:10.0pt;font-family:&quot;Tahoma&quot;,&quot;sans-serif&quot;"=
>
            <a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a>
            [<a href=3D"mailto:[email protected]" target=3D"_blank=
">mailto:[email protected]</a>]
            <b>On Behalf Of </b>Jeff Greenberg<br>
            <b>Sent:</b> Wednesday, February 11, 2015 8:45 AM<br>
            <b>To:</b> <a href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a><br>
            <b>Subject:</b> Re: [support] Changing checkboxes options
            during hook_node_validate<u></u><u></u></span></p>
        <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
        <div>
          <p class=3D"MsoNormal">Ok, I&#39;ve tried that. The last thing I =
do
            in hook_node_validate is $form_state[&#39;rebuild&#39;] =3D tru=
e; 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.<=
u></u><u></u></p>
        </div>
        <div>
          <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
          <div>
            <p class=3D"MsoNormal">On Wed, Feb 11, 2015 at 10:57 AM, Jamie
              Holly &lt;<a href=3D"mailto:[email protected]" targe=
t=3D"_blank">[email protected]</a>&gt;
              wrote:<u></u><u></u></p>
            <div>
              <div>
                <p class=3D"MsoNormal">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;rebuild&#39;] =3D TRUE; and check in you=
r
                  hook_form or hook_form_alter for your
                  $form_state[&#39;storage&#39;] setting, and if set then a=
dd
                  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 are after, the logic is still the same.
                  <br>
                  <br>
                  <u></u><u></u></p>
                <pre>Jamie Holly<u></u><u></u></pre>
                <pre><a href=3D"http://hollyit.net" target=3D"_blank">http:=
//hollyit.net</a><u></u><u></u></pre>
                <p class=3D"MsoNormal">On 2/11/2015 10:43 AM, Jeff
                  Greenberg wrote:<u></u><u></u></p>
              </div>
              <blockquote style=3D"margin-top:5.0pt;margin-bottom:5.0pt">
                <div>
                  <p class=3D"MsoNormal">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_state gets the information for the field when
                    rebuilding the form.<u></u><u></u></p>
                </div>
                <div>
                  <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
                  <div>
                    <p class=3D"MsoNormal">On Wed, Feb 11, 2015 at 9:31
                      AM, Jeff Greenberg &lt;<a href=3D"mailto:listmail.aye=
[email protected]" target=3D"_blank">[email protected]</a>&gt=
;
                      wrote:<u></u><u></u></p>
                    <div>
                      <div>
                        <p style=3D"margin-right:0in;margin-bottom:12.0pt;m=
argin-left:0in;vertical-align:baseline">I
                          have a node form that contains a checkboxes
                          field with options NULL at the time the form
                          is displayed.<u></u><u></u></p>
                        <p style=3D"margin-right:0in;margin-bottom:12.0pt;m=
argin-left:0in;vertical-align:baseline">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.<u></u><u></u></p>
                        <p style=3D"margin-right:0in;margin-bottom:12.0pt;m=
argin-left:0in;vertical-align:baseline">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.<u></u>=
<u></u></p>
                        <p style=3D"margin-right:0in;margin-bottom:12.0pt;m=
argin-left:0in;vertical-align:baseline">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.<u></u><u></u></p>
                        <div>
                          <p class=3D"MsoNormal"><span style=3D"color:#8888=
88"><u></u>=C2=A0<u></u></span></p>
                        </div>
                      </div>
                      <div>
                        <p class=3D"MsoNormal"><span style=3D"color:#888888=
"><u></u>=C2=A0<u></u></span></p>
                      </div>
                      <p class=3D"MsoNormal"><span><span style=3D"color:#88=
8888">-- </span><u></u><u></u></span></p>
                      <div>
                        <p class=3D"MsoNormal"><span style=3D"color:#888888=
">---
                          </span><u></u><u></u></p>
                        <div>
                          <p class=3D"MsoNormal"><span style=3D"color:#8888=
88"><a href=3D"http://drupal.org/user/367108" target=3D"_blank">drupal.org/=
user/367108</a><u></u><u></u></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span style=3D"color:#8888=
88"><a href=3D"http://linkedin.com/in/jeffrgreenberg" target=3D"_blank">lin=
kedin.com/in/jeffrgreenberg</a><u></u><u></u></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span style=3D"color:#8888=
88"><a href=3D"http://accidentalcoder.com" target=3D"_blank">accidentalcode=
r.com</a>
                              /
                              <a href=3D"http://ayendesigns.com" target=3D"=
_blank">ayendesigns.com</a><u></u><u></u></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span style=3D"color:#8888=
88">@accidentalcoder<u></u><u></u></span></p>
                        </div>
                      </div>
                    </div>
                  </div>
                  <p class=3D"MsoNormal"><span style=3D"color:#888888"><br>
                      <br clear=3D"all">
                    </span><span><u></u><u></u></span></p>
                  <div>
                    <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
                  </div>
                  <p class=3D"MsoNormal"><span><span style=3D"color:#888888=
">-- </span><u></u><u></u></span></p>
                  <div>
                    <p class=3D"MsoNormal"><span style=3D"color:#888888">--=
-
                      </span><u></u><u></u></p>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888">=
<a href=3D"http://drupal.org/user/367108" target=3D"_blank">drupal.org/user=
/367108</a><u></u><u></u></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888">=
<a href=3D"http://linkedin.com/in/jeffrgreenberg" target=3D"_blank">linkedi=
n.com/in/jeffrgreenberg</a><u></u><u></u></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888">=
<a href=3D"http://accidentalcoder.com" target=3D"_blank">accidentalcoder.co=
m</a> /
                          <a href=3D"http://ayendesigns.com" target=3D"_bla=
nk">ayendesigns.com</a><u></u><u></u></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888">=
@accidentalcoder<u></u><u></u></span></p>
                    </div>
                  </div>
                </div>
                <p class=3D"MsoNormal" style=3D"margin-bottom:12.0pt"><u></=
u>=C2=A0<u></u></p>
              </blockquote>
              <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
            </div>
            <p class=3D"MsoNormal"><br>
              --<br>
              [ Drupal support list | <a href=3D"http://lists.drupal.org/" =
target=3D"_blank">http://lists.drupal.org/</a>
              ]<u></u><u></u></p>
          </div>
          <p class=3D"MsoNormal"><br>
            <br clear=3D"all">
            <u></u><u></u></p>
          <div>
            <p class=3D"MsoNormal"><u></u>=C2=A0<u></u></p>
          </div>
          <p class=3D"MsoNormal">-- <u></u><u></u></p>
          <div>
            <p class=3D"MsoNormal">---<u></u><u></u></p>
            <div>
              <p class=3D"MsoNormal"><a href=3D"http://drupal.org/user/3671=
08" target=3D"_blank">drupal.org/user/367108</a><u></u><u></u></p>
            </div>
            <div>
              <p class=3D"MsoNormal"><a href=3D"http://linkedin.com/in/jeff=
rgreenberg" target=3D"_blank">linkedin.com/in/jeffrgreenberg</a><u></u><u><=
/u></p>
            </div>
            <div>
              <p class=3D"MsoNormal"><a href=3D"http://accidentalcoder.com"=
 target=3D"_blank">accidentalcoder.com</a>
                /
                <a href=3D"http://ayendesigns.com" target=3D"_blank">ayende=
signs.com</a><u></u><u></u></p>
            </div>
            <div>
              <p class=3D"MsoNormal">@accidentalcoder<u></u><u></u></p>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
    </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>

--bcaec5196c2531d2f4050ee5a291--

--===============2905058357156927762==
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/ ]
--===============2905058357156927762==--