Re: Changing checkboxes options during hook_node_validate

Jamie Holly <[email protected]> Wed, 11 Feb 2015 14:25:12 -0500
Newsgroups gmane.comp.php.drupal.support
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============8186529861573191292==
Content-Type: multipart/alternative;
	boundary="------------050306090505060008080805"

This is a multi-part message in MIME format.
--------------050306090505060008080805
Content-Type: text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding: 7bit

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 Holly
http://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]] 
> *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] <mailto:[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]
>     <mailto:[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 <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.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
>
>
>


--------------050306090505060008080805
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3Dwindows-1252"
      http-equiv=3D"Content-Type">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">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'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 class=3D"moz-txt-link-freetext" href=3D"http://cgit.drupalcode.org/exa=
mples/tree/form_example/form_example_states.inc">http://cgit.drupalcode.o=
rg/examples/tree/form_example/form_example_states.inc</a><br>
      <br>
      <pre class=3D"moz-signature" cols=3D"72">Jamie Holly
<a class=3D"moz-txt-link-freetext" href=3D"http://hollyit.net">http://hol=
lyit.net</a></pre>
      On 2/11/2015 11:58 AM, Metzler, David wrote:<br>
    </div>
    <blockquote
cite=3D"mid:[email protected]=
u"
      type=3D"cite">
      <meta http-equiv=3D"Content-Type" content=3D"text/html;
        charset=3Dwindows-1252">
      <meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered
        medium)">
      <style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
	{font-family:Tahoma;
	panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
	{font-family:Consolas;
	panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
p
	{mso-style-priority:99;
	mso-margin-top-alt:auto;
	margin-right:0in;
	mso-margin-bottom-alt:auto;
	margin-left:0in;
	font-size:12.0pt;
	font-family:"Times New Roman","serif";}
pre
	{mso-style-priority:99;
	mso-style-link:"HTML Preformatted Char";
	margin:0in;
	margin-bottom:.0001pt;
	font-size:10.0pt;
	font-family:"Courier New";}
span.HTMLPreformattedChar
	{mso-style-name:"HTML Preformatted Char";
	mso-style-priority:99;
	mso-style-link:"HTML Preformatted";
	font-family:"Consolas","serif";}
span.hoenzb
	{mso-style-name:hoenzb;}
span.EmailStyle21
	{mso-style-type:personal-reply;
	font-family:"Calibri","sans-serif";
	color:#1F497D;}
.MsoChpDefault
	{mso-style-type:export-only;
	font-family:"Calibri","sans-serif";}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
      <div class=3D"WordSection1">
        <p class=3D"MsoNormal"><span
style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-seri=
f&quot;;color:#1F497D">Actually
            you should be able to alter the form in the validation
            function yes?=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? =A0=A0<o:p></o=
:p></span></p>
        <p class=3D"MsoNormal"><span
style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-seri=
f&quot;;color:#1F497D"><br>
            Dave<o:p></o:p></span></p>
        <p class=3D"MsoNormal"><span
style=3D"font-size:11.0pt;font-family:&quot;Calibri&quot;,&quot;sans-seri=
f&quot;;color:#1F497D"><o:p>=A0</o:p></span></p>
        <p class=3D"MsoNormal"><b><span
style=3D"font-size:10.0pt;font-family:&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 class=3D"moz-txt-link-abbreviated" href=3D"mailto:support-=
[email protected]">[email protected]</a>
            [<a class=3D"moz-txt-link-freetext" href=3D"mailto:support-bo=
[email protected]">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 class=3D"moz-txt-link-abbreviated" href=3D"mail=
to:[email protected]">[email protected]</a><br>
            <b>Subject:</b> Re: [support] Changing checkboxes options
            during hook_node_validate<o:p></o:p></span></p>
        <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
        <div>
          <p class=3D"MsoNormal">Ok, I've tried that. The last thing I do
            in hook_node_validate is $form_state['rebuild'] =3D true; Wha=
t
            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.<o:p></=
o:p></p>
        </div>
        <div>
          <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
          <div>
            <p class=3D"MsoNormal">On Wed, Feb 11, 2015 at 10:57 AM, Jami=
e
              Holly &lt;<a moz-do-not-send=3D"true"
                href=3D"mailto:[email protected]" target=3D"_bla=
nk">[email protected]</a>&gt;
              wrote:<o:p></o:p></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['storage'] and set
                  $form_state['rebuild'] =3D TRUE; and check in your
                  hook_form or hook_form_alter for your
                  $form_state['storage'] setting, and if set then add
                  your checkboxes.
                  <br>
                  <br>
                  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.
                  <br>
                  <br>
                  <o:p></o:p></p>
                <pre>Jamie Holly<o:p></o:p></pre>
                <pre><a moz-do-not-send=3D"true" href=3D"http://hollyit.n=
et" target=3D"_blank">http://hollyit.net</a><o:p></o:p></pre>
                <p class=3D"MsoNormal">On 2/11/2015 10:43 AM, Jeff
                  Greenberg wrote:<o:p></o:p></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'm wondering where
                    $form_state gets the information for the field when
                    rebuilding the form.<o:p></o:p></p>
                </div>
                <div>
                  <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
                  <div>
                    <p class=3D"MsoNormal">On Wed, Feb 11, 2015 at 9:31
                      AM, Jeff Greenberg &lt;<a moz-do-not-send=3D"true"
                        href=3D"mailto:[email protected]"
                        target=3D"_blank">[email protected]<=
/a>&gt;
                      wrote:<o:p></o:p></p>
                    <div>
                      <div>
                        <p
style=3D"mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;mar=
gin-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.<o:p></o:p></p>
                        <p
style=3D"mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;mar=
gin-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.<o:p></o:p></p>
                        <p
style=3D"mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;mar=
gin-left:0in;vertical-align:baseline">I've
                          tried setting #options =3D the new array in the
                          $form, in $form_state[original_form], etc all
                          to no avail. The field doesn't change.<o:p></o:=
p></p>
                        <p
style=3D"mso-margin-top-alt:0in;margin-right:0in;margin-bottom:12.0pt;mar=
gin-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.<o:p></o:p></p>
                        <div>
                          <p class=3D"MsoNormal"><span
                              style=3D"color:#888888"><o:p>=A0</o:p></spa=
n></p>
                        </div>
                      </div>
                      <div>
                        <p class=3D"MsoNormal"><span style=3D"color:#8888=
88"><o:p>=A0</o:p></span></p>
                      </div>
                      <p class=3D"MsoNormal"><span class=3D"hoenzb"><span
                            style=3D"color:#888888">-- </span><o:p></o:p>=
</span></p>
                      <div>
                        <p class=3D"MsoNormal"><span style=3D"color:#8888=
88">---
                          </span><o:p></o:p></p>
                        <div>
                          <p class=3D"MsoNormal"><span
                              style=3D"color:#888888"><a
                                moz-do-not-send=3D"true"
                                href=3D"http://drupal.org/user/367108"
                                target=3D"_blank">drupal.org/user/367108<=
/a><o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span
                              style=3D"color:#888888"><a
                                moz-do-not-send=3D"true"
                                href=3D"http://linkedin.com/in/jeffrgreen=
berg"
                                target=3D"_blank">linkedin.com/in/jeffrgr=
eenberg</a><o:p></o:p></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span
                              style=3D"color:#888888"><a
                                moz-do-not-send=3D"true"
                                href=3D"http://accidentalcoder.com"
                                target=3D"_blank">accidentalcoder.com</a>
                              /
                              <a moz-do-not-send=3D"true"
                                href=3D"http://ayendesigns.com"
                                target=3D"_blank">ayendesigns.com</a><o:p=
></o:p></span></p>
                        </div>
                        <div>
                          <p class=3D"MsoNormal"><span
                              style=3D"color:#888888">@accidentalcoder<o:=
p></o:p></span></p>
                        </div>
                      </div>
                    </div>
                  </div>
                  <p class=3D"MsoNormal"><span style=3D"color:#888888"><b=
r>
                      <br clear=3D"all">
                    </span><span class=3D"hoenzb"><o:p></o:p></span></p>
                  <div>
                    <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
                  </div>
                  <p class=3D"MsoNormal"><span class=3D"hoenzb"><span
                        style=3D"color:#888888">-- </span><o:p></o:p></sp=
an></p>
                  <div>
                    <p class=3D"MsoNormal"><span style=3D"color:#888888">=
---
                      </span><o:p></o:p></p>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888=
"><a
                            moz-do-not-send=3D"true"
                            href=3D"http://drupal.org/user/367108"
                            target=3D"_blank">drupal.org/user/367108</a><=
o:p></o:p></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888=
"><a
                            moz-do-not-send=3D"true"
                            href=3D"http://linkedin.com/in/jeffrgreenberg=
"
                            target=3D"_blank">linkedin.com/in/jeffrgreenb=
erg</a><o:p></o:p></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888=
"><a
                            moz-do-not-send=3D"true"
                            href=3D"http://accidentalcoder.com"
                            target=3D"_blank">accidentalcoder.com</a> /
                          <a moz-do-not-send=3D"true"
                            href=3D"http://ayendesigns.com"
                            target=3D"_blank">ayendesigns.com</a><o:p></o=
:p></span></p>
                    </div>
                    <div>
                      <p class=3D"MsoNormal"><span style=3D"color:#888888=
">@accidentalcoder<o:p></o:p></span></p>
                    </div>
                  </div>
                </div>
                <p class=3D"MsoNormal" style=3D"margin-bottom:12.0pt"><o:=
p>=A0</o:p></p>
              </blockquote>
              <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
            </div>
            <p class=3D"MsoNormal"><br>
              --<br>
              [ Drupal support list | <a moz-do-not-send=3D"true"
                href=3D"http://lists.drupal.org/" target=3D"_blank">http:=
//lists.drupal.org/</a>
              ]<o:p></o:p></p>
          </div>
          <p class=3D"MsoNormal"><br>
            <br clear=3D"all">
            <o:p></o:p></p>
          <div>
            <p class=3D"MsoNormal"><o:p>=A0</o:p></p>
          </div>
          <p class=3D"MsoNormal">-- <o:p></o:p></p>
          <div>
            <p class=3D"MsoNormal">---<o:p></o:p></p>
            <div>
              <p class=3D"MsoNormal"><a moz-do-not-send=3D"true"
                  href=3D"http://drupal.org/user/367108" target=3D"_blank=
">drupal.org/user/367108</a><o:p></o:p></p>
            </div>
            <div>
              <p class=3D"MsoNormal"><a moz-do-not-send=3D"true"
                  href=3D"http://linkedin.com/in/jeffrgreenberg"
                  target=3D"_blank">linkedin.com/in/jeffrgreenberg</a><o:=
p></o:p></p>
            </div>
            <div>
              <p class=3D"MsoNormal"><a moz-do-not-send=3D"true"
                  href=3D"http://accidentalcoder.com" target=3D"_blank">a=
ccidentalcoder.com</a>
                /
                <a moz-do-not-send=3D"true" href=3D"http://ayendesigns.co=
m"
                  target=3D"_blank">ayendesigns.com</a><o:p></o:p></p>
            </div>
            <div>
              <p class=3D"MsoNormal">@accidentalcoder<o:p></o:p></p>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class=3D"mimeAttachmentHeader"></fieldset>
      <br>
    </blockquote>
    <br>
  </body>
</html>

--------------050306090505060008080805--

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