Skip to content

Instantly share code, notes, and snippets.

@niraj-shah
Last active February 19, 2016 12:26

Revisions

  1. niraj-shah revised this gist Feb 19, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion startswith.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // validate that mobile number starts with '07'
    $rules = [
    'mobile' => 'required|numeric|startswith:07',
    'mobile' => 'required|numeric|startswith:07',
    ];

    // custom validator called startswith
  2. niraj-shah created this gist Feb 19, 2016.
    20 changes: 20 additions & 0 deletions startswith.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // validate that mobile number starts with '07'
    $rules = [
    'mobile' => 'required|numeric|startswith:07',
    ];

    // custom validator called startswith
    Validator::extend('startswith', function( $attribute, $value, $parameters ) {
    return substr( $value, 0, strlen( $parameters[0] ) ) == $parameters[0];
    });

    // custom replacer, so we can replace :value in the message
    Validator::replacer('startswith', function( $message, $attribute, $rule, $parameters ) {
    return str_replace( [':value'], $parameters, $message );
    });

    // Error message for startswith failure
    $messages = [ 'startswith' => ':attribute needs to starts with :value' ];

    // validate input
    $validation = Validator::make( Input::all(), $rules, $messages );