user1904159
user1904159

Reputation: 47

How to rename fields (of models/tabels) in models in CakePHP?

To create an input field corresponding to a model one can use this:

cakephp $this->Form->input('Model.field');

To rename it, one can use

cakephp $this->Form->input('Model.field', array('label' => __('new field name')));

But consider I have a huge application and I don't want to rename each field over and over again. This question is asked a lot of times and the always answer this beginner question with the solution I gave. But there has to be a more elegant way in CakePHP ... I would guess that one can build a workaround with Translate Behavior. Is this the best way to go?

Upvotes: 2

Views: 1221

Answers (2)

Leah Collins
Leah Collins

Reputation: 637

The labels of the input elements depends on field names in your database table's and model field's. So lets assume you have field username in your User's table and User Model, Then..

echo $this->Form->input('User.username');

Will output

<input type="text" label="Username" ... />

So change the fields in user table and model to get desired labels for your form elements.

Upvotes: 0

Chuck Burgess
Chuck Burgess

Reputation: 11574

The way you indicate is the way it is done in CakePHP. The field name MUST correspond to the name in the model. If you do not like the name, you can always change the name in the model to represent what you want it to show on the form. Otherwise, you must use the label field as indicated.

I guess there could be another way. You could override the form helper to do the translation for you. But then you will be managing a large array of form names to dynamically insert the label based on the name. This seems like a lot of extra work just to change a label though.

Upvotes: 1

Related Questions