Form Helper: Any other benefit?

坚强是说给别人听的谎言 提交于 2019-12-22 11:31:59

问题


I am new to codeigniter and I have just created a test login system. Chuffed...

I would like to make a form for this login system as I have just been passing the username/password directly for testing.

I am trying to work out the benefits of using the form helper. It helps me contsruct a form easily but can I not do this in just pure html? Is there another benefit of the form helper - I am just talking about the part that creates the form and not the validation - I am guessing I can use the validation regardless of how the form was created?

Thanks all


回答1:


The form helper exists to give you programmatic-control over your form. Yes, you could write it in HTML, but then you'll find yourself inserting PHP into your HTML when you want to auto-populate fields that need to persist between submissions and other similar stuff that is common to forms. Using this helper will keep your logic in the controller, and out of your views as much as possible.

By the way, good choice learning MVC!




回答2:


check out this explanation of why you should use the form/html helpers. he makes some good points

http://philsturgeon.co.uk/news/2009/12/Why-CodeIgniter-HTML-helper-functions-rock




回答3:


These are the functions from Form_Helper that I always use in view file of my projects:

  • form_open
  • form_close
  • form_dropdown
  • form_checkbox
  • form_radio
  • set_value

For other field type, such as <input type="text" />, I leave it as is, since the view file is a direct translation from a HTML file provided by a HTML/CSS designer in my team, or my client provide it. I kept the change minimal, and only use set_value in a value="" attribute of those form fields.

As it named helper, it is there to help you. It will make the complex function to select default value in select element easier. Your view file will be a lot cleaner, so it will be easier to read it and search for the bug source.

Form_validation have no connection with form_helper. Validation is run in controller, form_helper is in view. Validation is to filter the value before you process and put it in your database. Form_helper is to make your task to display form field and display or select default value easier. Bot can be used together, just one of it, or neither. It's all up to you.



来源:https://stackoverflow.com/questions/2241571/form-helper-any-other-benefit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!