I am working with cakephp. I need to add three validation on email field. First validation if email not given, second for valid email address, third if email address is given then it should be unique. Because its a signup form.
How I have add three validations on one field I try with the following code but it did not work for me.
public $validate = array(
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Invalid email address',
'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
)
),
'email' => array(
'rule' => 'isUnique',
'message' => 'Email already registered'
)
);
You have two identical indexes 'email' which PHP won't allow you. Change to something like:-
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'
)
)
);
Otherwise only one of your rules will be used.
As of cakephp 3.0 in the entities table it should look something like this
namespace App\Model\Table;
public function validationDefault($validator)
{
$validator
->email('email')
->add('email', 'email', [
'rule' => [$this, 'isUnique'],
'message' => __('Email already registered')
])
->requirePresence('email', 'create')
->notEmpty('email', 'Email is Required', function( $context ){
if(isset($context['data']['role_id']) && $context['data']['role_id'] != 4){
return true;
}
return false;
});
return $validator;
}
}
function isUnique($email){
$user = $this->find('all')
->where([
'Users.email' => $email,
])
->first();
if($user){
return false;
}
return true;
}
What version of Cakephp do you use?
Because I think if you use 2.3, it should be:
public $validate = array( 'email' => 'email' );
with the field email in your SQL table set as a primary key.
Related
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.
Folks,
I'm trying to ensure at least one of two fields (last_name or email) is being populated. Each field also has multiple rules. I'm using CakePHP version 2.4.2.
The problem I have at the moment, after multiple permutations of updating and/or moving around the use 'last', 'allowEmpty', 'required', etc, is that the fields just aren't validating at all, or aren't executing all the rules when a field is populated.
Any advice on how to modify the code below to achieve the following behaviour is much appreciated:
1. One of the two fields must be populated;
2. The other rules attached to each field must validate as well (i.e. if a last name is passed, then it must be between 2 and 45 chars and alphanumeric only)
Here's the model code:
public $validate = array(
'last_name'=> array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','last_name'),
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only'
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters'
)
),
'email' => array(
'needOne' => array (
'required' => FALSE,
'allowEmpty' => TRUE,
'last' => TRUE,
'rule' => array('checkOne','email'),
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'last' => TRUE,
'rule' => array('email', FALSE),
'message' => 'A valid Email address is required'
)
)
);
// Ensure at least the last name or email field value is provided
function checkOne($field) {
if(!empty($this->data[$this->User]['last_name']) ||
!empty($this->data[$this->User]['email'])){
return true;
} else {
return false;
}
}
Thanks in advance!
Thanks very much vicocamacho. I kept at it and, in addition to your advice, found the solution also lay in adding a 'required' => false in the view.
Here's the working solution for any one else with this problem:
The model:
public $validate = array(
'last_name'=> array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only',
'allowEmpty' => TRUE
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters',
'allowEmpty' => TRUE
)
),
'email' => array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'rule' => 'email',
'message' => 'A valid Email address is required',
'allowEmpty' => TRUE
)
)
);
// Ensure at least the last name or email field value is provided
public function checkOne($data) {
if(!empty($this->data[$this->alias]['last_name'])
|| !empty($this->data[$this->alias]['email'])) {
return TRUE;
} else {
return FALSE;
}
}
The view/fields (I'm using Bootstrap):
echo $this->Form->input('last_name', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Last Name <span class="one-required">*</span></label>',
'class' => 'form-control',
'placeholder' => 'Last Name',
'div' => 'form-group col-sm-12',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'class' => 'alert alert-danger'
)
)
)
);
echo $this->Form->input('email', array(
'required' => false,
'fieldset' => false,
'label' => false,
'before' => '<label class="control-label">Email <span class="one-required">*</span></label>',
'after' => '',
'class' => 'form-control',
'div' => 'form-group col-sm-12 col-xs-12',
'error' => array(
'attributes' => array(
'wrap' => 'div',
'class' => 'alert alert-danger'
)
)
)
);
Thanks.
Write a custom rule that will check if one of both fields is not empty and use that rule on both fields. And set allowEmpty to true for the other rules.
You should allowEmpty in all the rules that do not check if the fields are present, i updated your code so you can see what i mean, also you need to change the $this->User index in your checkOne to $this->alias otherwise it will always return false:
public $validate = array(
'last_name'=> array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'message' => 'Alphabets and numbers only',
'allowEmpty' => true
),
'between' => array(
'rule' => array('between', 2, 45),
'message' => 'Between 2 to 45 characters',
'allowEmpty' => true
)
),
'email' => array(
'needOne' => array (
'rule' => 'checkOne',
'message' => 'You must enter at least a contact name or email address.'
),
'emailAddress' => array (
'allowEmpty' => true,
'rule' => 'email',
'message' => 'A valid Email address is required'
)
)
);
// Ensure at least the last name or email field value is provided
function checkOne($field) {
if(
!empty($this->data[$this->alias]['last_name']) ||
!empty($this->data[$this->alias]['email'])
)
{
return true;
} else {
return false;
}
}
Say I have a fairly typical, by-the-docs validation array and I want to keep those rules and add a specific validation rule for 8+ other fields.
Original $validate:
var $validate = array(
'name' => array(
'notEmpty'=> array(
'rule' => 'notEmpty',
'message' => 'Name can not be blank.'
),
'allowedCharacters'=> array(
'rule' => '|^[a-zA-Z ]*$|',
'message' => 'Name can only be letters.'
),
'minLength'=> array(
'rule' => array('minLength', 3),
'message' => 'Name must be at least 3 characters long.'
),
'maxLength'=> array(
'rule' => array('maxLength', 255),
'message' => 'Name can not be longer that 255 characters.'
)
),
'email' => array(
'email' => array(
'rule' => 'email',
'message' => 'Please provide a valid email address.'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This E-mail used by another user.'
)
)
);
I can add the following rule to the validation array, but I do not want to have to repeat it 8 times.
////fragment of array
'field1' => array(
'greaterThanField' => array(
'rule' => array('greaterThanField', 'age'),
'message' => 'This field cannot be greater than age'
)
)
////
function greaterThanField( $field=array(), $compare_field=null ){
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 > $v2) {
return false;
} else {
continue;
}
}
return true;
}
And I want to add to this an array of field names that will all be evaluated using the same rule and message. I tried creating a variable in the model and then running code on the beforeValidate function, but beforeValidate doesn't seem to have access to the compareFields variable
var $compareFields = array('field1', 'field2', 'field3', 'field4', 'field5', 'field6', 'field7', 'field8');
function beforeValidate(){
foreach ($compareFields as $field) {
$validate[$field] = array(
'rule'=> array('greaterThanField', 'age' ),
'message'=>'Cannot exceed delivery age',
);
}
}
What is the proper way to achieve this?
If you're using 2.2 then check this.
If you're using an older version and (if I understood correctly your question) you are too lazy to add the same validation rule to multiple fields, then I would add them dynamically in the constructor of your model. I don't like that solution though, I would stick to the convention.
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;
}
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.