-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render checkboxes and radios box groups #1155
Comments
Modified the protected function _fields(array $fields, array $options = array()) {
$result = array();
foreach ($fields as $field => $label) {
if (is_numeric($field)) {
$field = $label;
unset($label);
}
//Start of where I added
if(is_array($label)) {
foreach($label as $l => $opts) {
if(is_array($opts)) {
$result[] = $this->field($field, array('label' => $l) + $opts + $options);
} else {
$result[] = $this->field($field, array('label' => $opts) + $options);
}
}
} else {
$result[] = $this->field($field, compact('label') + $options);
}
//End of what I added
}
return join("\n", $result);
} I added the $this->form->field(array('test' => array('Label' => array('id' => 'TestField', 'checked' => true), 'test')), array( 'type' => 'radio')) This would create two radio buttons with the name |
Hi @DrRoach, Thanks for sharing your solution. I really appreciate your time to make some similar solution for my needs. I have been wondering how to use the My function code seem to be a little longer then yours. However, it has a better benefit for a front-end developer. Most call to generate checkboxes and radios will be much easily understandable as example below: $values = array("a" => "Option A", "b" => "Option B", "c" => "Option C");
echo $this->Form->checkboxes('somecheckboxes[]', $values, array("a", "c"););
echo $this->Form->radios('someradios', $values, "b"); Results for checkboxes: Results for radios: In additions they can include some prefix and suffix in an array of options. Your solution on the other hand, will work great if the Many thanks. |
Fair enough, I see your point, if you or other people want I will expand on what you have got to allow for extra features like adding ids and classes? |
@DrRoach The main idea here is to have:
Many thanks. |
Another idea would be to repurpose the |
Using the public function radios($name, array $radios = array())
{
$return = '';
foreach($radios as $options) {
$return .= $this->radio($name, $options);
}
return $return;
} That's the code, shall I make a pull request? |
@DavidPersson Thanks for looking at this again. |
I don't know what the |
My idea would be to not create additional radios/checkboxes methods, but make people utilize $this->form->field('colors', array('type' => 'checkbox', 'list' => array('b' => 'blue', 'g' => 'green));
At the same time |
Hello @nateabele and Team,
I'm working on a project using Lithium. Some pages require a list of checkboxes to be shown. We can just use HTML or individual
Form->checkbox(...)
andForm->radio(...)
within foreach. But I find that's not very friendly for a template maker.Therefore I made a working snippet of Form helper that can print out a list of checkboxes and radios (and anything) depending on what input type has been chosen.
I would like to contribute that to this project. I know it might not be perfect or following Lithium standards yet. But if consider we can optimize it then hopefully can be included as Lithium functions.
Here is the code:
The text was updated successfully, but these errors were encountered: