List Info

Thread: Forms API question: "checkboxes" type and validation thereof




Forms API question: "checkboxes" type and validation thereof
user name
2006-09-28 18:21:43
Greetings!

This may be a dumb question, but I can't seem to find any
Drupal core example
code that does what I need.

In a module settings form, I have a "checkboxes"
form element with a list of
options of the format:

    array($short_name => $description, ...)

The goal (which is being achieved at the HTML level) is to
have code like this:

   <label class="option"><input
type="checkbox" name="..."
value="$short_name"/>
             $description</label>

That's all working fine, and the returned values for checked
boxes are as
desired. I have no problem getting the FAPI to render and
process the form
correctly, but what's happening is that any *unchecked*
boxes are being set to
the value zero rather than being left unset. This is causing
extra cruft in my
database records because what I really want is just the list
of shortnames of
the checked values.

I've been through the docs and examples, and looked at
several parts of Drupal
core, to see how to use #validate on a form, but is there a
way to use it on
just one form element and then *change* the value of that
element by removing
undesired parts? Or is that something I should do in
#submit?

Several parts of the docs have "almost" what I
need, I think:

   http://drupal.org/node/5
1104
   http://drupal.org/node/5
8689

One of the reasons this is so tricky is that the form
element in question happens
to be used in the module settings (where I don't have
control over the processing)
and also in the user preferences (where I also don't have
control over the
processing). I need to be able to hook in somewhere between
when the browser
sends via POST and when the module and user settings get
stored.

What's the correct approach (in general terms, not looking
for someone to write
the code for me) to do this?

Thanks for any suggestions.

Syscrusher

-- 
------------------------------------------------------------
-------------------
Syscrusher (Scott Courtney)          Drupal page:   http://drupal.org/user/91
84
syscrusher at 4th dot com            Home page:     http://4th.com/   
Forms API question: "checkboxes" type and validation thereof
user name
2006-09-28 20:11:14
Hi!  In the conversion to 5.0, they're getting rid of
hook_settings.   
However, there is no reason that you can't make the
conversion in 4.7  
modules.  Just add a MENU_NORMAL_ITEM at the path
'admin/settings/ 
yourmodule', and implement your own form handling.

If you want an example of a place I do this, I can send you
a module  
I've written that handles this kind of issue off list.

(Also, you will want to know about array_filter()).

HTH,
Ricky

On Sep 28, 2006, at 2:21 PM, Syscrusher wrote:

> Greetings!
>
> This may be a dumb question, but I can't seem to find
any Drupal  
> core example
> code that does what I need.
>
> In a module settings form, I have a
"checkboxes" form element with  
> a list of
> options of the format:
>
>     array($short_name => $description, ...)
>
> The goal (which is being achieved at the HTML level) is
to have  
> code like this:
>
>    <label class="option"><input
type="checkbox" name="..."  
> value="$short_name"/>
>              $description</label>
>
> That's all working fine, and the returned values for
checked boxes  
> are as
> desired. I have no problem getting the FAPI to render
and process  
> the form
> correctly, but what's happening is that any *unchecked*
boxes are  
> being set to
> the value zero rather than being left unset. This is
causing extra  
> cruft in my
> database records because what I really want is just the
list of  
> shortnames of
> the checked values.
>
> I've been through the docs and examples, and looked at
several  
> parts of Drupal
> core, to see how to use #validate on a form, but is
there a way to  
> use it on
> just one form element and then *change* the value of
that element  
> by removing
> undesired parts? Or is that something I should do in
#submit?
>
> Several parts of the docs have "almost" what
I need, I think:
>
>    http://drupal.org/node/5
1104
>    http://drupal.org/node/5
8689
>
> One of the reasons this is so tricky is that the form
element in  
> question happens
> to be used in the module settings (where I don't have
control over  
> the processing)
> and also in the user preferences (where I also don't
have control  
> over the
> processing). I need to be able to hook in somewhere
between when  
> the browser
> sends via POST and when the module and user settings
get stored.
>
> What's the correct approach (in general terms, not
looking for  
> someone to write
> the code for me) to do this?
>
> Thanks for any suggestions.
>
> Syscrusher
>
> -- 
>
------------------------------------------------------------
---------- 
> ---------
> Syscrusher (Scott Courtney)          Drupal page:  
http:// 
> drupal.org/user/9184
> syscrusher at 4th dot com            Home page:     http://4th.com/

Forms API question: "checkboxes" type and validation thereof
user name
2006-09-28 20:53:34
On Thursday 28 September 2006 16:11, Richard Morse wrote:
> Hi!  In the conversion to 5.0, they're getting rid of
hook_settings.   
> However, there is no reason that you can't make the
conversion in 4.7  
> modules.  Just add a MENU_NORMAL_ITEM at the path
'admin/settings/ 
> yourmodule', and implement your own form handling.

I'm actually already doing this in 4.7 because I have some
settings sub-pages
that I *don't* want in the main navigation menu by default.
But where is the
right place for me to inject the change to the values?
#submit, or #validate,
or somewhere else? #process looked promising, but it's
documented as INTERNAL
and I'm loathe to rely on it being a stable API because of
that.
 
> If you want an example of a place I do this, I can send
you a module  
> I've written that handles this kind of issue off list.

Please do! I would appreciate this.

> 
> (Also, you will want to know about array_filter()).

Lemme guess...I just finished reinventing a wheel.  No, I
didn't know about
that but will look it up.

Syscrusher

-- 
------------------------------------------------------------
-------------------
Syscrusher (Scott Courtney)          Drupal page:   http://drupal.org/user/91
84
syscrusher at 4th dot com            Home page:     http://4th.com/   
Forms API question: "checkboxes" type and validation thereof
user name
2006-09-28 21:03:23
Richard Morse schrieb:
> ...
> If you want an example of a place I do this, I can send
you a module  
> I've written that handles this kind of issue off list.
>   
Is it a public module? I had the same problem (ok, I didn't
realized it 
as a real problem because there were some others  ) with
my module. 
It would be great if you could post the relevant code (or a
link to the 
module).
> (Also, you will want to know about array_filter()).
>   
Ok, tell us more...

greets,

 Stefan
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )