CCK - checkbox enabled textfield - HELP!
schoenae - June 25, 2007 - 18:12
Hi,
I was wondering, if there's a module similar to the one I'm desperately needing! I need to have a CCK widget that includes a textfield that can be enabled with a checkbox. Like the last choise of checkboxes that has "other" -option...;)
There should be the option to have multiple rows. I have some start in the code, but since I'm just a basic coder, I would appreciate the help.
Also, if there's someone with same kind of needs or visions packed with the ability, I'm willing to pay for the job!!!!
This project is rather urgent, so if possible, please answer quickly! Thank you!

...
I was sure I've read about a module that does something similar. Didn't find it. Whatever.
Why don't you simply use Javascript to hide/show the textfield(s)? It has the drawback that the "other" field isn't realy cleared from the DB when you clear the checkbox --it only vanishes form the form.
Let's say you have a "Pets" field (that is, "field_pets") which is a set of checkboxes. That is, it's a "text" field having the "Multiple values" attribute and the "radio buttons" widget.
Let's say its "Allowed values list" is:
DonkeyDog
Cat
Cow
other|Manualy type your pet(s)
And let's say you have an "Other pets" field (that is, "field_other_pets") which is of type "text" and has the "Multiple values" attribute. You want this field to show when the 'other' checkbox is checekd.
Your page should contain the following JS code:
<script type="text/javascript">$(function() {
$('#edit-field-pets-keys-other').click(function() {
with ($('#edit-field-other-pets-0-value').ancestors('fieldset:first')) {
$(this).is(':checked') ? show() : hide();
}
}).click();
});
</script>
This will do the magic.
Note the
other|Manualy type your pet(s)thing. "other" will be the actual value stored in the field. This syntax gives us freedom in choosing a label --we don't have to modify the JS code whenever we change this label. (And it allows us to have "other" in the code and yet "Other" on the form: by doingother|Other.)The hard part is to include this JS code in the page. It's actually simple, but it would require me to write lenghy instructions. Meanwhile use the instructions here.
Does anybody know of a module that lets the admin put some arbitrary JS code in the page? It would be a most useful module.
jQuery cool, but...
Thanks for the answer. The javascript version would probably do fine, no doubt. Actually, a jQuery version would be the coolest. Example: http://www.ubercart.org/jquery_dynamic_form/18
But how the last checkbox would be declared in the new CCK module? I've tried doing something with the help of the Drupal Dojo custom CCK module podcast, but I don't seem to understand a couple things:
a) what kind of element should be declared? Custom checkboxes? A checkbox that's added to checkboxes (a setting inside the checkbox module)?
b) how to set up custom checkboxes that I can manipulate (referring to the point above)? The settings or add a separate on/off checkbox with a textfield?
Thanks! Really appreciate the help!
Btw. If anyone is interested in this work, I'll gladly pay for it. Please contact me at antti.kettunen@solidi.net
...
But the code I gave is jQuery.
This code is a very bad Drupal code. It falsely gives the impression that it uses the Form API.
I'm not sure I get you. You don't know how to create checkboxes?
Suppose you want a to have a field, "pets I have", which can have one or more of three values: cat, dog, donkey. Do you know how to display this field as three checkboxes?
We call what you see on the form a widget. Widgets are the way we interact with fields. Checkboxes are just another widget to display a field.
Or is it that you emphasize the last checkbox? Yeah, it's possible to make it a separate field. An integer field, for example, that's assigned either '0' or '1'.
I'm afraid your questions are too vague/general. It's better to describe your specific case.
...
I have to explain an important point:
There are two ways to solve a problem: either use existing tools, or write new ones.
The solution I gave uses existing tools: existing fields and widgets. Therefore this solution has some drawbacks. for example, since the "other" data items the user types in are a separate field, it would be almost impossible to combine them for the benefit of the Views module. I.e., if you use the "summary" listing of Views on the "pets" fields, this summary will include only the three values you defined for this field. This summary will not include the "lizard" I typed in the textfield.
A better solution would be to write a new tool: a new widget. Say "open checkboxes" (the "open" means we are able to type values ourselves).
But sometimes it's not economical to develope new tools.
BTW, I wonder if somebody has written this "open chekboxes", or "open radios", widget yet. I planned to, in the past.
Oops...
looks like you were faster! What we would do probably in this project, is to update the "courses" -checkboxes with the user entered values. Since the client knows most of the available courses, there shouldn't be many additions.
In fact, the idea that I have with this module, could include the addition of the "other" field value into the database.
Shouldn't it be just to have a data processing case, which checks if the entered data already exists in the database (in the checkbox values) and if not, then add it???
...
No, it probably can't be done automatically, because you want a human as a judge: maybe it's an invalid course, or maybe the user used some synonym for an already existing one. The machine is dumb.
(I'll soon reply to your other points.)
...
Let's sum this discussion up, at least temporarily:
It's likely that you can do with the existing tools. You're likely to have further questions addressed to this forum.
Yes, that's the way to go. Using the 'Views' module you can locate all nodes having custom typed pets, and the human sitting there could edit these nodes and update the "Allowed values" of the pets field. This has a huge advantage: the pets are all stored in a single field, not in two fields.
If you need something more automated, or more sofisticated, I think you'd better try the "paid services" forum.
Hmmm...
What a faux pass. It's only now that I notice we're in the "Module development" forum. I though we are in the "Post installation" one. It means that I've read your messages somewhat wrong.
I'm a bit embarrassed now :*)
But my opinions haven't changed --though I might have used different words.
Sorry...
.. I was probably very unclear/unspesific...! I didn't even look at your code, of course it's jQuery! LOL! (embarrased) :P
Let me be thorough of my needs:
I need basically an addition to a set of checkboxes (which I do know how to make, if it's like a norma form api checkboxes), which would have the "other" -textfield next to it, as you already understood it correctly. The user needs to select his/her optional courses (multiple) and the also have the option to input a new course.
So, the way I now understand it, I could do it by implementing in hook_widget these form elements:
<?php
$form['courses']['keys'] = array(
'#type' => 'checkboxes',
'#title' => t('courses'),
'#options' => $options,
'#description' => t($field['widget']['description']),
);
$form['courses']['other']['key'] = array(
'#type' => 'checkbox',
'#title' => t('Other'),
);
$form['courses']['other']['field'] = array(
'#type' => 'textfield',
'#title' => t('Type your custom course'),
'#description' => t($field['widget']['description']),
);
?>
...or something like that? And then theme them to look like one smoothly running element... ;)
A correction
Ooops. I meant checkboxes, not radio button.
I don't know how I came to
I don't know how I came to skip the bottom of your first answer. I think that should do it... one thing is what I don't understand:
So, if I would have a checkbox with
other|Manually type your courseand the jQuery you posted, in there. The user would click the checkbox and a textfield would be enabled. But will the typed value be "other" or "Manually type..." or the user entered data?...
Let's assume this is how the form looks before submission:
Pets:
[ ] Donkey
[x] Dog
[x] Cat
[ ] Cow
[x] Manualy type your pet(s)
Other pets:
[Lizard____]
[__________]
[__________]
After submision, the "field_pets" field would contain three values: "Dog", "Cat" and "other". The "field_other_pets" would contain one value: "Lizard".
If you don't want this "other" value to polute your "field_pets" field, define a new field to represent this chekbox (e.g. "field_other_pets_exist") and remove the
other|Manualy type...line from the options in "field_pets".The javascript adds cosmetics only, and apart from that all is quite basic. I suggest that you ignore the JS portion for the time being and experiment with possible setups for your form. The JS has completely no effect on the DB.
How 'bout show/hide fieldgroup
hi mooffie,
this code works great. thanks!!
is it possible to show/hide an entire fieldgroup with this mechanism. CCK fieldgroups don't seem to have ID's, but they are clickable if collapsable. is there a different way to do that?
thanks,
m