Laravel custom validation messages -
i'm trying validate uk postcode using laravel
. here's i've got:
//routes.php $rules = array( 'pcode' => array('required:|regex:/^([gg][ii][rr] 0[aa]{2})|((([a-za-z][0-9]{1,2})|(([a-za-z][a-ha-hj-yj-y][0-9]{1,2})|(([a-za-z][0-9][a-za-z])|([a-za-z][a-ha-hj-yj-y][0-9]?[a-za-z])))) [0-9][a-za-z]{2})$/') ); $messages = array( 'required' => 'the :attribute field required.', 'pcode' => array('regex', 'poscode should valid uk based entry'), ); $validator = validator::make(input::all(), $rules, $messages);
in blade
:
<input id="postcode" name="pcode" value="{{input::old('pcode')}}" type="text" placeholder="postcode" class="form-control" xequired="" /> @if( $errors->has('pcode') ) <span class="error" style='background-color: pink;'>{{ $errors->first('pcode') }}</span> @endif
if submit form empty pcode
field, warns me required field. if enter invalid postcode, '74rht' say, validator nothing or fails display custom message defined above?
the laravel manual states:
note: when using regex pattern, may necessary specify rules in array instead of using pipe delimiters, if regular expression contains pipe character.
change $rules
structure:
$rules = array( 'pcode' => array( 'required', 'regex:/^([gg][ii][rr] 0[aa]{2})|((([a-za-z][0-9]{1,2})|(([a-za-z][a-ha-hj-yj-y][0-9]{1,2})|(([a-za-z][0-9][a-za-z])|([a-za-z][a-ha-hj-yj-y][0-9]?[a-za-z])))) [0-9][a-za-z]{2})$/' ) );
if doesn't work, maybe regex isn't valid, try use easier regex check if validator works.
Comments
Post a Comment