I'm currently using CakePHP's "automagic" field elements for my CRUD forms.
By this I mean I'm using theecho $form->input('fieldname', $options);
method to generate everything.
This chooses the correct element type, and wraps everything into a div with a <label> elem开发者_运维百科ent.
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!
DanielYou 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"
.
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]);
}
}
?>
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
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.
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!
echo $form->input('something', array('div'=>false, 'label'=>false));
精彩评论