views:

289

answers:

6

I'm currently using CakePHP's "automagic" field elements for my CRUD forms.
By this I mean I'm using the

echo $form->input('fieldname', $options);

method to generate everything.

This chooses the correct element type, and wraps everything into a div with a <label> element.

Now I have some fields that are not editable, but i'd like for them to show (so there wouldn't really be a label, just a span, and instead of the <input> control, there'd be simply some text, or a span.
I also need to be able to control the contents of the "field value" arbitrarily.

Is there a way to do this with $form->input?
I know I can simply generate the markup for all this, but it'd look pretty ugly, and it's very repetitive.

Thanks!
Daniel

+1  A: 

What about:

$html->tag("span", $form->data["fieldname"]);

If that's too ugly, you could write your own helper:

<?php
class WhateverHelper extends AppHelper {
    var $helpers = array('Html');
    function whatever($fieldname) {
        return $this->Html->tag("span", $form->data[$fieldname]);
    }
}
?>
mgroves
That's not too ugly, but it doesn't do what I want, which is precisely to have Cake create the <div> with the <label>, etc.
Daniel Magliola
What's the point of the label tag if there's no text box, though?
mgroves
I see what you're saying, and I share the feeling, from a purist point of view. In my particular case, the point is that it all looks the same with the same style and it's easier to maintain.
Daniel Magliola
I wouldn't say "purist", more like following the purpose of the label tag, but hey it's your markup :)
mgroves
+2  A: 

You can always leave them as inputs, but take away the editing option. Adding 'readonly' => true to the input params should add to the input something like this: readonly="readonly".

PawelMysior
yes, thought about this, but it won't let me control the value that is shown, and it sounds a bit dangerous (since someone can tamper the HTML and edit the field, and I want to take advantage of the "auto-magic anti-tampering feature".
Daniel Magliola
A: 

In the end, I ended up modifying CakePHP :-(

I just added the option to do this:

echo $form->input('customer_id', array('type' => 'output', 'value' => 'xxxx' ));

It was relatively trivial to do so, although i would much rather not touch the core files.

I added the details on how to do it to their bug system, to see if they'll consider adding it in future releases.

Hope this helps someone!

Daniel Magliola
+1  A: 

You don't need to (not should you ever!) touch the core files.

You can create your own FormHelper based on the built in one and override the input method.

Here's how I override the HtmlHelper's sort method to add sort direction classes for paginated tables:

http://richardathome.com/blog/cakephp-extend-paginatorhelper-indicate-sort-field-and-direction

RichardAtHome
+1  A: 

You'd be better off doing:

$form->data["fieldname"]

and surrounding it with any markup you require. If you need to, add your own helper as Richard has suggested.

But don't EVER change the core. It'll only give you headaches further down the line.

Leo
A: 

hey, what are you doing?

echo $form->input('something', array('div'=>false, 'label'=>false));
Aziz