0

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?

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

My rules are inside my model. Your above example passes string 'foobar' inside my filter. How am I going to pass the value of the 'foobar' input field?
Does the 'dns' validation really needs values from other inputs? Perhaps you can validate those in a seperate validation rule. If not, perhaps you can do something with str_replace for the value in $rules['nameserver'] with input::get('foobar'). (when creating the validator)
My goal is to validate if ip points to the DNS that was given. So I need both input field. I assume there is no such a thing built-in in Laravel. I guess I can say Input::get(), but isn't there a better option?
Not at the moment without a 'dirty' fix (aka feeding input vars in the rules while building the validator). Checkin ip and dns seems like a task wich can take some time, maybe suitable for a background process?
I think I'd prefer to use session variables rather than Input::get().
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.