[英]Laravel Option Select - Default Issue
Here is my Select Box, Here All Company will be loaded, 这是我的选择框,这里将加载所有公司,
But i want to display the Particular company as default selected which i have it in session. 但是我希望将特定公司显示为默认选择 ,我在会话中使用它。
Here is my Code 这是我的代码
$sessioncompany = 'ABCcompany'
$comp[''] = 'Company';
@foreach($company_list as $row)
<?php
$comp[$row->CompanyID] = $row->CompanyName;
?>
@endforeach
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID')); }}
What i tried is 我尝试的是
{{ Form::select('CompanyID', $comp, '', array('id' => 'seCompanyID'), array(id=>$sessioncompany)); }}
But i ended with failure 但我以失败告终
What is the mistake i am doing and How can i fix that ? 我正在做的错误是什么?如何解决这个问题?
Note : I want it in the native laravel method 注意:我想在原生laravel方法中使用它
$selectedId = ... // get the id from the session
{{ Form::select('CompanyID', $comp, array($selectedId), array('id' => 'seCompanyID')) }}
The Form::select()
method has the following parameters: Form::select()
方法具有以下参数:
$name
- the name
HTML attribute $name
- name
HTML属性 (array) $list
- the list of options, key-value pairs for actual value => displayed value (array) $list
- 选项列表,实际值的键值对=>显示的值 $selected
- the selected item's value $selected
- 所选项目的值 (array) $options
- any extra HTML attributes, as per other Form/HTML helper methods (array) $options
- 任何额外的HTML属性,与其他Form / HTML帮助器方法一样 So, the third parameter $selected
is the one you want to pass your session value to. 因此,第三个参数
$selected
是您要将会话值传递给的参数。 Copying your example and replacing: 复制您的示例并替换:
{{ Form::select('CompanyID', $comp, Session::get('my_session_var'), array('id' => 'seCompanyID')); }}
Where 'my_session_var'
is the name of the session variable you store the value of the item that should be selected by default. 其中
'my_session_var'
是会话变量的名称,您存储应默认选择的项的值。
The great thing about using Laravel's Form helpers is that you get the 'old' input for free. 使用Laravel的Form助手的好处是你可以免费获得“旧”输入。 For example, if you set a default here and the user changes it, submits the form and there's a validation error.
例如,如果您在此处设置默认值并且用户更改了它,则提交表单并出现验证错误。 When the form is redisplayed (assuming you redirected back with
return Redirect::back()->withInput();
) the value the user selected, rather than your explicit default will be set. 重新显示表单时(假设您重定向并
return Redirect::back()->withInput();
)用户选择的值,而不是显式默认值。
(Thanks to @JarekTkaczyk for putting me right on the old input thing.) (感谢@JarekTkaczyk把我放在旧输入的东西上。)
You are passing wrong parameters. 你传递错误的参数。 max parameters should be 4. Try this:
最大参数应为4.试试这个:
{{ Form::select('CompanyID', array('default' => $sessioncompany)+$comp, 'default') }}
请注意,如果您在带有选择框的表单的页面上,请确保在测试时刷新每个页面而不使用缓存执行整页刷新 - Ctrl + Shift + R
,否则最后选择的值可能会保留选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.