ServerInformationRequest.php 2.4 KB
<?php

namespace App\Http\Requests\Aside\Devops;

use App\Models\Devops\ServerInformation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $required = 'required';
        // 编辑时不需要验证必填
        if ($this->getRequestUri() != '/a/devops/server/add') {
            $required = '';
        }
        return [
            'type'      => [
                $required, Rule::in(array_keys((new ServerInformation)->ServiceArray()))
            ],
            'ip'        => $required . '|ip',
            'title'     => $required . '|max:200',
            'belong_to' => $required . '|integer|in:1,2', // 1:公司 2:客户
            'sshpass'   => $required . '|max:100',
            'ports'     => $required . '|integer|min:1|max:65535',
        ];
    }

    public function messages()
    {
        $service_array = (new ServerInformation)->ServiceArray();
        $serviceStr    = implode(', ', $service_array);
        return [
            'type.required'      => '服务器类型不能为空',
            'type.integer'       => '服务器类型格式不正确',
            'type.between'       => '服务器类型 ' . $serviceStr,
            'ip.required'        => 'ip不能为空',
            'ip.ip'              => 'ip格式不正确',
            'title.required'     => '服务器标题不能为空',
            'title.max'          => '服务器标题不能超过200个字符',
            'belong_to.required' => '服务器归属不能为空',
            'belong_to.integer'  => '服务器归属格式不正确',
            'belong_to.in'       => '服务器归属只能是公司或者客户',
            'sshpass.required'   => 'ssh密码不能为空',
            'sshpass.max'        => 'ssh密码不能超过100个字符',
            'ports.required'     => 'ssh端口不能为空',
            'ports.integer'      => 'ssh端口格式不正确',
            'ports.min'          => 'ssh端口不能小于1',
            'ports.max'          => 'ssh端口不能大于65535',
        ];
    }

}