多重选择编辑表单选择的值 [英] Multiple select edit form selected values

查看:67
本文介绍了多重选择编辑表单选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 4中遇到问题时,可以通过联系"模型编辑表单来获取所有字段的当前值,但要与另一个模型公司"建立关系的多重选择除外.这是多对多的关系.我正在获取公司列表,但是即使存在关系也没有选择.

Struggling with an issue in Laravel 4, in a "contact" model edit form, I can get all fields current values except those from the multiple select which is to establish a relation with another model "company". It's a many-to-many relationship. I'm getting the list of companies, but none are selected even if a relation exists.

这是我的编辑表单:

{{ Form::model($contact, array('route' => array('crm.contacts.update', $contact->id), 'id' => 'edit-contact')) }}
        <div class="control-group">
            {{ Form::label('first_name', 'First Name', array( 'class' => 'control-label' )) }}
            {{ Form::text('first_name') }}
        </div>
        <div class="control-group">
            {{ Form::label('last_name', 'Last Name', array( 'class' => 'control-label' )) }}
            {{ Form::text('last_name') }}
        </div>
        <div class="control-group">
            {{ Form::label('email', 'Company Email', array( 'class' => 'control-label' )) }}
            {{ Form::text('email') }}
        </div>
        <div class="control-group">
            {{ Form::label('company_ids', 'Company', array( 'class' => 'control-label' )) }}
            {{ Form::select('company_ids[]', $companies, array('',''), array('multiple'), Input::old('company_ids[]')) }}
        </div>
{{ Form::close() }}

我的控制器:

public function edit($id)
{
    $contact = Contact::find($id);
    $company_options = Company::lists('name', 'id');
    return View::make('crm.contacts.edit')
        ->with('contact', $contact)
        ->with('companies', $company_options);;
}

关于如何使多个选择字段预先填充现有值的任何想法?

Any ideas on how to have the multiple select field pre-filled with existing values?

谢谢

推荐答案

Laravel在默认情况下不支持多选字段您需要使用Form :: macro

Laravel does not support multi-select fields by default you need to use a Form::macro

@Itrulia的以下示例是正确的,您可以执行以下操作:

The example below by @Itrulia was correct, you can simply do:

$users = array(
    1 => 'joe',
    2 => 'bob',
    3 => 'john',
    4 => 'doe'
);
echo Form::select('members[]', $users, array(1,2), array('multiple' => true));

这篇关于多重选择编辑表单选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆