仅验证PHP Laravel中的一个值选择下拉输入字段

时间:2019-05-02 07:33:34

标签: php laravel validation drop-down-menu

我正在处理Laravel应用程序,从而捕获了一些输入 字段,其中包含某些卡上的下拉字段,我通过AJAX将其提交到后端。

在后端,我想创建一个验证逻辑,其中 选择下拉列表中只能只有一名丈夫或妻子 这意味着用户应该仅从前端卡上的选择下拉列表中选择一个丈夫或妻子

表单模板

<form method="POST" action="#" id="phase3" accept-charset="UTF-8">
<!-- CSRF TOKEN-->
<input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">

<!-- Card 1-->
<div class="col-md-4 col-sm-12 phonecard3">
<div class="innerDiv">
    <!-- Gender -->
    <div class="row">
        <label class="fm-input"> Relation :</label>
        <select class="fm-input otherMenu" id="relation1" required>
            <option value="Husband"> Husband </option>
            <option value="Wife"> Wife </option>
            <option value="Son"> Son </option>
            <option value="Daughter"> Daughter </option>
        </select>
    </div>
    <!-- END -->
</div>
</div>
<!-- End card 1-->

<!-- Card 2-->
<div class="col-md-4 col-sm-12 phonecard3">
<div class="innerDiv">
    <!-- Gender -->
    <div class="row">
        <label class="fm-input"> Relation :</label>
        <select class="fm-input otherMenu" id="relation2" required>
            <option value="Husband"> Husband </option>
            <option value="Wife"> Wife </option>
            <option value="Son"> Son </option>
            <option value="Daughter"> Daughter </option>
        </select>
    </div>
    <!-- END -->
</div>
</div>
<!-- End card 2-->

<!-- Card 3-->
<div class="col-md-4 col-sm-12 phonecard3">
<div class="innerDiv">
    <!-- Gender -->
    <div class="row">
        <label class="fm-input"> Relation :</label>
        <select class="fm-input otherMenu" id="relation3" required>
            <option value="Husband"> Husband </option>
            <option value="Wife"> Wife </option>
            <option value="Son"> Son </option>
            <option value="Daughter"> Daughter </option>
        </select>
    </div>
    <!-- END -->
</div>
</div>
<!-- End card 3-->

<div class="row">
    <div class="col-md-6 col-md-offset-3">
        <button class="btn btn-lg lov1" type="submit"> Save & Create for Spouse & Children <i class="fa fa-check-circle" ></i></button>
    </div>
</div> 
</form>

AJAX代码以提交表单详细信息

$("#phase3").submit(function( event ) {
    event.preventDefault();

    //Fetch data
    var token = $('#token').val();

    var relation1 = $("#relation1").val(); 
    var relation2 = $("#relation2").val(); 
    var relation3 = $("#relation3").val(); 

    //Store in a JS object
    var type ={
        'token': token,
        //Relations
        'relation1' : relation1,
        'relation2' : relation2,
        'relation3' : relation3
    };

    //console.log(type);

    $.ajax({
        type: "POST",
        url: "submitPhase3",
        data:JSON.stringify(type),
        contentType: 'application/json',
        dataType: "json",
        success: function(response){
            //console.log(response);
        },
        //Alert errors from backend
        error: function(data) {
            //console.log(data);
        }
    });
});

后端PHP函数来处理请求

public function submitPhase3(Request $request){

    dd($request->all());

    $validation = $this->validate($request, [
        //Relations
        'relation1'=>  isset($request->relation1) ? $request->relation1 : null,
        'relation2'=>  isset($request->relation2) ? $request->relation2 : null,
        'relation3'=>  isset($request->relation3) ? $request->relation3 : null,
    ]
    );
}

App / Providers / AppServiceProvider.php文件中的自定义验证器

 public function boot(){

    //Custom Validation
    Validator::extend('relation', 
        function ($attribute, $value, $parameters, $validator){

            $relationMaxMap = [];//create a map with key=>'value', value=>'maximum'
            $relations = Config::get('app.relations');
            foreach($relations as $relation) {
               $relationMaxMap[$relation['value']] = $relation['maximum'];
            }
            //$relationMaxMap will be [1=>1, 2=>1,3=>10,4=>10];
            $requestMap = []; //map from the request 'value'=>'count', this way you know what relations has been selected and how many times
            foreach($values as $value) {
                if(!isset($values[$value])) { //at the begining
                    $requestMap[$value] = 1;
                }
                $requestMap[$value]++;
            }
            //Compare the maps to check if it execedes the maximum allowed in your app
            foreach($relationMaxMap as $value=>$maximum) {
                if($requestMap[$value] > $maximum) {
                    return false;
                }
            }
            return true;
    });
}

新功能

public function submitPhase3(Request $request){

        dd($request->all());

        $validation = $this->validate($request, [
            //Pull code from custom validator
            'relation' => 'relation'
        ]
    );
}

2 个答案:

答案 0 :(得分:0)

我将在config中定义一个值列表,并将它们传递到给定的视图(键是值,文本是显示的内容):

$relations=[
    ['value'=>1,'display'=>'Wife','maximum'=>1]
    ['value'=>2,'display'=>'Husband','maximum'=>1],
    ['value'=>3,'display'=>'Son','maximum'=>10],
    ['value'=>4,'display'=>'Daughter','maximum'=>10]
];

控制器:

[...]
     return view('my_view', Config::get('app.relations');
[...]

查看:

[...]
@foreach ($relations as $relation)
    <option value="{{$relation['value']}}">{{$relation['display']}}</option>
@endforeach
[...]

我也将relations作为数组传递到custom validator中:

<?php

class CustomValidator extends \Illuminate\Validation\Validator 
{

    public function validateRelations($attribute, $values, $parameters)
    {
        $relationMaxMap = [];//create a map with key=>'value', value=>'maximum'
        $relations = Config::get('app.relations');
        foreach($relations as $relation) {
           $relationMaxMap[$relation['value']] = $relation['maximum'];
        }
        //$relationMaxMap will be [1=>1, 2=>1,3=>10,4=>10];
        $requestMap = []; //map from the request 'value'=>'count', this way you know what relations has been selected and how many times
        foreach($values as $value) {
            if(!isset($values[$value])) { //at the begining
                $requestMap[$value] = 1;
            }
            $requestMap[$value]++;
        }
        //Compare the maps to check if it execedes the maximum allowed in your app
        foreach($relationMaxMap as $value=>$maximum) {
            if($requestMap[$value] > $maximum) {
                return false;
            }
        }
        return true;
    }

}

注意:代码未经测试,但这就是我的处理方式

我也将使用验证器为form request

答案 1 :(得分:0)

我想我会用这种方法,希望对您有所帮助 Use rule in of laravel

$options = ['Husband', 'Wife', 'Son', 'Daughter']; // rule default
$relation1 = $request->input('relation1');         // value of relation 1
$relation2 = $request->input('relation2')          // value of relation 2

// get rule of relation 2, if relation 1 choose husband 
//then relation2 will remove husband --> ['Wife', 'Son', 'Daughter']
$ruleForRelation2 = array_diff($options, [$relation1]);        
// same relation 1
$ruleForRelation3 = array_diff($options, [$ruleForRelation2]);


$validation = $this->validate($request, [
    'relation1'=>  Rule::in($options),
    'relation2'=>  Rule::in($ruleForRelation2),
    'relation3'=>  Rule::in($ruleForRelation3),
]
);