mercredi 6 septembre 2017

Validation in dynamic form Laravel 5.5

How can I create a validtion for a dynamic form?

//Controller
public function store(Request $request)
{       
    $rules = [
        'companyName' => 'required',
        'bannerName' => 'required',
        'bannerDescription' => 'required',
        'bannerURL' => 'required',
        'bannerImg' => 'required',
    ];

    $customMessages = [
        'companyName.required' => 'Yo, what should I call you?',
        'bannerName.required' => 'Yo, what should I call you?',
        'bannerDescription.required' => 'Yo, what should I call you?',
        'bannerURL.required' => 'Yo, what should I call you?',
        'bannerImg.required' => 'Yo, what should I call you?',
    ];

    $this->validate($request, $rules, $customMessages);
}

And here is my view, my "name" attr are arrays cause I'll store a bulk of data to DB. Clicking on "+ Add Banner" jquery will clone div with the same 4 inputs. If I'll remove arrays everything will work ofc, but if no I'll get the following error

htmlspecialchars() expects parameter 1 to be string, array given

{!! Form::open(['action' => 'CompanyController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
                        <div class="form-group">
                            <label for="companyName">Company URL Address</label>    
                                <input type="text" class="form-control" value="" id="companyName" name="companyName" placeholder="example.com">
                                <small class="text-danger"></small>        
                        </div>

                        <hr>

                         <div data-sel-baner-box>                         
                            <div data-sel-baner-form>
                                    <div class="panel-heading"><h4 style="text-align: center">New banner</h4></div>

                                    <div class="form-group">
                                        <label for="bannerName">Title</label>     
                                        <input type="text" class="form-control" id="bannerName" value="" name="bannerName[]" placeholder="Name">
                                        <small class="text-danger"></small>
                                    </div>

                                    <div class="form-group">
                                        <label for="bannerDescription">Banner Description</label>
                                        <input type="text" class="form-control" id="bannerDescription" value="" name="bannerDescription[]" placeholder="Description">
                                        <small class="text-danger"></small>
                                    </div>

                                    <div class="form-group">
                                        <label for="bannerURL">Banner URL</label>
                                        <input type="text" class="form-control" id="bannerURL" value="" name="bannerURL[]" placeholder="URL">
                                        <small class="text-danger"></small>
                                    </div>

                                    <div class="form-group">
                                        <label for="bannerImg">File input</label>
                                        <input type="file" class="form-control-file" id="bannerImg" name="bannerImg[]">
                                        <small class="text-danger"></small>
                                   </div>
                               </div>

                       <a href="##" data-sel-btn-add-baner class="btn btn-primary">+ Add Banner</a>
                       <button type="submit" class="btn btn-primary">Save Company</button> 

                    {!! Form::close() !!}       

Any hints please?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire