Laravel独特的验证从标签输入多个值 [英] Laravel unique validation for multiple values from tags input
本文介绍了Laravel独特的验证从标签输入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用标签上输入laravel独特的验证,但我面对的问题有多个值,独特的规则运作良好时插入一个值,但如果添加多个值,但这并没有工作。
I need to use laravel unique validation over tags inputs,but i face issue with multiple values ,the unique rule working well when insert one value, but when add more than one value it does't work .
这是我的控制器code
this my controller code
public function createteacher()
{
$rules = array (
'teacher-name' => 'required|unique:teacher,name',
);
$msgs =array(
'teacher-name.required' => 'please insert teacher name',
'unique' => 'teacher name already exist',
);
$validator = Validator::make(Input::all(),$rules,$msgs);
if ($validator->fails())
{
return Response::json(['success'=>false,'error'=>$validator->errors()->toArray() ]);
}
else
{
$c = count(explode(',', Input::get('teacher-name')));
$teachers_ids = explode(',', Input::get('teacher-name'));
//if Validated
for ($i = 0 ; $i < $c ; $i++)
{
$new_teacher = Teacher::firstOrCreate(array('name'=>$teachers_ids[$i]));
}
return Response::json(['success'=>true]);
}
}
和本我的看法code
and this my view code
<div class="col-md-6">
<div class="box box-redorange">
<div class="box-header">
<h3 class="box-title" style="float:right"> Adding Teachers </h3>
<i class="fa fa-spinner fa-spin fa-lg" style="display:none float:left;" id="teacher-adding-load"></i>
</div>
<div class="box-body">
{{Form::open(array('url'=>'management/createteacher','id'=>'teacher-form'))}}
<div class="form-group">
<label for="teacher-name[]">Teacher Name </label>
<select multiple data-role="tagsinput" name="teacher-name[]" id="teacher-name" class="form-control" autofocus="true">
<option value="" ></option>
</select>
</div>
</div> <!-- end box body-->
<div class="box-footer">
{{Form::submit('Add',array('style'=>'color:#fff;font-weight:bold; ','class' => 'btn btn-redorange btn-block btn-sm')) }}
{{Form::close()}}
</div> <!--end box footer -->
</div> <!-- end box -->
<div class="form-group help-block teahcer-form-msgs" style="display:none;">
<small></small>
</div>
</div> <!-- end col-m-6 -->
终于我的ajax code
finally my ajax code
$(document).ready(function(){
var msgs = $('.teahcer-form-msgs');
$('#teacher-form').submit(function(e){
e.preventDefault();
var $teacher_loading = $('#teacher-adding-load').show();
// form data
var formData = new FormData ();
formData.append('teacher-name' , $('#teacher-name').val());
$.ajax({
url:'/elaqsacenter/public/management/createteacher',
method:'post',
processData:false,
contentType:false,
cache:false,
dataType: 'json',
data:formData,
complete: function(){
$teacher_loading.hide();
},
global: false,
success:function(data){
msgs.hide().find('small').empty();
if (!data.success) {
$.each(data.error,function(index,error){
msgs.find('small').append('<p class="text-danger">'+'<span
class="glyphicon glyphicon-exclamation-sign" aria-hidden="true">
</span>'+' '+error+'</p>');
// disable submit button when throw Validation messages
$('input[type=submit]').prop("disabled", true);
setTimeout(function() {
$('input[type=submit]').prop("disabled", false);
}, 4000);
});
msgs.slideDown();
msgs.delay(3000).slideUp();
}else{
msgs.find('small').append('<p class="text-success">data Inserted</p>');
msgs.slideDown();
msgs.delay(3000).slideUp();
setTimeout(function(){location.reload();},2000);
}
},
global: false,
error:function(){},
});
});
});
我的主要问题是如何我可以使用独特的规则与标签输入插入多个值
推荐答案
我找到更简单的方法可以遍历tagsInput值来验证它,如下
I found more simple way can loop over tagsInput values to validate it as below
$c = count(Input::get('teacher-name') );
//In view the name must be name="teacher-name[]"
$teacher_name =Input::get('teacher-name');
/* To loop over multiple values that insert from tags input.
you can add all laravel validation with this way .
and custom :attribute by setAttributeNames() method */
for ($i = 0; $i < $c ; $i++){
$rules[$i] = 'required|between:3,15';
$customAttributes[$i] = $i + 1 ;
}
$msgs = array('required' => '.Teacher Name Is Required' ,
'between' => '.Teacher Num (:attribute) must be between 3 : 15 chars' ) ;
$validateTeacher = Validator::make($teacher-name,$rules,$msgs);
$validateTeacher->setAttributeNames($customAttributes);
if ($validateDivision->fails() OR $validateGrade->fails())
{
// return Ajax Validation Messages OR laravel Validation Messages
}
通过以上方式,您可以遍历tagsInput值一个接一个,享受:)
By the above way you can loop over tagsInput values one by one, Enjoy :)
这篇关于Laravel独特的验证从标签输入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文