在Laravel应用程序中使用Javascript进行表单验证

时间:2017-05-22 12:08:20

标签: javascript php laravel

我有一个Laravel项目。不是我做的。但我现在正在管理应用程序。我在项目中遇到了问题。需要删除一些验证。使强制项目可选。这是视图文件。

<div class="form-group col-md-3 col-sm-12">
    <label>Number of People <span class="required">*</span></label>
    <input class="form-control" name="no_of_people" id="no_of_people" type="number"
           value="{{isset($cur_package) ? $cur_package->no_of_people : null}}">
</div>

这是表单的刀片模板。这是我需要删除所需验证部分的HTML输入。

var validator = new FormValidator('frm_trip_package', [{
name: 'title',
display: 'Title',
rules: 'required|max_length[50]'
},{
    name: 'description',
    display: 'Description',
    rules: 'required|max_length[2500]|min_length[90]'
}, {
    name: 'no_of_people',
    display: 'Number of people',
    rules: 'required'
}, {
    name: 'iteneries',
    display: 'Itineraries',
    rules: 'required'
}, {
    name: 'method_of_travel',
    display: 'Method of travel',
    rules: 'required'
}, {
    name: 'total_hours',
    display: 'Total Hours',
    rules: 'required'
}, {
    name: 'total_minutes',
    display: 'Total Minutes',
    rules: 'required'
}, {
    name: 'price',
    display: 'Price',
    rules: 'required|max_length[8]'
}, {
    name: 'terms',
    display: 'Terms',
    rules: 'required'
}, {
    name: 'images',
    display: 'Images',
    rules: 'required'
}, {
    name: 'pricing_type',
    display: 'Pricing Type',
    rules: 'required'
}
], function (errors, event) {
if (errors.length > 0) {
    var errorString = '<a href="#" class="close" onclick="hideTripErrorMessages()">&times;</a>';

    for (var i = 0, errorLength = errors.length; i < errorLength; i++) {
        if (errors[i].name == 'images') {
            errors[i].message = 'Please add images to your package';
        }
        errorString += errors[i].message + '<br />';
    }

    $("#trip_error_box").show();
    $("#trip_error_box").html(errorString);
}
});

而且我发现这个javascript代码也与验证过程有关。但我改变了。没啥事儿。

{{1}}

这是一种标准的验证方式吗?有人可以告诉我。

2 个答案:

答案 0 :(得分:0)

更新此

{
    name: 'description',
    display: 'Description',
    rules: 'required|max_length[2500]|min_length[90]'
} 

用这个并尝试

{
        name: 'description',
        display: 'Description',
        rules: 'max_length[2500]|min_length[90]'
    }

答案 1 :(得分:0)

将javascript代码更改为

var validator = new FormValidator('frm_trip_package', [{
    name: 'title',
    display: 'Title',
    rules: 'required|max_length[50]'
},{
    name: 'description',
    display: 'Description',
    rules: 'required|max_length[2500]|min_length[90]'
}, {
    name: 'iteneries',
    display: 'Itineraries',
    rules: 'required'
}, {
    name: 'method_of_travel',
    display: 'Method of travel',
    rules: 'required'
}, {
    name: 'total_hours',
    display: 'Total Hours',
    rules: 'required'
}, {
    name: 'total_minutes',
    display: 'Total Minutes',
    rules: 'required'
}, {
    name: 'price',
    display: 'Price',
    rules: 'required|max_length[8]'
}, {
    name: 'terms',
    display: 'Terms',
    rules: 'required'
}, {
    name: 'images',
    display: 'Images',
    rules: 'required'
}, {
    name: 'pricing_type',
    display: 'Pricing Type',
    rules: 'required'
}
], function (errors, event) {
    if (errors.length > 0) {
        var errorString = '<a href="#" class="close" onclick="hideTripErrorMessages()">&times;</a>';

        for (var i = 0, errorLength = errors.length; i < errorLength; i++) {
            if (errors[i].name == 'images') {
                errors[i].message = 'Please add images to your package';
            }
            errorString += errors[i].message + '<br />';
        }

        $("#trip_error_box").show();
        $("#trip_error_box").html(errorString);
    }
});

将标签更新为

<label>Number of People</label>

这非常简单,您只需在验证器规则中搜索表单输入ID并将其删除。