cakephp not validating select field - validation

I just downloaded a fresh copy of cakephp version 2.3.0 and I am trying to validate a select field of a form that I have just created:
echo $this->Form->input('province_id', array('empty' => '- select -', 'options' => $options));
echo $this->Form->input('username');
And the validation:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'province_id' => array(
'rule' => 'notEmpty',
'message' => 'Select something'
)
);
The username field is being validated correctly, but the province_id is being ignored. What can it be?

You should write like this:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'province_id' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Select something'
)
)
);

It looks like your form helper declarations are fine.
My experience with Cake shows that the validation is screwy when you start to mix the syntax (one of your fields has a nested array, while the other doesn't). I am sure there is some rhyme or reason to what is going on, but I haven't really dived that deep and usually just do trial and error.
Try this:
public $validate = array(
'username' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'A username is required'
),
'province_id' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Select something'
)
);

Better late then never, try this:
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A username is required'
)
),
'province_id' => array(
'required' => array(
'rule' => array('myOwnValidationRule'),
'message' => 'Select something'
)
)
);
function myOwnValidationRule($data)
{
if($data["province_id"] != 'empty')
{
return true;
}
}

Related

CakePHP load model validation rule not working

I am loading a model in different controller.
Controller name in which I am loading different model is - "MembersController" and I am loading "Usermgmt" Model of "UsermgmtController".
"Usermgmt" Model has validation as following-
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a value for email.'
),
'duplicate' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'This email is already exist.'
),
'duplicate1' => array(
'rule' => 'email',
'message' => 'Please enter valid email.'
)
),
'firstname' => array(
'valid' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a value for first name.'
)
),
'username' => array(
'valid' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a value for user name.'
),
'duplicate' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'This user is already exist.'
)
),
'password' => array(
'valid' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a value for password.'
)
),
'confirm_password' => array(
'valid' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a value for confirm password.'
),
'duplicate2' => array(
'rule' => 'matchpassword',
'on' => 'create',
'message' => 'Password must be same.'
)
)
);
And now I am applying and loading model in following way.
$this->loadModel('Usermgmt');
$this->Usermgmt->set($this->data);
if ($this->Usermgmt->validates()) {
if ($this->Usermgmt->save($data, true)) {
$userid = $this->Usermgmt->id;
$this->Session->setFlash('User has been added', 'success');
}
}
But validation are not working and it is inserting empty values.
try uses to load model
Example :
public $uses = array('Usermgmt');
You are using two different instances of $data. namely $this->data and $data
You should be validating the same set of data you want to save, like either of the two following examples, whichever holds your data.
$this->loadModel('Usermgmt');
$this->Usermgmt->set($data);
if ($this->Usermgmt->validates()) {
if ($this->Usermgmt->save($data, true)) {
$userid = $this->Usermgmt->id;
$this->Session->setFlash('User has been added', 'success');
}
}
Or
$this->loadModel('Usermgmt');
$this->Usermgmt->set($this->data);
if ($this->Usermgmt->validates()) {
if ($this->Usermgmt->save($this->data, true)) {
$userid = $this->Usermgmt->id;
$this->Session->setFlash('User has been added', 'success');
}
}
Unless you are requiring to do some other logic after validation and before saving you could do away with explicitly calling validates() and just save the data as save() will automatically validate the data for you. Also, I've noticed you are using $this->data, it looks like you are using CakePHP 2 in which case you need to use $this->request->data instead:-
$this->loadModel('Usermgmt');
if ($this->Usermgmt->save($this->request->data) !== false) {
$this->Session->setFlash('User has been added', 'success');
}
If this still fails then make sure you are not overriding beforeValidate() in your model in a way that would cause validation to break.

Cakephp Form Validation empty indeces

Okay, so, in short, I'm trying to validate a form, as I've done a million times before with no trouble. However, I'm finding that on logging the validation errors, all the invalidated fields have an index in the validationErrors array but the messages are empty.
Yes, I've definitely set the validation messages in the model so I'm unsure why it's empty.
Here are my model validations:
public $validate = array(
'effective_policy_date' => array(
'date' => array(
'rule' => array('date'),
'message' => 'Invalid date format',
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Policy date required',
),
),
'business_address' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Business address required',
),
),
'city' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'City required',
),
),
'zip_code' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Zip code required',
),
),
'state_id' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'State required',
),
),
'contact_person' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Contact person required',
),
),
'phone' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Phone number required',
),
),
'email' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Email address required',
),
'email' => array(
'rule' => array('email'),
'message' => 'Invalid email format',
),
),
);
Here's the output of the validationErrors array after submitting the form with empty fields:
[AccountsRequest] => Array
(
[effective_policy_date] => Array
(
)
[policy_number_one] => Array
(
)
[policy_number_two] => Array
(
)
[policy_number_three] => Array
(
)
[policy_number_four] => Array
(
)
[business_address] => Array
(
)
[city] => Array
(
)
[zip_code] => Array
(
)
[state_id] => Array
(
)
)
For completeness sake, here's my Form->create array I use in the view and the controller action responsible for handling the form submission:
Form create() method
<?php
echo $this->Form->create(
'Request',
array(
'novalidate' => 'novalidate',
'action' => 'new_request',
'inputDefaults' => array(
'label' => false,
'div' => false,
'error' => array(
'attributes' => array(
'wrap' => 'label', 'class' => 'error'
)
)
)
)
);
?>
Controller action
public function new_request()
{
$this->page_id = 'requester_newform';
if($this->request->is('post'))
{
if($this->Request->saveAll($this->request->data, array('deep' => true, 'validate' => 'only')))
{
$this->Session->setFlash('Request saved successfully', 'flash/success');
}
else
{
$this->Session->setFlash('Could not save request. Please correct validation errors', 'flash/error');
}
}
}
You'll see some indeces in the validation array that aren't in the validationErrors, that's simply because I haven't quite finished converting the raw HTML to CakePHP forms.
Problem: The validationErrors array shouldn't be empty. It should contain the messages for the notEmpty rules and as a result there are empty error validation elements on the Form's frontend. Any ideas about what I'm doing wrong?
Aarg, how annoying. I've figured it out and it's a lesson well learnt.
For anyone having a similar issue in the future, make sure that your input fields conform to the relationships of the current form's Model.
For instance, in this example, my form's model is 'Request'. Request hasMany 'AccountsRequest'. So my form inputs were something like:
AccountsRequest.effective_policy_date
where it should have been
AccountsRequest.0.effective_policy_date
With this change, my model validation messages are now showing without issue.
I'd still love to know, however, why CakePHP even picked up those fields as invalid and further, if it was intelligent enough to pick up those fields as invalid why it didn't give me validation messages.
Oh well.....

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!!

How do I validate multiple fields from a single location in CakePHP?

I wanna validate multiple fields at one place. So in a form I have included 4 fields
as follows
facebook_link
twitter_link
google_plus_link
linked_in_link
The user atleast type any one field of above. Please help me to get the solution like, the user types anyone of the links in the form.
you may add your own Validation Methods.
public $validate = array(
'facebook_link' => array(
'rule' => array('validateLink'),
'message' => '...'
),
'twitter_link' => array(
'rule' => array('validateLink'),
'message' => '...'
),
'google_plus_link' => array(
'rule' => array('validateLink'),
'message' => '...'
),
'linked_in_link' => array(
'rule' => array('validateLink'),
'message' => '...'
),
);
public function validateLink($link) {
$allFieldsAreEmpty = (
empty($this->data[$this->alias]['facebook_link']) &&
empty($this->data[$this->alias]['twitter_link']) &&
empty($this->data[$this->alias]['google_plus_link']) &&
empty($this->data[$this->alias]['linked_in_link'])
);
return !$allFieldsAreEmpty;
}
Hope this will work for you.
public $validate = array(
'facebook_link' => array(
'rule' => array('customValidation','facebook_link'),
'message' => 'Please enter facebook link.'
),
'twitter_link' => array(
'rule' => array('customValidation','twitter_link'),
'message' => 'Please enter twitter link.'
),
'google_plus_link' => array(
'rule' => array('customValidation'),
'message' => 'Please enter google plus link.'
),
'linked_in_link' => array(
'rule' => array('customValidation'),
'message' => 'Please enter linkedin link.'
),
);
function customValidation($data , $filed) {
if(empty($data[$filed])) {
return false;
}
return true;
}

CakePHP JSON Post validation not working?

I'm using CakePHP 2.1.1 and whilst my methods which use the form helper, for example my add method in my Users Controller validates correctly, I can't get my mobile_register method in the controller to validate, it saves even when data is missing.
This is my mobile_register method:
public function mobile_register() {
if ($this->request->is('post')) {
$jsonData = file_get_contents("php://input");
if(isset($jsonData) && !empty($jsonData)){
$data = json_decode($jsonData,true);
$data = $this->User->configureUser($data);
$this->User->create();
if($this->User->save($data)){
$jsonResponse = array('status' => 'OK', 'token' => $data['User']['token'], 'message' =>'SUCCESS');
}else{
$errors = $this->User->validationErrors;
$this->log($errors,'errors');
$jsonResponse = array('status' => 'ERROR','message' => $errors);
}
}
}else{
$jsonResponse = array('status' => 'ERROR','message' => 'API method expects POST data');
}
$this->set('response' , $jsonResponse);
}
I've been using curl to test my method and it always saves despite post the following data:
[User] => Array
(
[firstname] => Andy
[token] => cc42030bbb49faf2cf0393418036e44f
[role] => user
)
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"User":{"firstname":"Andy"}}' url/users/mobile_register
My validation rules in my model are:
public $validate = array(
'username' => array(
'username-1' => array(
'rule' => array('notempty'),
'message' => 'Username is required',
),
'username-2' => array(
'rule' => 'alphaNumeric',
'message' => 'Usernames must only contain letters and numbers.',
),
),
'password' => array(
'password-1' => array(
'rule' => array('notempty'),
'message' => 'Password is required',
),
'password-2' => array(
'rule' => array('between', 5, 15),
'message' => 'Passwords must be between 5 and 15 characters long.',
),
),
'firstname' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Forename is required'
),
),
'surname' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Surname is required',
),
),
'email' => array(
'email-1' => array(
'rule' => 'notempty',
'message' => 'Email is required',
),
'email-1' => array(
'rule' => 'email',
'message' => 'Please enter a valid email',
)
)
);
Am I missing something?
Perhaps a better approach to this solution would be to make a good use of the "required" and "allowEmpty" validation options:
CakePhp's data validation
This is what solved my problem, I thought it was related with the way I was requesting the action, but instead it was that I wasn't even setting the fields I wanted to validate.
I figured it out. Basically I learnt that if you don't pass the fields then they will not be validated.

Resources