Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel custom validation issue

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".

like image 367
Lucky Soni Avatar asked Jan 28 '26 04:01

Lucky Soni


1 Answers

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

like image 175
Manish Avatar answered Jan 29 '26 19:01

Manish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!