It seems my validation isnt quite correct, so Im getting this error message while Im trying to store fees into my database.
I have previosuly checked related questions with but it didnt quite help. I created FeeValidator that extends Validator with this array:
<?php namespace \Events\Services\Validations;
use \Events\Services\Validations\AbstractValidator as Validator;
class FeeValidator extends Validator {
protected $rules = array(
'title' => 'required',
'price' => 'required|numeric',
'quantity' => 'integer',
'valid_from' => 'date',
'valid_to' => 'date',
'ticket_limit' => 'integer',
'url_redirect' => 'string',
);
}
Then I have a FeesRepository with this store() function in the FeesRepository class:
<?php namespace \Events\Repositories;
use \Events\Models\Fee as Fees;
use \Events\Models\Event as Event;
use \Events\Repositories\Contracts\FeesRepositoryInterface;
use \Events\Services\Validations\FeeValidator;
class FeeRepository implements FeesRepositoryInterface {
protected $theme;
protected $feeValidator;
public function store($eventId) {
$validation = $this->feeValidator->with(\Input::all());
if ($validation->passes()) {
$fee = new Fees;
$fee->event_id = $eventId;
$fee->title = \Input::get('title');
$fee->price = \Input::get('price');
$fee->quantity = \Input::get('quantity') == "" ? 1 : \Input::get('quantity');
$fee->discount = \Input::get('discount');
$fee->valid_from = \Input::get('valid_from');
$fee->valid_to = \Input::get('valid_to');
$fee->coupon = \Input::get('coupon') == "" ? null : \Input::get('coupon');
$fee->tickets_limit = \Input::get('ticket_limit');
$fee->url_redirect = \Input::get('url_redirect');
$fee->save();
$id = $fee->event->id;
if ( $fee->event->eventable_type == '\Events\Models\Training' )
{
return \Redirect::route('admin.training.edit' , array($id));
}
elseif( $fee->event->eventable_type == '\Events\Models\Meetup' ) {
return \Redirect::route('admin.meetup.edit' , array($id));
}
else
{
return \Redirect::route('admin.conference.edit' , array($id));
}
}
else {
return \Redirect::back()->withInput()->withErrors($this->feeValidator->errors());
}
}
}
Finally you can check my store() function in the FeesController:
<?php namespace \Events\Controllers;
use \Events\Repositories\FeeRepository as Fee;
use \Events\Models\Fee as Fees;use \Events\Services\Validations\FeeValidator;
class FeesController extends \BaseController {
protected $fee;
protected $feeValidator;
public function __construct(Fee $fee , FeeValidator $feeValidator)
{
$this->fee = $fee;
$this->feeValidator = $feeValidator;
}
public function store($eventId) {
return $this->fee->store($eventId);
}
}
Im wondering what is the issue. Can someone give me a hint! Thanks!
For your validation you can use default validation which already build in Laravel https://laravel.com/docs/5.2/validation to display errors you can use JS or simply in your form ex :
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>@endif
</div>
</div>
EDIT : hey you have simple validation so you have a easy way to use validation like this but your way is correct but why can't you try easy way ? This for Laravel 4.2 easy way to use validation example
Route::post('register', function()
{
$rules = array(...);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::to('register')->withErrors($validator);
}
});