【发布时间】:2016-11-22 21:44:51
【问题描述】:
我的验证似乎不太正确,所以我在尝试将费用存储到我的数据库中时收到此错误消息。
我之前已经检查过相关问题,但并没有太大帮助。我创建了 FeeValidator,用这个数组扩展了 Validator:
<?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',
);
}
然后我在 FeesRepository 类中有一个带有这个 store() 函数 的 FeesRepository:
<?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());
}
}
}
最后你可以在 FeesController 中检查我的 store() 函数:
<?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);
}
}
我想知道是什么问题。谁能给我一个提示! 谢谢!
【问题讨论】:
-
您收到什么错误?
-
发布您的错误。
标签: php string validation laravel store