Validation file upload CakePHP - validation

How validate file upload in CakePHP?
When i wrote some like this:var $validate = array(
'file' => array(
'select' => array(
'rule' => array('selectFile'),
'message' => 'There is no file!'),
'type' => array(
'rule' => array('typeFile'),
'message' => 'Bad type!'),
'size' => array(
'rule' => array('sizeFile'),
'message' => 'Bad with size!')));
Works only with the last validate, here 'size'.
Maybe You know solution, for validation for files with many messages?

try adding required=true to each of the 3 rules

Related

Magento issue with adding validation into multi-select checkbox

I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.

CakePHP isUnique doesn't work

I prepare some registration form (the simplest) and in my Model i prepare some validation for email field:
'email' => array(
'mail' => array(
'rule' => array('email', true),
'required' => false,
'message' => 'Not correct e-mail!'),
'unique' => array(
'rule' => 'isUnique',
'message' => 'E-mail was registered!'))
But the rule isUnique doesn't work.
Addition i change in MySQL field email to unique, but still doesn't work.
I use CakePHP 2.3.7
Problem is when we use Translate behavior with model.
The query:
SELECT COUNT(DISTINCT(`User`.`id`)) AS count FROM `sometable`.`users` AS `User` INNER JOIN `sometable`.`i18n` AS `I18nModel` ON (`User`.`id` = `I18nModel`.`foreign_key` AND `I18nModel`.`model` = 'User' AND `I18nModel`.`locale` = 'pl') WHERE `User`.`email` = 'kicaj#kdev.pl'
Field email isn't translated.
Try this.
array(
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Provide an email address'
),
'validEmailRule' => array(
'rule' => array('email'),
'message' => 'Invalid email address'
),
'uniqueEmailRule' => array(
'rule' => 'isUnique',
'message' => 'Email already registered'
)
)
);
I copied your code and pasted it in my Model and it just worked fine!!! I don't think there is any error in it. I just added semicolon after your code. Here is the code that I have checked:
'email' => array(
'mail' => array(
'rule' => array('email', true),
'required' => false,
'message' => 'Not correct e-mail!'),
'unique' => array(
'rule' => 'isUnique',
'message' => 'E-mail was registered!')),
I think there would be some other error!!

CakePHP How to override already declared model validation error messages?

I can successfully put new messages in view files only if they are not present in model. I mean validations have to be in model, but messages attached to those validations should not be there.
Example model
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true, 'allowEmpty' => false,
'message' => 'Please enter a username.'),
'alpha' => array(
'rule' => array('alphaNumeric'),
'message' => 'The username must be alphanumeric.'),
'unique_username' => array(
'rule'=>array('isUnique', 'username'),
'message' => 'This username is already in use.'),
'username_min' => array(
'rule' => array('minLength', '3'),
'message' => 'The username must have at least 3 characters.')),
'email' => array(
...................
...................
and here's an example of view
echo $this->Form->input('username',array(
'label' => __d('app_users','Username'),
'error' => array(
'required' => __d('app_users', 'Please enter a username'),
.....................
.....................
));
So, my question is how can one make it into a work without removing 'message' => 'Please enter a username.' in a model but to TRULY OVERRIDE IT.
I mean validations have to be in model, but messages attached to those validations should not be there.
And how do you then have messages for a JSON API service? The text contained in validation has nothing to do with how your site looks so should not be in a view.
You can change the validation message from the model or from custom validation methods without much hassle and its all in the book

cakephp validation message not showing

i am new to cakephp and am trying to build an app in it. I have some textfields, i wish to have validation on them. I following cakephp tutorials in cakephp.org and did the following but i cant see the validation messages near the textfields. Following is my code:
ctp:
<?php echo $this->Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom)); ?>
model:
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty',
// 'born' => 'date'
);
where am i getting wrong? how do i solve it?
You have used comma in dropat
public $validate = array(
'PickFrom' => array(
'rule' => 'notEmpty',
'message' => 'Cannot leave this field blank.'
),
//'PickFrom' => 'notEmpty',
'DropAt' => 'notEmpty'
// 'born' => 'date'
);
You can use this
Form->text('Rideoffer.PickFrom',
array('class' => 'address-text',
'value' => $dropFrom,
'error'=>'Cannot leave this field blank.')); ?>

cakephp validate translated field

I have a model ex. posts that acts as translate and I want to validate the title which is a translated field.
How is this possible. The below does not work.
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
),
));
Change rule name to camelCase: notEmpty and add message
you translate your message in /Locale default.po
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'your translated message',
),
));
whats the big deal?

Resources