In the creation validation of a post-use entry this request:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostStoreRequest extends FormRequest
{
/**
* 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()
{
$rules = [
'name' => 'required',
'slug' => 'required|unique:posts,slug',
'user_id' => 'required|integer',
'category_id' => 'required|integer',
'tags' => 'required|array',
'body' => 'required',
'status' => 'required|in:DRAFT,PUBLISHED',
];
if($this->get('file'))
$rules = array_merge($rules, ['file' => 'mimes:jpg,jpeg,png']);
if($this->get('file2'))
$rules = array_merge($rules, ['file2' => 'mimes:pdf|max:2048']);
return $rules;
}
}
The validation when I upload a file, which would be file for images and file2 for pdfs does not work, it lets me upload basically any type of files without validating what type they are. Those fields are nullable, so the if in both. The rest of the validations works perfectly.
Edit1: The version of Laravel that I use is 5.5
I enclose the form that I use to create and edit the posts:
{{ Form::hidden('user_id', auth()->user()->id) }}
<div class="form-group">
{{ Form::label('category_id', 'Categorias') }}
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('name', 'Nombre de la etiqueta') }}
{{ Form::text('name', null, ['class' => 'form-control', 'id' => 'name']) }}
</div>
<div class="form-group">
{{ Form::label('slug', 'URL amigable') }}
{{ Form::text('slug', null, ['class' => 'form-control', 'id' => 'slug']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Imagen') }}
{{ Form::file('file') }}
<div class="form-group">
{{ Form::label('file2', 'Notas') }}
{{ Form::file('file2') }}
</div>
<div class="form-group">
{{ Form::label('status', 'Estado') }}
<label >
{{ Form::radio('status', 'PUBLISHED') }} Publicado
</label>
<label >
{{ Form::radio('status', 'DRAFT') }} Borrador
</label>
</div>
<div class="form-group">
{{ Form::label('tags', 'Etiquetas') }}
<div>
@foreach($tags as $tag)
<label>
{{ Form::checkbox('tags[]', $tag->id) }} {{ $tag->name }}
</label>
@endforeach
</div>
</div>
<div class="form-group">
{{ Form::label('excerpt', 'Extracto') }}
{{ Form::textarea('excerpt', null, ['class' => 'form-control', 'rows' => '2']) }}
</div>
<div class="form-group">
{{ Form::label('body', 'Descripcion') }}
{{ Form::textarea('body', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::submit('Guardar', ['class' => 'btn btn-sm btn-primary']) }}
</div>
@section('scripts')
<script src="{{ asset('vendor/stringToSlug/jquery.stringToSlug.min.js') }}">
</script>
<script src="{{ asset('vendor/stringToSlug/jquery.stringToSlug.js') }}">
</script>
<script src="{{ asset('vendor/ckeditor/ckeditor.js') }}"></script>
<script>
$(document).ready(function(){
$("#name, #slug").stringToSlug({
callback: function(text){
$('#slug').val(text);
}
});
});
CKEDITOR.config.height = 400;
CKEDITOR.config.width = 'auto';
CKEDITOR.replace('body');
</script>
@endsection