我需要使用setter方法在Validator类中设置$rules
数组。
我需要使用setter设置$ rules规则,因为规则存储在数据库中。我需要使用Eloquent来查询数据库,以确定规则的外观。
我扩展了Validator类并添加了一个名为setCustomRules()的方法。我在这个方法中注入了一个类,这个允许我使用Eloquent读取数据库来确定规则。
但是,当Laravel尝试验证时,我如何强制启动setCustomRules()方法?必须首先执行此方法,以确保在验证发生之前设置规则。
这就是我所做的
<?php namespace Vend\Surveys\Validator\SurveyAnswers;
use Cartalyst\Support\Validator;
use Vend\Surveys\Repositories\SurveyAnswers\SurveyAnswerDefinedRepository;
class SurveyAnswerDefinedValidator extends Validator implements SurveyAnswerDefinedValidatorInterface {
/**
* {@inheritDoc}
*/
protected $rules;
/**
* {@inheritDoc}
*/
public function onUpdate()
{
}
public function setCustomRules(SurveyAnswerDefinedRepository $rules)
{
foreach($rules as $rule){
$this->rules[$rule->name] = $rule->spec;
}
//this should display the rules that will be used to validate the form
dd($this->rules);
}
}
被修改 根据The Alpha的回答,我创建了一个事件处理程序,可以让我创建规则。但不知道如何告诉Laravel现在验证。
这是我的EventHandler类
<?php namespace vend\Surveys\Handlers\SurveyAnswers;
use Illuminate\Events\Dispatcher;
use vend\Surveys\Models\SurveyAnswerDefined;
use Cartalyst\Support\Handlers\EventHandler as BaseEventHandler;
use vend\Surveys\Repositories\SurveyQuestions\SurveyQuestionsRepositoryInterface;
class SurveyAnswerDefinedEventHandler extends BaseEventHandler implements SurveyAnswerDefinedEventHandlerInterface {
protected $rules = [];
/**
* {@inheritDoc}
*/
public function subscribe(Dispatcher $dispatcher)
{
$dispatcher->listen('vend.surveys.surveyanswerdefined.creating', __CLASS__.'@creating');
$dispatcher->listen('vend.surveys.surveyanswerdefined.created', __CLASS__.'@created');
$dispatcher->listen('vend.surveys.surveyanswerdefined.updating', __CLASS__.'@updating');
$dispatcher->listen('vend.surveys.surveyanswerdefined.updated', __CLASS__.'@updated');
$dispatcher->listen('vend.surveys.surveyanswerdefined.deleted', __CLASS__.'@deleted');
}
/**
* {@inheritDoc}
*/
public function creating(SurveyQuestionsRepositoryInterface $questions, array $data)
{
$this->setRules($questions);
dd($this->rules);
}
/**
* {@inheritDoc}
*/
public function created(SurveyAnswerDefined $surveyanswers)
{
$this->flushCache($surveyanswers);
}
/**
* {@inheritDoc}
*/
public function updating(SurveyAnswerDefined $surveyanswers, array $data)
{
$this->setRules($questions);
dd($this->rules);
}
/**
* {@inheritDoc}
*/
public function updated(SurveyAnswerDefined $surveyanswers)
{
$this->flushCache($surveyanswers);
}
/**
* {@inheritDoc}
*/
public function deleted(SurveyAnswerDefined $surveyanswers)
{
$this->flushCache($surveyanswers);
}
/**
* Flush the cache.
*
* @param \vend\Surveys\Models\Surveyanswers $surveyanswers
* @return void
*/
protected function flushCache(SurveyAnswerDefined $surveyanswers)
{
$this->app['cache']->forget('vend.surveys.surveyanswerdefined.all');
$this->app['cache']->forget('vend.surveys.surveyanswerdefined.'.$surveyanswers->id);
}
private function setRules($questions){
foreach($questions as $question){
foreach($questions->controls as $control){
$this->rules['control_' . $control->id] = $this->makeRules($control);
}
}
}
private function makeRules($control){
$rules = [];
if($control->is_required){
$rules[] = 'required';
}
if($control->validation_rule == 'Text'){
if($control->min_length){
$rules[] = 'min:' . $control->max_length;
}
if($control->max_length){
$rules[] = 'max:' . $control->max_length;
}
}
if($control->validation_rule == 'Number'){
$rules[] = 'numeric';
if($control->min_value){
$rules[] = 'min:' . $control->max_length;
}
if($control->max_value){
$rules[] = 'max:' . $control->max_length;
}
}
}
}
答案 0 :(得分:0)
您可以(IMO)使用模型事件,例如:保存时(适用于创建和更新),您可以使用if (isspace(passCode.at(i)) != 0)
方法调用规则来设置和验证规则:
setCustomRules
这应该在模型中,并且还有其他方式来注册事件,但我最喜欢这种方式。有关详细信息,请查看Laravel Website@Events
答案 1 :(得分:0)
迟到的回答
您还可以使用Model Mutators并验证
e.g。
String query= "SELECT " +columnName+ " FROM " + tableName+ " WHERE " + "eventId= " + eventiD+ " AND date LIKE = " + yourTargetDate
try {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
// Your code enter here
// example : event.setName(cursor.getString(cursor.getColumnIndex(Name)))
cursor.moveToNext();
}
} finally {
cursor.close();
sqlDB.close();
}
return events;