Strong Like Bull
Strong Like Bull

Reputation: 11317

How do I display a form using CakePHP that has radio buttons?

I am some what new to CakePHP and wanted some assistance in setting up a form in a CakePHP View.

I have Array in the following form:

Array ( [1] => B002I0HJZO [2] => B002I0HJzz [3] => B002I0HJccccccccc )

I want a form with 3 radio buttons (in this case) that goes to a custom method called test

Upvotes: 0

Views: 714

Answers (1)

Francois Deschenes
Francois Deschenes

Reputation: 24989

You can do that with the FormHelper class.

Example:

<?php
$options = array(
  1 => 'B002I0HJZO',
  2 => 'B002I0HJzz',
  3 => 'B002I0HJccccccccc',
);

echo $this->Form->input('option_id', array('options' => $options, 'type' => 'radio'));

The key is to specify the "type". CakePHP usually defaults to a select element by default.

The example above doesn't use the "automagic" feature of CakePHP. If you retrieve the options in your controller using a find('list') and the array is passed to the view in the plural form of the field name without the "_id" suffix (e.g. if the field is "option_id", you should do $this->set('options', $this->Option->find('list'); assuming that "Option" is the model name), then you shouldn't need to specify the "options", just the "type"

To answer the second part of your question, to post to a different action (e.g. "test"), you need to specify the action when creating the form.

Example:

<?php
$this->Form->create('Product', array('action' => 'test'));

For more information, read the documentation

Upvotes: 2

Related Questions