开发者

CakePHP: Field label in model

Are there any approaches t开发者_开发知识库o set in model field label? I dont want use 'label' property in form helper.


Please make sure I understand this correctly, you want to set a field label in the model, rather than using the form helper?

That violates basic MVC architecture. While Cake is flexible on some things, I don't think this is a possible option. I also don't see why you'd want to do it -- is there some reason that you don't want to use the label property in the form helper?

The basic issue is that the label for a form is part of the presentation layer, while the model represents the data. As such, it isn't possible (and I can't think of a situation where it'd make sense...) to assign a label to a data field which would then be used whenever that field is output.

If I misunderstood your question, please clarify.


What I do is setting up a convention in my models. I added a public attribute called "fieldLabels" to all the models, to assign default text labels for generic forms.

Example:

class MyModel extends AppModel {
    // ...
    public $fieldLabels = array(
        'username' => 'User name',
        'email' => 'e-mail address',
        'phone' => 'Phone No.',
    );
// ....
}

Then I pass around the labels to the view and use the extra parameter for the input, as sibidiba said:

echo $this->Form->input('title', array(
                        'label' => $fieldLabels['title'] . ': ',
                  ));

In case I need special labels, I'll handle each case as an exception. Of course, if you want internationalization, that's a whole different topic.


Do you want to set the label's value? This is done in the view, but of course the value can originate from the controller/model. Like this:

echo $this->Form->input('title', array(
                    'label' => $titleLabel,
                    ));

you can also disable the label element:

echo $this->Form->input('title', array(
                    'label' => null,
                    ));


Not 100% sure, but I think you might be looking for Model::displayField


i suggest u directly use the helper u want.. bcos $form-input() creates div .. labels..

i personally had to weed out this on each line

with the direct helper like

$form->text() $form->textarea() $form->select()

u can keep the code much cleaner.

Note: $form->input saves time when used right...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜