I have a syntax error in validation array in model. Its ok if I write the same code in controller
$validation = array(
'field' => array(
'required' => array(
'rule' => 'notEmpty',
'message' => 'field Can not be empty!'
),
'validRange' => array(
'rule' => array('comparison', '>', __('MAX_RANGE'))), //here is the syntax error. MAX_RANGE is a constant which is define in language file
'message' => 'field cant not be greater than maximum range'
)
)
)
If you get these kind of errors, i too will recommend you to start the paranthesis .
Here it is
field will have the array
'field' => array(
'required' => array(
'rule' => 'notEmpty',
'message' => 'field Can not be empty!'
)
validRange has
validRange' => array(
'rule' => array('comparison', '>', __('MAX_RANGE')),
is define in language file
'message' => 'field cant not be greater than maximum range'
)
And you missed the rule inside
'rule' => array('comparison', '>', __('MAX_RANGE')),
So, at the end you will be having
<?php
$validation = array(
'field' => array(
'required' => array(
'rule' => 'notEmpty',
'message' => 'field Can not be empty!'
)),
'validRange' => array(
'rule' => array('comparison', '>', __('MAX_RANGE')), //here the syntax error should have gone :p
'message' => 'field cant not be greater than maximum range')
);
?>
Hello Try This code once
$validation = array(
'field' => array(
'required' => array(
'rule' => 'notEmpty',
'message' => 'field Canenter code here not be empty!'
),
'validRange' => array(
'rule' => array('comparison', '>', __('MAX_RANGE')), //here is the syntax error. MAX_RANGE is a constant which is define in language file
'message' => 'field cant not be greater than maximum range'
)
)
)
Related
I am using CakePHP 2.10.19. I have a form for entering Item Types. I also have other models for entering appropriate database objects. Error I am occurring is that my validation errors are displayed twice for this form. For other forms it works well. Here is the model:
ItemType.php
App::uses('AppModel', 'Model');
class ItemType extends AppModel {
public $classes = array(
'product' => 'Proizvod',
'kit' => 'Kit (bundle)',
'material' => 'Repromaterijal'
);
public $validate = array(
'code' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A code is required'
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'A code must be an alphanumeric value'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This code already exists!'
),
'between' => array(
'rule' => array('lengthBetween', 3, 7),
'message' => 'Code must be between 3 and 7 characters long'
)
),
'name' => array(
'required' => array(
'rule' => 'notBlank',
'message' => 'A name is required'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This name already exists!'
),
'between' => array(
'rule' => array('lengthBetween', 3, 30),
'message' => 'Name must be between 3 and 30 characters long'
)
),
'class' => array(
'valid' => array(
'rule' => array('inList', array('product', 'material', 'kit', 'semi_product', 'service_product', 'service_supplier','consumable','inventory','goods','other')),
'message' => 'Please enter a valid class',
'allowEmpty' => false
)
),
'tangible' => array(
'bool' => array(
'rule' => 'boolean',
'message' => 'Incorrect value for the checkbox'
)
),
'active' => array(
'bool' => array(
'rule' => 'boolean',
'message' => 'Incorrect value for the checkbox'
)
)
);
public $hasMany = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'item_type_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
ItemTypesController.php
<?php
class ItemTypesController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->ItemType->set($this->request->data);
if($this->ItemType->validates()){
debug($this->ItemType->validates());
$this->ItemType->create();
if ($this->ItemType->save($this->request->data)) {
$this->Flash->success(__('The item type has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
debug($this->ItemType->invalidFields());
$this->Flash->error(__('The item type could not be saved. Please, try again.'));
}
}
debug($this->ItemType->invalidFields());
$this->Flash->warning($this->ItemType->validationErrors, array(
'key' => 'negative'
));
}
$this->set('classes', $this->ItemType->classes);
}
}
Also, debug($this->ItemType->invalidFields()) is showing array with two fields for each field, like this:
array(
'code' => array(
(int) 0 => 'Code must be between 3 and 7 characters long',
(int) 1 => 'Code must be between 3 and 7 characters long'
),
'name' => array(
(int) 0 => 'Name must be between 3 and 30 characters long',
(int) 1 => 'Name must be between 3 and 30 characters long'
)
)
...so I am guessing a model is making some sort of mistake.
add.ctp
<div class="itemTypes form">
<?php echo $this->Form->create('ItemType'); ?>
<fieldset>
<legend><?php echo __('Add Item Type'); ?></legend>
<?php
echo $this->Form->input('code');
echo $this->Form->input('name');
echo $this->Form->input('class', array('options' => $classes));
echo $this->Form->input('tangible');
echo $this->Form->input('active');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Anyone has an idea. Thing is, other controllers have basically the same logic but do not have this sort of a mistake where validation errors are displayed twice.
Generally this is because validation is triggered twice. This is triggered twice, once when you call $this->ItemType->validates()) and another time when you call debug($this->ItemType->invalidFields());
Please comment and remove all the debug statements.
Apparently upon removing the debug statements it works like it should with only one validation error per type as it should do. Not sure why debug made problem tho.
I am working on cakephp v2.3. While I am validation data by $this->User->validates it will always return false even if values are there.
Here is my validation rule
public $validate = array(
'first_name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
'required' => true,
),
'size' => array(
'rule' => array('maxLength', 50),
'message' => 'This field must be no larger than 50 characters long.'
),
),
'last_name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
'required' => true,
),
'size' => array(
'rule' => array('maxLength', 50),
'message' => 'This field must be no larger than 50 characters long.',
'allowEmpty' => true,
),
),
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
'required' => true,
),
'checkMail' => array(
'rule' => 'checkMail',
'message' => 'Please provide a valid email address.',
'last' => true,
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Email address already in use.',
'last' => true,
),
),
'password' => array(
'rule1' => array(
'rule' => array('minLength', 6),
'message' => 'Passwords must be at least 6 characters long.',
'required' => true,
),
),
'verify_password' => array(
'rule' => 'validIdentical',
),
'current_pass' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
'match' => array(
'rule' => 'validICurrent',
)
),
'gender' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
),
),
'mobile' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank.',
'last' => true,
'required' => true,
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter valid phone numbers.',
)
),
'image' => array(
'SizeLimit' => array(
'rule' => array('isUnderPhpSizeLimit', false),
'message' => 'File exceeds upload filesize limit',
),
'FormSizeLimit' => array(
'rule' => array('isUnderFormSizeLimit', false),
'allowEmpty' => true,
'message' => 'File exceeds form upload filesize limit'
),
'CompletedUpload' => array(
'rule' => array('isCompletedUpload', false),
'message' => 'File was not successfully uploaded'
),
'ValidExtension' => array(
'allowEmpty' => true,
'rule' => array('isValidExtension', array('gif', 'png', 'jpg', 'jpeg'), false),
'message' => 'File does not have a gif, png, ,jpg and jpeg extension'
)
),
);
and this is how I am validating
if ($this->request->is("post")) {
if (!empty($this->request->data)) {
if (!$this->User->validates($this->request->data)) {
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
}
When I try
die(debug($this->User->invalidFields()));
it will return
array(
'password' => '*****',
'last_name' => array(
(int) 0 => 'This field cannot be left blank.',
(int) 1 => 'This field cannot be left blank.'
),
'email' => array(
(int) 0 => 'This field cannot be left blank.',
(int) 1 => 'This field cannot be left blank.'
),
'mobile' => array(
(int) 0 => 'This field cannot be left blank.',
(int) 1 => 'This field cannot be left blank.'
)
)
This is the data I am passing
Array
(
[User] => Array
(
[role_id] => 2
[first_name] => kapil
[last_name] => sharma
[email] => kapiltest#mailinator.com
[password] => abc#123
[verify_password] => abc#123
[mobile] => 1234567890
[gender] => Male
[image] => Array
(
[name] => Screenshot_61.png
[type] => image/png
[tmp_name] => /tmp/phppYBJQF
[error] => 0
[size] => 50611
)
)
)
Your Validations seems fine to me But if we don't set the data in model some time that will cause this kind of issue.
Add below line before validating.
$this->User->set($this->request->data);
So code should look like
$this->User->set($this->request->data); //set data in model
if (!$this->User->validates()) {
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
I have a form with values that aren't in my database. I do wan't validation on them so I made some validation rules using the field names. Somehow the validation rules aren't being used.
I already tried to set different rules. The file names are correct since the $hasMany connection works.
I hope you can help!
Form:
<?php
echo $this->Form->create('Map');
echo $this->Form->input('min_x');
echo $this->Form->input('max_x');
echo $this->Form->input('min_y');
echo $this->Form->input('max_y');
echo $this->Form->end('Submit');
?>
Validation rules:
public $validate = array(
'min_x' => array(
'rule' => 'Numeric',
'message' => 'Please enter a numeric value.'
),
'max_x' => array(
'rule' => 'Numeric',
'message' => 'Please enter a numeric value.'
),
'min_y' => array(
'rule' => 'Numeric',
'message' => 'Please enter a numeric value.'
),
'max_y' => array(
'rule' => 'Numeric',
'message' => 'Please enter a numeric value.'
),
);
Rules for validation are case sensitive, so:
'rule' => 'Numeric',
should be changed to
'rule' => 'numeric',
for each instance.
You also have an extra comma on the last field's array.
public $validate = array(
'min_x' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter a numeric value.'
)
),
'max_x' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter a numeric value.'
)
),
'min_y' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter a numeric value.'
)
),
'max_y' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter a numeric value.'
)
)
);
At the end of all the messages there must be ','.
'max_x' => array(
'numeric' => array(
'rule' => 'numeric',
'message' => 'Please enter a numeric value.',
)
),
I'm trying to find out how to get the actual message from my validate() array which contains all the rules to validate a submission within my model.
Basically I'm POSTing ajaxily and I'd like to return all of the error messages in the form that have failed validation, but it's sending them anyway even when they have passed validation.
So in my
SubmissionsController I'm doing this:
if ($this->request->is('ajax')) {
$formData = $this->Submission->invalidFields();
$this->set(compact('formData'));
}
In my Submission model I have:
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 (e.g. IJL John F. Kennedy donated his presidential salary to charity)'
),
'maxLength' => array(
'rule' => array('maxLength', 300),
'message' => 'Your title needs to be shorter'
),
),
'description' => array(
'shortDescription' => array(
'rule' => array('shortDescription'),
'message' => 'Your description needs to be longer'
),
'longDescription' => array(
'rule' => array('longDescription'),
'message' => 'Your description needs to be shorter'
),
),
'source' => array(
'source' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
),
'website' => array(
'rule' => 'url',
'message' => 'Enter a valid source URL (e.g. http://en.wikipedia.org/wiki/Penguins)'
),
),
'category' => array(
'category' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please choose a category'
)
)
);
In my Submissions/json/submit.ctp file I have:
<?php
$fragment = $this->element('errors/flash_error');
$toReturn = array(
'formData' => $formData
);
echo json_encode($toReturn);
If I enter in a valid title or any other valid field, I still am getting back the error message instead of nothing.
Is there something I'm missing that invalidFields() needs in order to NOT return fields which HAVE passed validation?
EDIT:
As Leo suggested below, I wasn't calling save before invalidFields()
The correct code should be:
if ($this->Submission->save($this->request->data)) {
$formData = null;
} else {
$formData = $this->Submission->invalidFields();
}
$this->set(compact('formData'));
You're calling invalidFields() without validation either by a save() call or validates()!
I am working on a cakePHP project and have set up my data validation. The problem i have is that i keep getting an error from the core/model of cakePHP.
The error is:
Notice (8): Undefined offset: 0 [CORE/cake/libs/model/model.php, line 2435]
if (is_array($validator['rule'])) {
$rule = $validator['rule'][0];
My validation rules look like this:
var $validate = array(
'name' => array(
'rule' => array('maxLength' => 80),
'required' => true,
'message' => 'Please enter your name'
),
'address1' => array(
'rule' => array('maxLength' => 80),
'required' => true,
'message' => 'You forgot your address'
),
'address2' => array(
'rule' => array('maxLength' => 80),
'message' => 'Your address can\'t be that long?'
),
'city' => array(
'rule' => array('maxLength' => 80),
'required' => true,
'message' => 'Your city can\'t be that long?'
),
'zip' => array(
'rule' => array('postal', null, 'us'),
'required' => true,
'message' => 'Your zip code is not in the corect format.'
),
'phone' => array(
'rule' => array('phone', null, 'us'),
'required' => true,
'message' => 'Your phone number is not in the corect format.'
),
'email' => array(
'rule' => 'email',
'required' => true,
'message' => 'Please enter a valid email address.'
),
'seats' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'You forgot to let us know how many seats you need. If you will not be attending please enter a zero (0)'
),
'seat_with' => array(
'rule' => array('maxLength' => 80),
'message' => 'Please keep this field below 80 charcters.'
),
'cc_name' => array(
'rule' => array('maxLength' => 80),
'required' => true,
'message' => 'Did you forget something?'
),
'cc_number' => array(
'rule' => array('cc', 'all', false, null),
'required' => true,
'message' => 'Your credit card number is not in the correct format.'
),
'cc_expiration' => array(
'rule' => array('date', 'my'),
'required' => true,
'message' => 'The correct answer will be in the following format MM/YYYY'
),
'cc_cvv' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'Numbers only please.'
)
);
Any help is much appreciated.
Your problem is in the rule syntax:
array('maxLength' => 80)
Just like your other rules, it's ,, not =>: array('maxLength', 80).
BTW, my city can be that long: http://en.wikipedia.org/wiki/Krung Thep Mahanakhon Amon Rattanakosin Mahinthara... ;-)