简体   繁体   English

Laravel 4使用模型填充表单字段,但首先编辑字段

[英]Laravel 4 Populate Form fields with a Model but edit the field first

I am working with PHP and Laravel 4. Using the Form method to populate an edit form with a Model like below... 我正在使用PHP和Laravel 4.使用Form方法用下面的模型填充编辑表单...

Form::model($timecard, array('route' => array("admin/timecard/edit", $id)))

My problem is, some of the text fields get populated with DateTime values from the Database and I need to be able to run some code on these certain fields before it populates the Form field. 我的问题是,一些文本字段用数据库中的DateTime值填充,我需要能够在填充表单字段之前在这些特定字段上运行一些代码。

Any ideas how to do that or if it's possible to do that while still using the Model to auto-fill the Form fields? 任何想法如何做或如果仍然使用模型自动填充表单字段可以这样做?

For example this form field below gets filed with a GET value, otherwise it gets field with the Data from the Database for column clock_in_datetime however I would like to run a PHP function on this field before it fills the form so that I can apply TimeZone or other formatting to it... 例如,下面的这个表单字段被归档为GET值,否则它获取了来自数据库的数据字段clock_in_datetime但是我想在填充表单之前在该字段上运行PHP函数,以便我可以应用TimeZone或其他格式化...

{{ Form::text("clock_in_datetime", Input::get("clock_in_datetime"), array(
    "placeholder" => "2013-09-04 14:22:35",
    'class' => 'form-control'
)) }}

I believe you can do the following: 我相信你可以做到以下几点:

{{ Form::text("clock_in_datetime", yourFormattingFunction(Form::getValueAttribute("clock_in_datetime")), array(
    "placeholder" => "2013-09-04 14:22:35",
    'class' => 'form-control'
)) }}

Form::getValueAttribute() is Laravel's way of deciding which value to use (previous Input, Session or Model). Form::getValueAttribute()是Laravel决定使用哪个值(以前的输入,会话或模型)的方法。 So you can apply your formatting function to the output of this function. 因此,您可以将格式化函数应用于此函数的输出。

http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#751-773 http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html#751-773

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM