我是laravel新手, 我使用laravel 5.6建立了一个发送电子邮件的联系表单,其中包含一些字段和文件附件。
该表单正在运行,并且它发送所有字段,但是仅当我将附加输入留空时,才会引发错误。
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function isValid() on null
我需要file字段为空
'file'=>'有时'
控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ContactFormRequest;
class ContactController extends Controller
{
public function show()
{
return view('contact.show');
}
public function send(ContactFormRequest $request)
{
// upload photo file
$filePath = $this->upload($request->file('file'));
$fullName = $request->get('firstname');
\Mail::send('emails.contact', array(
'fullName' => $request->get('firstname'),
'email' => $request->get('email'),
'body' => $request->get('message'),
'file' => $filePath,
'company' =>$request->get('company'),
'insta' =>$request->get('insta'),
'web' =>$request->get('web'),
'description' =>$request->get('description'),
'phone' =>$request->get('phone'),
), function($message)
{
$message->from('my@mail.cl');
$message->to('my@mail.cl', 'Nombre')->subject('Tienes un nuevo registro de !');
}
);
return \Redirect::route('contact_show', array('locale' => \Lang::getLocale()))
->with('message', 'Gracias por inscribirte!\' nos pondremos en contacto contigo lo antes posible!');
}
protected function upload($file)
{
if ($file->isValid()) {
$fileName = (new \DateTime())->format('d.m.Y-hsi').'.'.$file->guessExtension();
$file->move(storage_path() . '/uploads', $fileName);
return storage_path() . '/uploads/' . $fileName;
} else {
return \Redirect::route('contact_show')
->with('message', 'El Archivo no es valido!');
}
}
}
请求:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class ContactFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'firstname' => 'required',
'email' => 'required|email',
'message' => 'sometimes',
'company' => 'sometimes',
'phone' => 'required',
'insta' => 'sometimes',
'web' => 'sometimes',
'description' => 'required',
'file' => 'sometimes'
];
}
}
表格:
@extends('base')
@section('content')
@if (Session::has('message'))
<div class="alert alert-info">
{{ Session::get('message') }}
</div>
@endif
<h1>{{ trans('contact.contact_us') }}</h1>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
{!! Form::open(array('route' => 'contact_send', 'class' => 'form', 'files' => true)) !!}
<div class="form-group">
{!! Form::label(Lang::get('first name')) !!}
{!! Form::text('firstname', null, array('required', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('Empresa')) !!}
{!! Form::text('company', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('insta')) !!}
{!! Form::text('insta', null, array( 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('web')) !!}
{!! Form::text('web', null, array( 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('email')) !!}
{!! Form::text('email', null, array('required', 'class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('phone')) !!}
{!! Form::text('phone', null, array('required','class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('description')) !!}
{!! Form::text('description', null, array('required','class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('photo')) !!}
{!! Form::file('file', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::label(Lang::get('message')) !!}
{!! Form::textarea('message', null, array('class' => 'form-control')) !!}
</div>
<div class="form-group">
{!! Form::submit(Lang::get('send'), array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
@endsection
答案 0 :(得分:0)
您错误地使用了sometimes
验证规则。
根据文档,如果要在传递的数组中存在值时应用其他验证规则,则可以使用该验证规则。
https://laravel.com/docs/5.6/validation#conditionally-adding-rules
您的验证数组应如下所示:
return [
'firstname' => 'required|string',
'email' => 'required|email',
'message' => 'string',
'company' => 'string',
'phone' => 'required',
'insta' => 'string',
'web' => 'url',
'description' => 'required|string',
'file' => 'file'
];
此外,我建议您不要使用表单构建器。即使它仍在Laravel中,也已经从laravel文档中删除了一段时间,构建html也一样容易。
我不确定这是否可以直接解决您的问题,但希望可以解决这个问题:)。
答案 1 :(得分:0)
第一件事是将验证规则从'file' => 'sometimes'
改成'file' => 'file'
,然后在检查function upload()
之前对$file->isValid()
进行一些更改,您必须检查它是否为空或为空
is_null($file)
!isset($file)
有关更多详细信息,这里是链接check if variable empty
答案 2 :(得分:0)
我解决了在表单文件为空的情况下发送默认img的问题
我的控制器
public function upload($file)
{
if (is_null($file)) {
return storage_path() . '/uploads/' . '1.png';
}
else {
if ($file->isValid()) {
$fileName = (new \DateTime())->format('d.m.Y-hsi').'.'.$file->guessExtension();
$file->move(storage_path() . '/uploads', $fileName);
return storage_path() . '/uploads/' . $fileName;
} else {
return \Redirect::route('contact_show')
->with('message', 'El Archivo no es valido!');
}
}
}
我的请求
return [
'firstname' => 'required',
'email' => 'required|email',
'message' => 'nullable',
'company' => 'nullable',
'phone' => 'required',
'insta' => 'nullable',
'web' => 'nullable',
'description' => 'required',
'file' => 'mimes:jpeg,bmp,png,mp4s,doc,dot,pdf,docx,cgm,gif,jpg,jpe,svg,txt,mp4,mov',
];
mime是用于我的文件上传的验证人