dongxiongshi9952 2015-10-19 22:15
浏览 32

如何在Laravel中使用setter方法设置验证规则?

I need to set the $rules array in the Validator Class using a setter method.

I need to set defined the $rules using a setter because the rules are stored in a database. I need to use Eloquent to query the database to figure out what will the rules look like.

I extended the Validator Class and added a method called setCustomRules(). I injected a class into this method which will allow me to use Eloquent to read the database to determine the rules.

But, how would I force the setCustomRules() method to fire when Laravel attempt to validate? This method must take place first to ensure that the rules are set before the validation takes place.

This is what I have done

<?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);
    }


}

Edited Based on The Alpha's answer, I created an Event Handler that will let me create the rules. But not sure how to tell Laravel to validate now.

This is my EventHandler class

<?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;
            }

        }


    }
}
  • 写回答

2条回答 默认 最新

  • drt3751 2015-10-19 22:41
    关注

    You can (IMO) utilize the model events, for example: when saving (Applies to create & Updated) you may call the setCustomRules method to set and validate rules using something like this:

    public static function boot()
    {
        parent::boot();
    
        // This method will be called on creating and updating
        // but there are separate events for both methods so you
        // may use separate handlers for different events like : 
        // static::creating(...) static::updating(...)
        static::saving(function($user)
        {
            // Set rules and validate here
        });
    }
    

    This should be in the Model and there are other ways to register events but I prefer this way most. For more information, check Laravel Website@Events

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?