im using a validation rule for image file in cakephp that i want to validate image extension only if not empty.
the problem is that cakephp is validating the extension if i don't give an image so it gives the validation error 'Only images files'
here's my validation rule that i use to validate an image
image' => array(
'notEmpty' => array(
//'message' => 'Your custom message here',
'allowEmpty' => true,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'extension' => array(
'rule' => array('extension', array('png','jpg','jpeg')),
'message' => 'Only images files',
),
i use this plugin for image upload https://github.com/josegonzalez/cakephp-upload.
Your notEmpty rule doesn't appear to do anything as you haven't specified a rule so can probably be removed.
You need to add the 'allowEmpty' => true to the extension rule to tell Cake to ignore the rule if the field is empty:-
public $validate = array(
'image' => array(
'extension' => array(
'rule' => array('extension', array('png','jpg','jpeg')),
'message' => 'Only images files',
'allowEmpty' => true
)
)
);
Update
The CakePHP Upload plugin has its own extension validation rule, isValidExtension, that you can use:-
public $validate = array(
'image' => array(
'extension' => array(
'rule' => array(
'isValidExtension',
array('png','jpg','jpeg'),
false
),
'message' => 'Only images files'
)
)
);
Passing false as the third parameter of the rule (as above) ensures that the rule is only checked when a file is uploaded.
Related
I want to get only triggered messages, but I am getting all registred messages.
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
),
array(
'name' => '\Zend\Validator\File\Extension',
'options' => ['extension' => 'png,jpg,jpeg', 'message' => 'Image extension has to be png,jpg or jpeg.'],
),
array(
'name' => '\Zend\Validator\File\Size',
'options' => ['max' => '2MB', 'message' => 'Maximum file size for image is 2MB.'],
),
),
));
later in controller:
if(!$filter->isValid()){
var_dump($filter->getMessages());
}
If I try to upload image of 5MB size I am getting all messages:
array(
'image' => array(
'fileIsImageNotReadable' => 'File has to be valid image'
'fileExtensionNotFound' => 'Image extension has to be png,jpg or jpeg'
'fileSizeNotFound' => 'Maximum file size for image is 2MB'
)
);
But expect only "Maximum file size for image is 2MB".
Is there any way to return only triggered messages?
Does that should be default behavior of the getMessages() method?
A possible solution for that is to use the Validator Chains.
In some cases it makes sense to have a validator break the chain if its validation process fails. Zend\Validator\ValidatorChain supports such use cases with the second parameter to the attach() method. By setting $breakChainOnFailure to TRUE, the added validator will break the chain execution upon failure, which avoids running any other validations that are determined to be unnecessary or inappropriate for the situation.
This way, validation stops at the first failure and you'll only have the message where validation fails. You could also set priorities so that your validators would be applied in a particular order. This example given on the documentation uses the method attach. This is not what you need exactly.
In your case, you could just use the break_chain_on_failure key in your validator spec with a value set to true. Something like this :
$inputFilter = $factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => ['message' => 'File has to be valid image.']
'break_chain_on_failure' => true,
),
),
));
I have a form on my homepage from an element in my Leads plugin. I have this in my Lead model:
public $validate = array(
'last_name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
),
),
'phone' => array(
'rocket' => array(
'rule' => array('phone', null, 'us')
),
),
);
My app is validating correctly because it's not posting to my database unless I fill in required fields. The problem is that the because my url isn't /leads/add it's not showing my custom error messages on my fields when I submit the form.
I think it has something to do with the fact that I'm using an element. Anyone have any ideas or had trouble with this before?
The rule should be notEmpty instead of notempty. What is working right now is the allowEmpty set to false which makes sure the field is not empty using !empty($field) || is_numeric($field).
I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?
I wrote a custom validation method inside my Submission model that basically allows a blank input field, but once someone enters something in it, it'll validate the data entered.
The validation inside my Submission Model looks like this (All other validation rules are working except for 'description'):
var $validate = array(
'title' => array(
'title' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a title'
),
'minLength' => array(
'rule' => array('minLength', 5),
'message' => 'Please make your title longer'
),
'maxLength' => array(
'rule' => array('maxLength', 300),
'message' => 'Your title needs to be shorter'
),
),
'description' => array(
'checkDescription' => array(
'rule' => array('validateDescription'),
'message' => 'Description must be greater than 5 characters'
),
),
'source' => array(
'source' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Enter a valid source URL'
),
'website' => array(
'rule' => 'url',
'message' => 'Please enter a valid source URL'
),
)
);
My method which is also in my Submission model (below the above code) is:
public function validateDescription($data) {
if(empty($data['Submission']['description']))
return true;
if((strlen($data['Submission']['description'])) <= 5)
return false;
}
I'm not sure why this isn't working at all. In my view, I've got this to display the error:
if ($form->isFieldError('Submission.description'))
echo ($form->error('Submission.description', null, array('class' => 'error')));
The only reason I'm trying to do this, is because using the normal validation with required => false and allowEmpty => true along with a minLength and maxLength validation rule weren't behaving how I intended.
Any help would be greatly appreciated! :)
The $data variable passed into the validation method only contains array($fieldname => $value). You're also not returning true for strings over length of 5. Your method should look like this:
public function validateDescription(array $data) {
$value = current($data);
return !$value || strlen($value) > 5;
}
I am trying to get multiple rules to run during an upload validation. One validation is built in and one is custom. The custom one works fine however the built in one is not working. The custom one extension was working on another field earlier just fine. Do I have this setup correct?
var $validate = array(
'description' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'required' => true
),
'title' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'required' => true
),
'Filedata' => array(
'rule' => array('FileExtCheck'),
'message' => 'Please supply a valid type.',
'required' => true
),
'Thumbdata' => array(
'dimensions'=>array(
'rule' => array('dimensions','120','142'),
'message' => 'Your image dimensions are incorrect: 120x142'
),
'extension' => array(
'rule' => array('extension'=>array('jpg','jpeg','png')),
'message' => 'Please supply a valid type.',
'required'=>true
)
)
);
The one I am having issue with is Thumbdata. I want the Thumbdata field to be required and make sure it has correct dimensions and is an image of jpg, jpeg or png. I don't want animated gif's.
I guess, you have a syntax error - unnecessary =>. Should be:
'rule' => array('extension', array('jpg','jpeg','png')),