«

laravel参数验证规则示例

时间:2024-2-18 10:12     作者:韩俊     分类: PHP


laravel 参数验证功能非常强大,这里将平时写的一个比较全的验证记录一下,以备忘,具体代码如下:

<?php
/**
 * Created by PhpStorm.
 * User: yhm
 * Date: 19-6-6
 * Time: 下午3:48
 */

namespace App\Http\Requests\Admin;

use App\Models\CoursePromotion;
use App\Models\CoursePromotionAgents;
use App\Models\Quan;
use App\Models\QuanActivity;
use Illuminate\Foundation\Http\FormRequest;
use Mockery\Exception;
use Validator;

class ToufangQuan extends FormRequest {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }

    /**
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function rules() {
        Validator::make($this->input(),
            [
                'quan_id' => 'required|int|exists:' . (new Quan())->getTable() . ',id',
                'open_remind' => 'required|int|in:0,1',
                'get_alert' => 'required|int|in:0,1',
                'get_message' => 'string|required_if:get_alert,1',
                'lose_alert' => 'required|int|in:0,1',
                'lose_message' => 'string|required_if:lose_alert,1',
                'lose_remind_days' => 'int|required_if:lose_alert,1',
                'lose_remind_time' => [
                    'string',
                    'required_if:lose_alert,1',
                    'regex:/^([0-1]{1}\d|2[0-3]):([0-5]\d)$/'
                ]
            ])->validate();

        $quan = Quan::xget($this->request->get('quan_id'));

        if (!$quan) {
            throw new Exception('券不存在', 21);
        }

        switch ($quan->type) {
            case Quan::TYPE_REG:
                Validator::make($this->input(),
                    [
                        'channel' => 'required|int|in:' . implode(',', array_keys(QuanActivity::getChannelList())),
                        'store_type' => 'required_if:channel,' . QuanActivity::CHANNLE_STORE . '|int|in:' . implode(',', array_keys(QuanActivity::getStoreList())),
                        'promotion_id' => 'required_if:channel,' . QuanActivity::CHANNLE_H5 . '|int|exists:' . (new CoursePromotion())->getTable() . ',id',
                        'seller_uid' => 'required_if:channel,' . QuanActivity::CHANNLE_H5 . '|int|exists:' . (new CoursePromotionAgents())->getTable() . ',agents_uid',
                        'num' => 'required|int|min:1',
                        'begin_time' => 'required|int',
                        'end_time' => 'required|int'
                    ]
                )->validate();
                break;
            case Quan::TYPE_PLATFORM:
                Validator::make($this->input(),
                    [
                        'type' => 'required|int|in:' . implode(',', array_keys(QuanActivity::getTypeList())),
                        'estimate_num' => 'required|int|min:0|max:' . QuanActivity::PLATFORM_TOUFAGNG_MAX_NUM
                    ]
                )->validate();

                if (!empty($this->request->get('select_role'))) {
                    switch ($this->request->get('select_role')) {
                        case QuanActivity::TYPE_FILTER:
                            Validator::make($this->input(),
                                [
                                    'select_role' => 'required|array',
                                ]
                            )->validate();
                            break;
                        case QuanActivity::TYPE_EXPORT:
                            Validator::make($this->input(),
                                [
                                    'select_role' => 'required|string',
                                ]
                            )->validate();
                            break;
                    }
                }
                break;
            case Quan::TYPE_PROMOTION:
                Validator::make($this->input(),
                    [
                        'channel' => 'required|int|in:' . implode(',', array_keys(QuanActivity::getChannelList())),
                        'store_type' => 'required_if:channel,' . QuanActivity::CHANNLE_STORE . '|int|in:' . implode(',', array_keys(QuanActivity::getStoreList())),
                        'promotion_id' => 'required_if:channel,' . QuanActivity::CHANNLE_H5 . '|int|exists:' . (new CoursePromotion())->getTable() . ',id',
                        'seller_uid' => 'required_if:channel,' . QuanActivity::CHANNLE_H5 . '|int|exists:' . (new CoursePromotionAgents())->getTable() . ',agents_uid',
                        'num' => 'required|int|min:1',
                        'begin_time' => 'required|int',
                        'end_time' => 'required|int'
                    ]
                )->validate();
                break;
        }

        return [];
    }
}

标签: php php教程

热门推荐