giannis christofakis
giannis christofakis

Reputation: 8311

Create a custom Validation rule that accepts two input parameters

I've created a custom Validation rule that accepts one input argument.

Validator::extend('dns', function($attribute, $host, $parameters)
{
    return ($host !== gethostbyname($host));
});

The rules

public static $rules = array(
    'nameserver'    => 'dns'
);

I have created a new file called validators.php and include it in the global.php file in order to be global.

I want to pass two input arguments in order to make some more checks compare to each other. How can I succeed this?

Upvotes: 0

Views: 611

Answers (1)

Rob Gordijn
Rob Gordijn

Reputation: 6511

send extra parameters like:

public static $rules = array(
    'nameserver'    => 'dns:foobar'
);

and access those via:

$parameters[0]

in the closure.

[edit] A way to seed the validator rules with input:

// model
static $rules = array(
     'valOne' => 'required|custom:%s'
    ,'valTwo' => 'required'
);

// controller
$inputValues = Input::only(array(
     'valOne'
    ,'valTwo'
));

$rules = MyModel::$rules;

$rules['valOne'] = sprintf($rules['valOne'], Input::get('valTwo'));

$validator = Validator::make($inputValues, $rules);

Upvotes: 1

Related Questions