views:

1413

answers:

3

hi, I'm trying to create a text box using the vales retrieved from the database. But instead of a text box, a select box (drop down box) is created. I do not understand why. I have given the type as text only, then why a select box is being created?

echo $form->input($r['Attribute']['label'], 
                  array('label' => $r['Attribute']['label'].'  * ',
                        'class' => 'required',
                        'id'    => $r['Attribute']['id'],
                        'name'  => $r['Attribute']['label'],
                        'type'  => 'text',
                        'style' => 'width:' . $r['Attribute']['size'] . 'px'));

This is the Attributes table with a few records.

id  form_id  label  type    size  sequence_no  required
2   1        Name   text    200   1            true
3   1        Age    number  200   2            true

The output of $form->input is

<div class="input select">
<label for="4">Name * </label>
<select id="4" class="required" style="width: 200px;" name="data[Name]"> </select>
</div>

instead of

<div class="input text">
<label for="4">Name * </label>
<input id="4" class="required" style="width: 200px;" name="data[Name]"> </input>
</div>

How does the input type get saved as "select" even when I explicitly mention it as "text"?

A: 

could you do a pr($r['Attribute']); above your block of code and paste the output? usually you don't need the name in there or even the type.

Check out these links for more info: http://book.cakephp.org/view/189/Automagic-Form-Elements http://api.cakephp.org/class/form-helper#method-FormHelperinput

jimiyash
array(8) { ["id"]=> string(1) "4" ["label"]=> string(4) "Name" ["type"]=> string(4) "text" ["sequence_no"]=> string(1) "1" ["size"]=> string(3) "200" ["form_id"]=> string(1) "2" ["instructions"]=> string(0) "" ["required"]=> string(4) "true" } v
Angeline Aarthi
Do you have a echo $form->create above? Usually you have a $form->create(<model name goes here>); Otherwise, you usually have to specify the model name in the input like $form->input(Model.fieldname)
jimiyash
A: 

The only thing I could think of is, if you have a field named, say, "name", and you also have a variable in your view named "$names", and this variable is an array, by naming convention Cake may think these belong together.

Looking through the FormHelper, there's actually this piece of code:

$types = array('text', 'checkbox', 'radio', 'select');
if (!isset($options['options']) && in_array($options['type'], $types)) {
    // ... looks for corresponding variable in the view ...
    $options['type'] = 'select';

This seems to be the special case where Cake may change the input type by itself.

Looks like setting 'options' => null in the $form->input() arguments should help.

deceze
it doesn't work :( I expected a lot that it would surely work, coz it seemed so logical.
Angeline Aarthi
Try using the `$form->text()` method then, that shouldn't change.
deceze
+1  A: 

Because the value of Attribute.label is capitalised, CakePHP thinks it is a reference to another model along the lines of a belongsTo relationship, and so it tries to give you the list automatically.

Try replacing:

$form->input($r['Attribute']['label'],

with something like:

$form->input('Attribute.'.$r['Attribute']['id'].'.label',

which should output:

<div class="input text">
<label for="4">Name * </label>
<input id="4" class="required" style="width: 200px;" name="data[Attribute][2][label]" />
</div>

This will give you all the information you need in a structure that CakePHP will recognise.

Edit: Oh, and change the line that has 'id' => $r['Attribute']['id'], because that is just going to generate nonsensical HTML attributes. Something like 'id' => 'Attribute'.$r['Attribute']['id'] should be more useful.

Robert P
Great.. Thank you... It works perfect now..For id, is it 'Attribute'.$r['Attribute']['id'] or 'Attribute.'.$r['Attribute']['id'].'.id'?
Angeline Aarthi
Glad it worked.`'id' => 'Attribute'.$r['Attribute']['id']` should be used, since it is the HTML element ID being generated.
Robert P