Why is CakePHP Form helper grouping my form radio options? - php

echo $this->Form->create('AmazonMatches', array('action' => 'selectMatches'));
echo $this->Form->input('option_id', array('options' => $allAmazonMatches, 'type' => 'radio'));
echo $this->Form->end(__('Submit', true));
Now I see a box around my radio buttons with a large red text saying "Option Id".
How can i get rid of it? Sorry I am a total Cake noob.

You need to set the 'legend' option to false if you don't want to show it, or to a string if you want to customize the message:
echo $this->Form->input('option_id', array(
'options' => $allAmazonMatches,
'type' => 'radio',
'legend' => false
));

$this->Form->input
Creates one input field with the id provided. You'll have to create multiple inputs inorder to make your checkboxes work separately.
There could be better methods, but doing it something likethis will work.
foreach($allAmazonMatches as $amazonMatch)
{
$this->Form->input...
}

Related

Custom Post Type meta box Radio-button(s) output

I like to do a simple thing with my custom meta box, but i d'ont find a (easy) solution for this problem.
I have done a custom post type with some meta boxes => text, textarea, select fields, ect.
all work just fine. Now if i set the button to sold,(see code excerpt from the radio array below) the text(div) should be red; if i set it to for_sale, it should be green.
but i have no idea how to do this, i mean there are this two states possible. nothing else.
and that is what i want.
but the html output is only text. or text in a html-tag, this i can style with css, but it would be red, or it is blue, green what else i sett it.
is there a simple thing, like a class to set for each value? i c'ant find an answer. i hope i have explained it understandable. thanks so much for taking time for this.
array(
'label' => 'Status',
'desc' => 'product status',
'id' => $prefix.'radio_status',
'type' => 'radio',
'options' => array (
'one' => array (
'label' => 'Product for sale',
'value' => 'for_sale'
),
'two' => array (
'label' => 'Product is sold',
'value' => 'is_sold'
),
),
),
EDIT :)
<span class="saleBox"><span class="noBox"><?php echo $post_meta_data['custom_radio_status'][0]; ?></span></span>
Thats the HTML, and what i get is the value thats setted.
<span class="saleBox"><span class="noBox">for sale</span></span>
or then: *soled
So i need a Output with a true-value, or class, ID,... something that i can adress with css.
A good idea that works. only thing that i doesen't understand is, is must call the array.
<?php $post_meta_data = get_post_custom($post->ID); ?>
if i call only the specific data:
<?php get_post_meta($post->ID, 'custom_radio_status', true) ?>
for me, it won't. or i made a typo. it doesen't matter i works!
thank you mr. Maguire
Put a conditional statement, something like:
<span class = "
<?php
if ($post_meta_data['custom_radio_status'][0] == "is_sold"){
echo "red_class";
}else{
echo "green_class";
}
?>
">

Passing data back to form in CakePHP

I have 2 actions in my controller. First action to display form and fetch lists for the form fields from related HABTM models. Second action to perform searching and display results.
First action:
public function advancedSearch() {
$this->set($this->Restaurant->fetchRelatedData());
}
Also I have View for this action which displays the form: advancedSearch.ctp
echo $this->Form->create(null, array(
'type' => 'get',
'url' => '/restaurants/search', 'inputDefaults' => array(
'label' => false,
'div' => false
)));
echo $this->Form->input('companyname', array('label' => 'Name:'));
echo $this->Form->input('addr', array('label' => 'Address:'));
echo $this->Form->input('district_id', array('label' => 'District:'));
echo $this->Form->input('Station', array('label' => 'Subway station(s):'));
// etc.
echo $this->Form->end('Go!');
As you can see, this form sends data to my second action which performs actual searching.
I need to mention that I use pagination component in my search action and because of this I use the GET method.
My problem is that user may select some "simple" fields and some multiselect fields, send the form and after that he may want to refine search result.
So I need to implement functionality to pass data back to my form and populate fields with values which user has selected before.
How can I do that? Thanks.

CakePHP form appending div and label automatically

I am using Cakephp 2.2.3 version. When I create form using Cake form helpers, it automatically appends a div and label to the input type field. How to avoid it?
Following is the code:
<?php echo $this->Form->input('username', array('id' => 'username', 'class' => 'login-inp', 'type' => 'text')); ?>
You can use options array of input to avoid form appending div and label automatically. Set div and label of options array to false.
echo $this->Form->input('username',
array('id' => 'username', 'class' => 'login-inp',
'div' => false, 'label' => false
)
);
That's what FormHelper::input() is supposed to do. If you don't want the label and wrapping div just use the functions to generate specific input elements only like FormHelper::text(), FormHelper::select(), FormHelper::radio(), etc.

How to display custom Form Drop down list label?

I can't figure out how to edit the label output by cakephp 2.0 in a form.
I have a simple drop down list with 3 items and all i want to do is change the text thats outputed (which of course prints the fieldname) how on earth do i do it. Ive looked a the 1.3 and 2.0 docs and i couldnt figure it out;
echo $this->Form->input('accounttype', array('options'=>array('customer' => 'Customer', 'retailer' => 'Retailer','manufacturer'=>'Manufacturer')));
Use the label property to achieve this:
echo $this->Form->input(
'accounttype',
array(
'label' => 'Foo',
'options'=>array(
'customer' => 'Customer',
'retailer' => 'Retailer',
'manufacturer'=>'Manufacturer'
)
)
);
See also:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
Just add the label property:
echo $this->Form->input('accounttype', array('label' => 'Account Type'));

CakePHP Radio Buttons

I am using CakePHP to create a simple blog for myself. I want to have a rating system attached to each post.
I have loaded the ratings which look like this:
Controller
$this->set('ratings', $this->Ratings->find('all'));
I want to generate radio buttons on the view with the ratings. The ratings have the fields value and label. I could use a foreach and loop around the ratings but I am wondering if the radio button helper in the form class can take a model object and generate the radio buttons?
I hope you understand what I mean.
You can pass an options attribute to the form helper.
<?=$this->Form->input('rating', array('type' => 'radio', 'options' => range(1, 10)))?>
$options = array(
'0' => 'Male',
'1' => 'FeMale'
);
$attributes = array(
'legend' => false,
'value' => 0
);
echo $this->Form->radio('type', $options, $attributes);

Categories