Multiple File Upload Validation Message Problem - validation

Cake3.6:
I am validating a form field which allows multiple files to be uploaded:
$this->Form->input('listing_images. ', ['type' => 'file', 'multiple' => 'multiple']);
I have a custom validation provider which correctly validates the multiple images:
$validator ->add('listing_images', 'listing_images', [
'rule' => [
'dimensions', [
'min' => ['w' => 100, 'h' => 100],
'max' => ['w' => 1600, 'h' => 1600]
]
],
'message' => 'Maximum image size is 1600 x 1600 pixels',
'provider' => 'custom'
]);
The problem is that when validation fails the validation error does not appear below the field. If make the file upload single file only and name is 'listing_images' the validation error does appear.
Why does it not work for multiple?

While it may work partially, the trailing dot syntax that you are using isn't supported (and the trailing space only makes things worse), the form helper will not be able to find the field based on that name.
You can use the name option to specify a name with trailing brackets as required for a multiple file upload HTML input, while passing the regular field name that the form helper understands:
echo $this->Form->control('listing_images', [
'type' => 'file',
'name' => 'listing_images[]',
'multiple' => 'multiple',
]);

Related

Handling CodeIgniter form validation (rule keys and data types)

Okay, so I've been searching for a while this question, but couldn't find an answer (or at least some direct one) that explains this to me.
I've been using CodeIgniter 3.x Form Validation library, so I have some data like this:
// Just example data
$input_data = [
'id' => 1,
'logged_in' => TRUE,
'username' => 'alejandroivan'
];
Then, when I want to validate it, I use:
$this->form_validation->set_data($input_data);
$this->form_validation->set_rules([
[
'field' => 'id',
'label' => 'The ID to work on',
'rules' => 'required|min_length[1]|is_natural_no_zero'
],
[
'field' => 'username',
'label' => 'The username',
'rules' => 'required|min_length[1]|alpha_numeric|strtolower'
],
[
'field' => 'logged_in',
'label' => 'The login status of the user',
'rules' => 'required|in_list[0,1]'
]
]);
if ( $this->form_validation->run() === FALSE ) { /* failed */ }
So I have some questions here:
Is the label key really necessary? I'm not using the Form Validation auto-generated error messages in any way, I just want to know if the data passed validation or not. Will something else fail if I just omit it? As this will be a JSON API, I don't really want to print the description of the field, just a static error that I have already defined.
In the username field of my example, will the required rule check length? In other words, is min_length optional in this case? The same question for alpha_numeric... is the empty string considered alpha numeric?
In the logged_in field (which is boolean), how do I check for TRUE or FALSE? Would in_list[0,1] be sufficient? Should I include required too? Is there something like is_boolean?
Thank you in advance.
The "label" key is necessary, but it can be empty.
The "required" rule does not check length, nor does the "alpha_numeric". It checks that a value is present, it does not check the length of said value. For that, there is min_length[] and max_length[].
If you're only passing a 0 or 1, then this is probably the easiest and shortest route.

Laravel Custom Validation for multi-language application

I am using Laravel Standard Validation, but I want to customize it because I am using multi language in my application, so I have to customize the message.
$rule_validation = [
'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
'agree_promo_code' => request('promo_code') ? 'accepted' : '',
'terms_of_services' => 'accepted',
'aware' => 'accepted'
];
Now I want to write customize validation message for agree_promo_code
I know to write message for phone, but having doubt in agree_promo_code, terms_of_services and aware.
Can anyone help me out to resolve this issue
But please keep in mind about the language. Thank you
You can pass second array to your validate function like
$this->validate($request,[
'phone' => 'required|max:20|min:6|regex:/^[0-9]+$/',
'agree_promo_code' => request('promo_code') ? 'accepted' : '',
'terms_of_services' => 'accepted',
'aware' => 'accepted'
],[
'phone.required' => trans('validation.phone'),
'agree_promo_code.accepted' => trans('validation.agree_promo_code'),
'terms_of_services.accepted' => trans('validation.terms_of_services'),
'aware.accepted' => trans('validation.aware'),
]);
and inside your resources/lang/{lang}/validation.php file ({lang} is your language directory).
you can do something like
return [
'phone' => 'Phone validation message',
'agree_promo_code' => 'agree_promo_code validation message',
'terms_of_services' => 'terms_of_services validation message',
];
So it will set your messages according to respective language.
As you set your Language files for respective language
you need to create validation.php file under respective language folder
and add custom message for each field for each type of validation applied
for example if you have name field and validation check is required
so in your language folder's validation file you will write as below
<?php
return [
'custom' => [
'name' => [
'required' => 'O campo nome é obrigatório.'
]
];
rest all language check will be handled by Laravel itself while displaying validation error messages as per your locale language selected for session

Magento creating custom attribute with custom validations

I need to create an attribute. Also i need to validate that attribute value. So i created a new .js file and add some functions. Then in setup file, i call the function name. But after creating the attribute that validation class wont come with the field.
$installer->addAttribute(MageTest_Module_Model_Name::ENTITY, 'test_value', array(
'input' => 'text',
'type' => 'text',
'label' => 'Test Value',
'backend' => '',
'user_defined' => false,
'visible' => 1,
'required' => 0,
'position' => 60,
'class' => 'validate-testValue',
'note' => 'This should contains 2 digits. Example 00',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
'validate-testValue' is my js function name. Can anyone help me to solve this please.
Thank You.
to use Magento form validator you have to register your method with the Validator object.
Try something like that in your custom js file
Validation.add('validate-testValue','HERE your error message if the field is not valid',function(fieldValue) {
return Validation.get('IsEmpty').test(fieldValue) || fieldValue == "testValue";
});
Ok, this example will only validate if your field value is "testValue", not very usefull.
But you can adapt it I guess :)

image type validation and filesize validation using cakephp

Hi i have this validaton for the images that validates only jpg and png types and also i aadded a filesize that is only 5mb. I want that when i will not select an image it will bypass. So no validation for selecting images. What happend is when i did not select an image it will validates to select valid image. How will i able to do this that when im not selecting an image it will not validate. Heres my validation below
'images' => [
'extension' => [
'rule' => [
'extension',
['png', 'jpg']
],
'allowEmpty'=>['Empty'],
'message' => 'Select valid image'
],
'fileSize' =>[
'rule'=>
'fileSize',
['<=', '5mb'],
'message' => 'Image must be less than 5mb'
]
],
this is just a simple one. Any help is muchly appreciated.

cakePHP optional validation for file upload

How to make file uploading as optional with validation?
The code below validates even if i didn't selected any file.
I want to check the extension only if i selected the the file.
If i am not selecting any file it should not return any validation error.
class Catalog extends AppModel{
var $name = 'Catalog';
var $validate = array(
'name' => array(
'rule' => '/^[a-z0-9 ]{0,}$/i',
'allowEmpty' => false,
'message' => 'Invalid Catalog name'
),
'imageupload' => array(
'rule' => array('extension',array('jpeg','jpg','png','gif')),
'required' => false,
'allowEmpty' => true,
'message' => 'Invalid file'
),
);
}
thanks in advance
"I assign $this->data['Catalog']['image'] = $this->data['Catalog']['imageupload']['name'];"
So by the time you save your data array, it looks something like this I assume:
array(
'image' => 'foobar',
'imageupload' => array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
)
Which means, the imageupload validation rule is trying to work on this data:
array(
'name' => 'foobar',
'size' => 1234567,
'error' => 0,
...
)
I.e. the value it's trying to validate is an array of stuff, not just a string. And that is unlikely to pass the specified validation rule. It's also probably never "empty".
Either you create a custom validation rule that can handle this array, or you need to do some more processing in the controller before you try to validate it.
Concept:
In Controller, before validating, or saving (which does validation automatically by default) check if any file is uploaded. If not uploaded, then unset validator for the file field.
Sample code:
Controller
// is any image uploaded?
$isNoFileUploaded = ($this->request->data['Model']['field_name']['error'] == UPLOAD_ERR_NO_FILE) ? true : false ;
if ($isNoFileUploaded) {
$this->Model->validator()->remove('field_name');
}
Notes:
This solution comes under preprocessing as one of the two alternative approaches (preprocessing in controller, custom validation in model) suggested by #deceze's answer

Resources