I'm new in laravel, im using laravel 5.6 to build a contact form who send email, with some fields and file attachmend .
The Form is working, and it send all the fields, but only when i left the attach input empty, throws a error.
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function isValid() on null
I need the file field to be able to be empty
'file' => 'sometimes'
Controller:
<?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!');
}
}
}
Request:
<?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'
];
}
}
Form:
@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