多选的Laravel验证

时间:2015-02-09 20:16:55

标签: php laravel laravel-validation

我正在制作一个多选表单元素,用于更新学校和专业数据透视表school_specialty。问题是,当我只更改多项选择而不是其他输入或textareas时,我无法监听模型事件,因此我无法同步school_specialty表。但是当我填写任何其他输入时,它的工作完美无缺。这是我从刀片中选择的多选:

{{Form::select('specialties[]', $specialties_data, $school->specialties, array('multiple' => 'true', 'id' => 'multi-select'))}}

这是school controller的更新方法:

public function update($id)
{
    $data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id');

    $school = School::find($id);
    $school->name = $data['name'];
    $school->type_id = $data['type_id'];
    $school->description = $data['description'];
    $school->info_specialties = $data['info_specialties'];
    $school->contacts = $data['contacts'];
    $school->cover_photo = Input::file('cover_photo');
    $school->set_specialties = $data['specialties'];
    $school->financing_id = $data['financing_id'];
    $school->set_district_id = $data['district_id'];
    $school->city_id = $data['city_id'];

    try {
        $school->save();
    } catch (ValidationException $errors) {
        return Redirect::route('admin.schools.edit', array($id))
            ->withErrors($errors->getErrors())
            ->withInput();
    }

    return Redirect::route('admin.schools.edit', array($id))
        ->withErrors(array('mainSuccess' => 'School was created.'));
}

这是我的例子school model

<?php

class School extends Eloquent {
    protected $table = 'schools';

    protected $fillable = array('name', 'type_id', 'description', 'city');
    protected $guarded = array('id');
    protected $appends = array('specialties');

    public $set_specialties;
    public $set_district_id;

    protected static function boot()
    {
        parent::boot();

        static::updating(function($model)
        {
            $data = array(
                'name' => $model->name,
                'type_id' => $model->type_id,
                'description' => $model->description,
                'specialties' => $model->set_specialties,
                'city_id' => $model->city_id
            );

            $rules = array(
                'name' => 'required|min:3|max:50',
                'type_id' => 'required|min:1|max:300000',
                'description' => 'required|min:10',
                'specialties' => 'required|array',
                'city_id' => 'required|min:1|max:300000'
            );

            $validator = Validator::make($data, $rules);

            if ($validator->fails()) {
                throw new ValidationException(null, null, null, $validator->messages());
            } else {
                return true;
            }
        });

        static::updated(function($model)
        {
            if ( $model->set_specialties != null )
            {
                $model->specialty()->sync($model->set_specialties);
            }
        });
    }

    public function specialty()
    {
        return $this->belongsToMany('Specialty', 'school_specialty');
    }
}
?>

1 个答案:

答案 0 :(得分:0)

仅更新学校专业时,学校模型活动不会被触发,因为学校模式保持不变。

我认为最简单,最优雅的解决方案是触摸学校模型实例。这将修改School对象的updated_at字段,从而触发模型事件。 为此,在try / catch块之前添加以下行:

    data frame A.str1="gmaps","facebook","gmail","linkedin"
    data frame B.m1  ="gmaps","oracle","gmail","intel"
    data frame B.m2  ="Google","xyz","Google","nvidia"

此外,验证不应在模型观察者中处理。在此处检查表单请求验证:https://laravel.com/docs/5.6/validation#form-request-validation