We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model user friendly column name

Hello, I think it's a simple question, but it is related to Phalcon 2.0.* features.

I don't want to implement something being that something already exists. My models have validation implemented, and I want to add user friendly names for columns. e.g:

I have the columns name, description, some_other. Normally the user sees an error like this "The field some_other is required.". Instead of that, I want to set a user friendly name, resulting on a message like this one for the user: "The field Some Other is required".

Later I want to use that names to create form fields. The preferred way is setting the user friendly names together with column definitions on Model class, this way the MVC will keep simple to understand for my project.

The good way to access the field user friendly name would be:

$model = new PostModel();

//Not existent way to access, but desired way DONT COPY AND PASTE
$columnUserFriendlyName = $model->userFriendly["some_other"];

As$userFriendly is an imaginary property for Model, this should be defined somewhere inside the model Class, might be on initialize()

There's an (ALREADY EXISTENT) way to do this on Phalcon 2.0.9 version ?

Thankyou very much !



221
Accepted
answer

You can override getMessage() method and use some i18ln for different language if it's need. Look here https://docs.phalcon.io/en/latest/reference/models.html#validation-messages

Hey Hodes,

https://github.com/phalcon/cphalcon/blob/2.1.x/CHANGELOG.md check the changelog from 2.0.9 there is a class Phalcon\Text::humanize. I think that is what you are searching for

cheer pat

Thankyou guys,

Wow @patrickacher, that's a really nice TIP. I will think about replacing model validation. Do you know if the model willl still validates before save?

@alexcore I didn't realized that I could use i18n for the purpuse I proposed on this post. This will enable even if I want to change the app language. That's why I will accept the answer.

cheer Hodes

Yes, Phalcon fire first validation like beforeValidationOnSave, beforeValidationOnCreate, beforeValidationOnUpdate methods.

Thankyou guys,

Wow @patrickacher, that's a really nice TIP. I will think about replacing model validation. Do you know if the model willl still validates before save?

@alexcore I didn't realized that I could use i18n for the purpuse I proposed on this post. This will enable even if I want to change the app language. That's why I will accept the answer.

cheer Hodes