Laravel:使用 Input::old 时不会重新填充 textarea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23327845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: textarea does not refill when using Input::old
提问by Soviet Patriot CCCP
I am experiencing that only the Input Textfields respond as expected when I write the code to repopulate a form (when errors were found for example) or when from a table row I click in the Edit button and I go to an editable form. The field for a textarea is not repopulated so it comes up empty, therefore, if I save it, I would delete the content of the textarea. (I know I am asking a succession of questions lately, the reason is that I have basically finished my application and I left for the end the minor things I could not solve, so my apologies for that).
我遇到的是,当我编写代码以重新填充表单时(例如,当发现错误时)或当我从表行中单击“编辑”按钮并转到可编辑表单时,只有输入文本字段会按预期响应。textarea 的字段不会重新填充,因此它变为空,因此,如果我保存它,我将删除 textarea 的内容。(我知道我最近在问一连串的问题,原因是我基本上完成了我的申请,把我无法解决的小事留到最后,所以我对此表示歉意)。
here is an example of what I am saying:
这是我所说的一个例子:
This WORKS for input textfield:
这适用于输入文本字段:
WORKS
作品
<div class="col-md-4">
<label for="relato">Charges</label>
<input type="text" name="expenses" maxlength ="30" class="form-control"
value = "{{ $user->expenses }}"/>
</div>
That is, the $user->expenses fills the textfield of the form that comes up when clicking the Edit button of a table row.
也就是说,$user->expenses 填充了单击表格行的 Edit 按钮时出现的表单的文本字段。
However, that does not work for a textarea field:
但是,这不适用于 textarea 字段:
<div class="row">
<label for="relato">Description</label>
<textarea name ="message" id="message" rows="5" cols="100" value = "{{ $user->message }} class="form-control"
</textarea>
</div>
See? that part $user->message will not pass the content to the textarea of a form.
看?这部分 $user->message 不会将内容传递给表单的 textarea。
Similarly: with Input::old
类似地:使用 Input::old
Works for an Input textfield
适用于输入文本字段
WORKS
作品
Email: <input type="text" class="form-control" name="email" {{ (Input::old('email')) ?' value ="' . e(Input::old('email')). '"' : '' }}>
DOES NOT WORK FOR TEXTAREA
不适用于文本区域
<div class="form-group">
<label for="relato">Une petite description</label>
<textarea id="message" name = "content" rows="10" cols="50" onKeyPress class="form-control" {{ (Input::old('content')) ?' value ="' . e(Input::old('content')). '"' : '' }}
">
</textarea>{{ $errors->first('content')}}
</div>
And the controller is also trying to refill the form by sending ->withInput
并且控制器还尝试通过发送 ->withInput 来重新填充表单
if($validator->fails()){
return Redirect::route('usersgetformtopostads')
->withErrors($validator)
->withInput();
}
but, as I said, it only works for textfields. Does not repopulate me a select list or a textrarea
但是,正如我所说,它仅适用于文本字段。不重新填充选择列表或文本区域
By the way, I have looked a related question here where the author says he could not repopulate a File field and he was told that "you cant" and he gave that as a correct answer, however, I have been able to repopulate Files uploaded, not having any problem with that.
顺便说一句,我在这里查看了一个相关的问题,作者说他无法重新填充文件字段,并被告知“你不能”,他给出了正确答案,但是,我已经能够重新填充上传的文件,没有任何问题。
回答by Marwelln
textarea
does not have a value
attribute. Values in textarea
should be inside <textarea></textarea>
, so in your case:
textarea
没有value
属性。值 intextarea
应该在 inside <textarea></textarea>
,所以在你的情况下:
<textarea id="message" name = "content" rows="10" cols="50" onKeyPress class="form-control">
{{{ Input::old('content') }}}
</textarea>
回答by Ngaiza
Just figured it out, put the old value in between the brackets as below
刚刚想通了,将旧值放在括号之间,如下所示
<textarea name="message">{{ old('message') }}</textarea>
回答by felipebla
This is another way to do the same but with the Forms component from laravel:
这是使用 laravel 的 Forms 组件执行相同操作的另一种方法:
{{ Form::textarea('content',Input::old('content'),array('id' => 'message')) }}
回答by ssi-anik
I'd like to add one thing, if you use Form Class for the form and elements then you don't need to explicitly right the Input::old('element-name') to retrieve the value of the previous form submission.
Just use {{ Form::text('name', null, array('id'=>'name', 'class' => 'class-to-apply')) }}
And you're good to go.
Here, null value for the text field will be null for the first time, and if you redirect back this page with input then this will automatically grab the value.
我想补充一点,如果您对表单和元素使用 Form Class,那么您不需要明确地修改 Input::old('element-name') 来检索先前表单提交的值。
只需使用{{ Form::text('name', null, array('id'=>'name', 'class' => 'class-to-apply')) }}
您就可以开始了。
在这里,文本字段的空值第一次为空,如果你用输入重定向回这个页面,那么这将自动获取该值。
回答by Bhawani singh
Sorry for late reply
抱歉回复晚了
{{Form::textarea('mobile_message', isset($notifications -> mobile_message) ? $notifications -> mobile_message : null, 'id'=> 'mob_id','class' => 'form-control'))}}