如何验证动态输入字段,laravel 5.4?

时间:2017-06-08 19:05:23

标签: javascript php laravel-5.4

我有一个带有两个字段的html表单,可以使用javascript动态添加,但我无法验证请求文件中的输入数组

请参阅以下表格

{!!Form::open(['route'=>'create_course.store','class'=>'ed_contact_form ed_toppadder40', 'files'=>'true'])!!} 
                    <div class="form-group {!!$errors->has('name')?'has-error':''!!}">
                        {!!Form::text('name',null,['class'=>'form-control', 'placeholder'=>'Titre du cours'])!!} {!!$errors->first('name', '<small class="help-block">:message</small>')!!}
                    </div>
                    <div class="form-group {!! $errors->has('description')?'has-error':''!!}">
                        {!!Form::textarea('description', null,['class'=>'form-control','placeholder'=>'Description du Cours'])!!}
                        {!!$errors->first('description','<small class="help-block">:message</small>')!!}
                    </div>
                        <div class="form-group {!!$errors->has('category')?'has-error':''!!}">
                        {!!Form::select('category' ,array(
                        'Choisir la Catégorie du cours',
                        '1'=>'Développement Web',
                        '2'=>'Développement mobile',
                        '3'=>'Business management',
                        '4'=>'Langues',
                        '5'=>'Motivation'

                        ),['class'=>'form-control','id'=>'selectCat'])!!}
                        {!!$errors->first('category','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!!$errors->has('price')?'has-error':''!!}">
                        {!!Form::text('price','Gratuit',['class'=>'form-control','placeholder'=>'price'])!!}
                        {!!$errors->first('price','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!!$errors->has('sale')?'has-error':''!!}">
                        {!!Form::number('sale',null,['class'=>'form-control','placeholder'=>'Prix Promo'])!!}
                        {!!$errors->first('sale','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!!$errors->has('langue')?'has-error':''!!}">

                        {!!Form::select('langue', array(
                        'Langue du cours',
                        'Moore'=>'Moore',
                        'Dioula'=>'Dioula',
                        'Wolof'=>'Wolof',
                        'Swahili'=>'Swahili',
                        'Fula'=>'Fula',
                        'Yoruba'=>'Yoruba',
                        'Twi'=>'twi'

                        ),['class'=>'form-control','id'=>'selectLangue'])!!}
                        {!!$errors->first('langue','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!!$errors->has('duration')?'has-error':''!!}">
                        {!!Form::number('duration','Gratuit',['class'=>'form-control','placeholder'=>'Durée du cours en semaines'])!!}
                        {!!$errors->first('duration','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!!$errors->has('image')?'has-error':''!!}">
                    {!!Form::label('imageCourse','Image du Cours')!!}
                        {!!Form::file('image',['class'=>'form-control','placeholder'=>'image du cours','id'=>'imageCourse'])!!}
                        {!!$errors->first('image','<small class="help-block">:message</small>')!!}
                    </div>
                <div class="form-group {!! $errors->has('video[]')?'has-error':''!!}">

                    {!!Form::label('CourseVideo', 'Les Vidéos de votre Cours')!!}
                    <div class="lesson_container">
                    <div>
                    {!!Form::text('lesson_title[]',null,['class'=>'form-control','placeholder'=>'Titre de la Lesson'])!!}
                  {!! Form::file('video[]',['class'=>'form-control','id'=>'CourseVideo'])!!}
                  {!!
                   $errors->first('video[]','<small class="help-block">:message</small>')
                  !!}
                        </div>
                    <div class="row"></div>
                        <br>
    {!!Form::button('Ajouter une lesson &nbsp;<i class="fa fa-plus" aria-hidden="true"></i>
',['class'=>'btn ed_btn ed_orange pull-left','id'=>'add_lesson'])!!}

                        </div>
                    <br>

               </div>
                {!!Form::hidden('prof_id',$prof_id)!!}
                    {!! Form::submit('Enregistrer',['class'=>'btn ed_btn ed_orange pull-right'])!!}
                {!!Form::close()!!}

我的脚本动态添加fiels

$(document).ready(function()
                {
   var wrapper=$(".lesson_container");
   var add=$("#add_lesson");
   $(add).click(function(e)
               {
       e.preventDefault();
       $(wrapper).append('<br> <br><br><div><label for="newvideo">Ajouter une Video</label><input type="text" name="lesson_title[]" value="" class="form-control", placeholder="Titre de la lesson"  id="newvideo"><input type="file" name="video[]" class="form-control"><div></div><br><button href="#" class="btn-primary" id="delete">Suprimer &nbsp;<i class="fa fa-trash-o" aria-hidden="true"></i></button></div>')

   });
   $(wrapper).on("click","#delete", function(e){
     e.preventDefault();
       $(this).parent('div').remove();
   });

 });

和我的请求代码

public function rules()
{
    $rules=[
        'name'=>'required|string|max:255',
        'description'=>'required|string',
        'category'=>'required|string',
        'price'=>'required|string',
        'sale'=>'string',
        'langue'=>'required|string',
        'duration'=>'required|integer',
         'image'=>'required|image',
        'prof_id'=>'required|integer'
        //
    ];
    var_dump($this->input('video'));
    $videos=count($this->input('video'));
    foreach(range(0,$videos) as $index)
    {
        $rules['video'.$index]='file|mimes:mp4,mov,wmv';

    }
    $lessons=count($this->input('lesson_title'));
    foreach(range(0,$lessons) as $idx)
    {
        $rules['lesson_title.'.$idx]='required|string';
       // echo $rules['lesson_title.'.$idx];
       // var_dump($this->input('lesson_title'));
    }
    return $rules ;
}

}

当我打印两个数组$this->input('lesson_title')$this->input('video')时,我得到NULL表示数组是空的。

这里有什么问题?有什么帮助吗?

0 个答案:

没有答案