I have a form with variable input fields that i want to validate. So i use a wildcard but it doesn't seem to work.
The code in my view
@for ($i = 1; $i < 4; $i++)
<div>
<label for="title[{{ $i }}]" class="control-label">Title</label>
<input type="text" name="title[{{ $i }}]">
@if ($errors->has('step-title'.$i))
<span class="help-block">
<strong>{{ $errors->first('step-title'.$i) }}</strong>
</span>
@endif
</div>
@endfor
code in my controller
$this->validate($request, [
'title1' => 'required|alpha_dash',
'title.*' => 'alpha_dash',
]);
The strange thing is that validating with 'title1' works fine but doesn't work with 'title.*'. why can't i use a wildcard? what am i missing?
If you see the documentation to alpha_dash rule, you can notice that you can only validate dash and underscore with the rule alpha_dash
.
alpha_dash
The field under validation may have alpha-numeric characters, as well as dashes and underscores.
The syntax title.*
is used to validate arrays, take a look of this. So I would recommend you to replace the asterisk with a underscore or dash before validating.