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


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

Ramy MohsenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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
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...
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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,
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:

Also, consider becoming active in OWASP.
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

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

From novice to tech pro — start learning today.