Yii2, I want to make the input names to be not the same as db fields

Hello,

In yii2, I want to make DB fields not the same as input fields, but the auto generated code from Gii makes them the same,

When I manually change name of the input fields, It shows an error, How can I map the changed input names to the corresponding fields

Regards,
Ramy MohsenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Loganathan NatarajanLAMP DeveloperCommented:
Can you post the form fields here?  Usually Gii will generate as per the db fields with $model appended  like http://www.yiiframework.com/doc-2.0/guide-input-forms.html
0
Ray PaseurCommented:
No points for this because it's not an answer to your question, but I'm moved to ask WTF?  This sounds theoretically possible using some kind of facade or translator, but given the context of Yii2, it sounds like a way to add complexity without adding functionality, at the potential risk of losing the benefits of active record design.  If this were my application I would seriously reconsider...
1
Dave BaldwinFixer of ProblemsCommented:
I wouldn't do that either.  Rather I should say, I normally make them the same on purpose so I know how the database fields correlate with the input fields.
1
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Ramy MohsenAuthor Commented:
Thank you all,

My cause for that is that I don't want anyone to be able to guess names of the fields in the DB (better security ),  so just I make the db  fields to be prefixed.

For example:  DB fields may be named:  XYZ_username, XYZ_password, while the input names named : username, password

Thanks in advance,
0
Ray PaseurCommented:
Munging your database column names is not a security measure.  

You've got the advice from the experience of the top Experts, so I will sign off.  If you want to learn about security issues that really matter, start here:
http://php.net/manual/en/security.php
http://www.yiiframework.com/doc-2.0/yii-base-security.html

Also, consider becoming active in OWASP.
https://www.owasp.org/index.php/Main_Page
1
Loganathan NatarajanLAMP DeveloperCommented:
When you use yii / active form fields way of CRUD generation using Gii, you must follow the standards.  More details her, http://www.yiiframework.com/doc-2.0/guide-start-forms.html#

Because when you defined form fields with model, it takes care validation and default active record methods to process with db.

For example,

<?= $form->field($model, 'username')->textInput() ?> 

Open in new window

will generate like this
<input class="form-control" id="loginform-username" name="LoginForm[username]" placeholder="Username" maxlength="35" type="text" >

Open in new window


Incase you want to avoid model way operation and want to do your own custom form design with  validation ,  you can follow this way.

Custom html form helper, http://www.yiiframework.com/doc-2.0/yii-helpers-html.html
<?= Html::textInput("name", $value) ?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.