October: OctoberCMS image(file) validation not working properly

Created on 2 Dec 2016  路  11Comments  路  Source: octobercms/october

I am trying to put image(file) validations in my model file but it seems to be not working as I want.Here below is my scenario.

fields.yaml

fields:
    slider_image:
        label: 'Slider Image (jpg,png,gif) (1920 X 600)'
        mode: image
        fileTypes: 'jpeg,jpg,png,gif'
        useCaption: true
        thumbOptions:
            mode: crop
            extension: auto
        span: auto
        required: 1
        type: fileupload

Model.php

public $rules = [    
'slider_image' => 'required|mimes:jpeg,jpg,png,gif',
];

public $customMessages = [                
            'slider_image.required' => 'Please select slider image',                
            'slider_image.mimes' => 'Please select valid slider image',                
];

As you can clearly see here I have a file upload option called as slider_image and in my .yaml file I have put validations to upload only jpeg,jpg,png,gif

But the issue is with my validation rules.

Even if I upload any other extension file (i.e. .zip) I m always having an error message saying

Please select slider image

but it should rather display below error as I have already invalid extension file.

Please select valid slider image

If I do not upload any image then it should display first error and if I upload an invalid image then it should display second error.

Can someone tell me what is wrong in my current scenario here ?

Additionally, I wanted to know if there is any validation rule available through which we can check height width of the image and set rule of minimum height width to upload and put validation message based on it in our model file.

Thanks

Revision Needed Unconfirmed Bug

Most helpful comment

For anyone else to test this, he/she would have to build his own plugin. Either create a plugin yourself and publish eg on github to demonstrate or create additional code for the aforementioned test plugin and create a pull request. That's what daftspunk meant.

All 11 comments

no one ? really ?

Are your model using the Validation trait?
https://octobercms.com/docs/database/traits#validation

It seems you're using the validation trait, otherwise you wouldn't get the messages. But check.

In the past I have encountered problems with fileupload's deferred binding and validation. Can you try if validation passes if you just keep the required rule or put required behind mimes? I'd check that first.

@leocavalcante yes, i am using the Validation trait.

@jBOKA , yes i am using the Validation trait.

Additionally i have tried to put mime validation first and required validation second but i m still having the same issue as it is keep showiing only one error message saying below,even if i m uploading invalid format.

Please select slider image

Slider.php

<?php namespace Technobrave\Slider\Models;

use Model;

/**
 * Model
 */
class Slider extends Model
{
    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [    
    'slider_image' => 'mimes:jpeg,jpg,png,gif|required',
    ];

    public $customMessages = [    
                 'slider_image.mimes' => 'Please select valid slider image',             
                'slider_image.required' => 'Please select slider image',                
    ];

    /*
     * Disable timestamps by default.
     * Remove this line if timestamps are defined in the database table.
     */
    //public $timestamps = false;



    /**
     * @var string The database table used by the model.
     */
    public $table = 'mytable_slider_';


    public $attachOne = [
            'slider_image' => 'System\Models\File'
    ];
}

what should i do ?

This is a complex case. Please submit a Pull Request to the test plugin for investigation that demonstrates the issue. Don't forget to include step by step instructions how how to replicate it using the test plugin.

Ok, This is what i have done in my fields.yaml file https://paste.ee/r/TCREk and this is my Slider.php model file https://paste.ee/r/to2Tf but still i am having the same problem.

For anyone else to test this, he/she would have to build his own plugin. Either create a plugin yourself and publish eg on github to demonstrate or create additional code for the aforementioned test plugin and create a pull request. That's what daftspunk meant.

As above, please follow the instructions provided.

@mittultechnobrave Please submit your example demonstrating this issue to the test plugin so that we can progress towards a resolution.

Test plugin demonstrates a required attachment via Users. Tested working The photo field is required.

    public $rules = [
        'photo' => 'required',
        'portfolio' => 'required',
        'roles' => 'required',
    ];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

oppin picture oppin  路  3Comments

d3monfiend picture d3monfiend  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments

dunets picture dunets  路  3Comments