2

I am trying to register a custom validation rule but it does not seem to work. I need either of 2 fields to be filled in. One is a URL(link) field and other is a File input(file_upload). Here is my custom validation:

Validator::register('file_check', function($attribute, $value, $parameters) {
    if (!trim($value) == "" || array_get(Input::file($parameters[0]), 'tmp_name')) {
        return true;
    }
    return false;
});

$messages = array(
    'file_check' => 'Please upload a file or provide a link to files.',
);

$rules = array(
    'link' => 'url|file_check:file_upload',
    'file_upload' => 'mimes:jpg,jpeg,gif,png,psd,ai,bmp,xls,xlsx,doc,docx,zip,rar,7z,txt,pdf'

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

if ($validation - > fails()) {
    return Redirect::to('page') - > with_errors($validation - > errors) - > with_input();
}

Need help :)

EDITED

Also, I just noticed that the validation rule should accept "PSD" files but when I try to upload a PSD file it redirects with the error "Invalid file type".

1
  • after $validation->fails() have you checked the contents of $validation to see if the rules are being applied? Commented Jan 30, 2013 at 12:51

3 Answers 3

6

I am maybe late in party but may be somebody will find it useful, in case you need to create implicit rule which will be called even if field is not present in Input (like required,required_if....) use

Validator::extendImplicit( 'validator_name', function($attribute, $value, $parameters)
{

});

Check this out

Sign up to request clarification or add additional context in comments.

Comments

5

I was just struggling with this myself! It turns out that except when a few specific rules are applied to them, Laravel doesn't pass empty fields through the Validator at all. So a custom either-this-or-that rule can't work, since at least one of the two fields is likely to not be visible to it.

You can get around this by moving from the registering-a-new-rule approach to the alternate extend-the-Validator-class approach. Your new class will inherit all the methods of the standard Validator, including a method called "implicit" (you can find the original on line 215 of the standard Validator class), which specifies a whitelist of rules that Laravel should pass fields along to even if they are empty. Add your new rule to that list and you should be good to go.

Comments

0

Jason is right, but there is one thing that can be confusing.

Laravel's 'registering a new rule' approach uses the syntax 'Validator::extend(...'. As described elsewhere, this is convenient when you want to customize in a special situation. However, if you want to add a number of reusable rules, then you probably want to use the extend-the-Validator-class approach. In that case, IF you have a rule conditionally requires something, you need to override the existing implicitRules array with a new one adding your rule.

If the first rules you add don't conditionally require, you will think you have it nailed, then you will spend hours trying to figure out why your new 'RequireWhenBlaBla...' rule is invisible.

Comments

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.