php - How a chosen multiselect can be validated with JsValidator in Laravel 5 -
i using chosen convert simple select in multiselect here...
<div class="form-group"> {!! form::label('media_formats', trans('medias::messages.valid_formats'), ['class' => 'control-label']) !!} {!! form::select('media_formats[]', $media_formats, null, ['class' => 'form-control', 'id' => 'media_formats', 'multiple', 'data-chosen']) !!} </div>
trying validate media_format element has been selected using jsvalidator package...
{!! jsvalidator::formrequest('maravel\media\http\requests\admin\mediaresourcecreaterequest', '#create-edit-form' ) !!}
and finally, using following rule @ request class...
public function rules() { return [ ... 'media_formats' => 'required', ]; }
the laravel validation bag of errors returns media format error, not being printed anywhere. why? has fact chosen hides select element? tried...
{!! jsvalidator::formrequest('maravel\media\http\requests\admin\mediaresourcecreaterequest', '#create-edit-form' )->ignore('') !!}
with no success. or has fact field name media_formats[] , not media_formats?
finally answer in 2 problems suspecting from. doing...
{!! jsvalidator::formrequest('maravel\media\http\requests\admin\mediaresourcecreaterequest', '#create-edit-form' )->ignore('') !!}
...and if do...
public function rules() { return [ ... 'media_formats[]' => 'required', ]; }
the error message gets printed because jquery validate looks input name 'media_formats[]' -with brackets-. however, laravel validation not work way , expects name of field without brackets.
so, figured out workaround working. if in request do
public function rules() { return [ ... 'media_formats' => 'required|array', ]; }
and change view used in jsvalidator , add these lines before rules parameter set...
<?php foreach($validator['rules'] $i=>$rule){ foreach($rule $j=>$rule_type){ foreach($rule_type $k=>$rule_option){ if($rule_option[0] == "array") { $validator['rules'][$i."[]"] = $validator['rules'][$i]; unset($validator['rules'][$i]); } } } }; ?> rules: <?php echo json_encode($validator['rules']); ?>
i able change rules array , concatenate '[]' fields validated arrays jquery validate can find element error should appear.
the view used jsvalidator can changed on jsvalidator config file.
return array( /* * default view used render javascript validation code */ 'view' => 'jsvalidation::custom_bootstrap',
Comments
Post a Comment