I have a site developed in cakephp.
I have a model called User like this:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'not_empty' => array(
'rule'=> 'notEmpty',
'message'=> 'Username not empty'
)
),
'email' => array(
'email_invalid' => array(
'rule' => 'email',
'message' => 'Invalid mail'
),
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database'
)
)
);
public function beforeSave(){
if (isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
}
}
Into my validate I have the rules email_unique that check if inside the database is already present another email equal.
When I update a user I make this inside my controller:
$this->User->id = $this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
$this->redirect (array ('action'=>'index'));
}
else{
$this->Session->write('flash_element','error');
$this->Session->setFlash ('Error');
}
It always fail because email isn't unique but is the same record!
I would like to know what is the best method to escape the validation if the save is an update not a create?
Or something like: check if the page is edit escape validation or I don't know.. maybe there are many system, I would like to know what is the more correct for my problem.
Thanks
You can adjust your validation rules to only apply when a new record is created, not when an existing record is updated. You can do this by setting the on key in your validation rule to create, so it will look like this:
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database',
'on' => 'create' // Only apply this rule upon creation of a new record
)
See the documentation on this for further details.
If you also want to block duplicate e-mails upon updating, create a beforeSave method in your User model, looking for the e-mail address:
public function beforeSave($options = array()) {
// If the email key is set in the data to be saved...
if (isset($this->data[$this->alias]['email'])) {
// Make sure the email is not already in use by another user
if ($this->find('count', array(
'conditions' => array(
$this->alias . '.id !=' => $this->data[$this->alias]['id'],
$this->alias . '.email' => $this->data[$this->alias]['email']
)
)) > 0) {
// The email is found for a user with another id, abort!
return false;
}
}
}
Related
I'm working with a Cake PHP 2 project and am utilising the model validation and have implemented validation into my controller
I've set up my model to contain a $validate array with my fields and some basic rules, but when validating the request data with the validates method, the validation is failing for some reason.
My model looks ike:
<?php
class MyModelName extends AppModel
{
/*
** Model name
*/
public $name = 'MyModelName';
/*
** Table prefix
*/
public $tablePrefix = 'tlp_';
/*
** Table to save data to
*/
public $useTable = 'some_table';
/*
** Validation
*/
public $validate = array(
'age' => array(
'required' => array(
'rule' => 'required',
'message' => 'This field is required'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'This field can only be numeric'
)
)
);
}
And then I'm validating like this...
$data = [
'age' => $this->request->data['age'] ?? null,
];
$this->MyModelName->set($this->request->data);
if (!$this->MyModelName->validates()) {
$json['errors'] = $this->MyModelName->validationErrors;
echo json_encode($json);
exit();
}
$hop = $this->MyModelName->save($data);
Why would $this->MyModelName->validates() always be returning false? I've checked that age is in $this->request->data and it is, and it's absolutely a numeric value.
$this->request->data looks like the following:
{
"age": 50
}
Am in a need to validate form fields and manipulate them with out saving to database.
This is what i have done
In controller
<?php
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\View;
class UsersController extends ControllerBase {
public function loginAction() {
if($this->request->isPost()) {
$user = new Users();
$validates = $user->validation($this->request->getPost());
// now validation works fine, but cancelOnFail in model doesn't seems to work,
if($validates) {
echo 'valid inputs';
}
else {
print_r($user->getMessages());
// now how can we show these error messages below the corresponding input fields in the view.
// we would also like to show error message as follows, if a field has more than one validation conditions,
// Eg: say username have notempty and valid e-mail validation set in model so if username is empty show only not empty message,
// similarly if username is not empty and if its not a valid e-mail , show not valid email message.
}
exit();
}
}
}
?>
Am trying to validate from the Model and it looks like as follows
<?php
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\Validator\PresenceOf;
use Phalcon\Mvc\Model\Validator\Email;
class Users extends \Phalcon\Mvc\Model {
public function validation() {
$this->validate(new PresenceOf(
array(
'field' => 'username',
'message' => 'Username is required.',
'cancelOnFail' => true
)
));
$this->validate(new Email(
array(
'field' => 'username',
'message' => 'Username must be a valid e-mail.'
)
));
$this->validate(new PresenceOf(
array(
'field' => 'password',
'message' => 'Password is required.'
)
));
return $this->validationHasFailed() != true;
}
}
?>
My view file is as follows
<?php
echo $this->tag->form(array("users/login", "role" => "form"));
echo $this->tag->textField(array('username', 'class' => 'form-control', 'placeholder' => 'E-mail', 'type' => 'email', 'tabindex' => 1));
echo $this->tag->passwordField(array('password', 'class' => 'form-control', 'placeholder' => 'Password', 'type' => 'password', 'tabindex' => 2));
echo $this->tag->submitButton(array('Login','class' => 'btn btn-sm btn-success btn-block', 'tabindex' => 5));
?>
</form>
How can i achieve the following,
1) Check if form fields validates correctly as given in the Model from the Controller.
2) Am not looking to save the form data, only validate it.
3) Show the corresponding error messages below the input field in the view.
Thankz
You need to create the form, bind your entity and then validate on post request. See http://docs.phalconphp.com/en/latest/reference/forms.html#validation
EDIT: To display error messages you can do this in your controller
// ...
$messages = array();
foreach ($user->getMessages() as $message) {
$messages[$message->getField()] = $message->getMessage();
}
$this->view->messages = $messages;
//...
now you have $messages in your view.
I think you really should use a form in this case. You trying to validate a user login in model, but is a simple form validation. In model, you validates business rules of users in the app.
I have a model Interesttype where i want two fields to be validated one should not be more than the other and none should be less than a particular set value. Here is my model.
class Interesttype extends AppModel
{
public $primaryKey = 'int_id';
public $displayField = 'int_name';
public $hasMany= array(
'Loan' => array(
'className' => 'Loan',
'foreignKey' => 'lon_int_id'
)
);
public $validate = array(
'int_name'=> array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'The interest type name is required.'
),
'int_active'=>array(
'rule'=>array('boolean'),
'allowEmpty'=>false,
'message'=>'Please select the status of this interest type'
),
'int_max'=> array(
'numeric'=>array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Please specify a valid maximum interest rate.'
),
'comparison'=>array(
'rule' => array('comparison','>',1000),
'allowEmpty' => false,
'message' => 'The Maximum interest rate cannot be less than the special rate.'
),
'checklimits'=>array(
'rule' => array('checkRateLimits','int_min'),
'allowEmpty' => false,
'message' => 'The Maximum interest rate cannot be less than the minimum rate.'
)
),
'int_min'=> array(
'numeric'=>array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Please specify a valid minimum interest rate.'
),
'comparison'=>array(
'rule' => array('comparison','>',1000),
'allowEmpty' => false,
'message' => 'The Minimum interest rate cannot be less than the special rate.'
))
);
function checkRateLimits($maxr,$minr){
if($maxr>=$minr){
return true;
}
else{
return false;
}
}
}
the above model validates my forms well except that one check will not be done, it will not check if the maximum interest rate is indeed more than or equal to the minimum inerest rate.
Where am i going wrong on the validation?
You'll have to add your own Validation Method to make this possible. Here's a pretty generic example that makes use of Validation::comparison() and supports all of its operators (>, <, >=, <=, ==, !=, isgreater, isless, greaterorequal, lessorequal, equalto, notequal) as the second argument in the options array and a field name to compare the validated value to as a third parameter.
class MyModel extends AppModel
{
/* Example usage of custom validation function */
public $validate = array(
'myfield' => array(
'lessThanMyOtherField' => array(
'rule' => array('comparisonWithField', '<', 'myotherfield'),
'message' => 'Myfield value has to be lower then Myotherfield value.',
),
),
);
/* Custom validation function */
public function comparisonWithField($validationFields = array(), $operator = null, $compareFieldName = '') {
if (!isset($this->data[$this->name][$compareFieldName])) {
throw new CakeException(sprintf(__('Can\'t compare to the non-existing field "%s" of model %s.'), $compareFieldName, $this->name));
}
$compareTo = $this->data[$this->name][$compareFieldName];
foreach ($validationFields as $key => $value) {
if (!Validation::comparison($value, $operator, $compareTo)) {
return false;
}
}
return true;
}
}
This is generic, so you could throw the function in your AppModel if you want to make use of it in multiple models in your App. You could also make this a Behavior to share it between different models.
I won't help you to do your code however I will suggest you to read the following backery article http://bakery.cakephp.org/articles/aranworld/2008/01/14/using-equalto-validation-to-compare-two-form-fields
<?php
class Contact extends AppModel
{
var $name = 'Contact';
var $validate = array(
'email' => array(
'identicalFieldValues' => array(
'rule' => array('identicalFieldValues', 'confirm_email' ),
'message' => 'Please re-enter your password twice so that the values match'
)
)
);
function identicalFieldValues( $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;
}
}
?>
modify the code I think it should be easy from here.
I don't think you can achieve this with directly inserting it into the validate array in a model. You can however specify your own validation function. An example is given here (look at the model code):
Example custom validation function
In your Model your validate should be field name with rule pointing to the function you want to run.
And "None should be less than a particular set value" I would use the "range" validation as seen below. The example will accept any value which is larger than 0 (e.g., 1) and less than 10
public $validate = array(
'maxr'=>array(
'Rate Limits'=>array(
'rule'=>'checkRateLimits',
'message'=>'Your Error Message'
),
'Greater Than'=>array(
'rule'=>array('range', -1, 11),
'message'=>'Please enter a number between 0 and 10'
)
),
);
In the function you pass $data which is the fields being passed. If $data doesn't work try $this->data['Model']['field'] ex($this->data['Model']['maxr'] >= $this->data['Model']['minr'])
function checkRateLimits($data){
if ( $data['maxr'] >= $data['minr'] ) {
return true;
} else {
$this->invalidate('minr', 'Your error message here');
return false;
}
}
Your Form would be something like
echo $this->Form->create('Model');
echo $this->Form->input('maxr');
echo $this->Form->input('minr');
echo $this->Form->end('Submit');
I'm writing a customer management system using cakePHP (the first thing I've ever attempted to build using this framework), and I'm struggling to figure out how to validate certain fields when adding a new customer.
Each customer has an id that I need to be able to add manually, and a username that has to be unique but can be empty.
Here is what I want to happen:
When adding a new customer:
Check whether the id already exists and alert the user if it does (and not add the user)
Check whether the username already exists and alert the user if it does (and not add the user)
When updating a customer profile (the id cannot be modified at this point):
If the username has been modified, check whether it already exists and alert the user.
As it now stands, whenever I try adding a user with an existing id, cakePHP simply overwrites the existing id's info with the new info.
I've tried several validation options but nothing seems to work.
Here's the first:
public $validate = array(
'id' => array(
'idRule-1' => array(
'on' => 'create',
'rule' => 'uniqueID',
'message' => 'This Oracle ID already exists.',
'last' => false
),
'idRule-2' => array(
'rule' => 'numeric',
'message' => 'Oracle ID can only contain numbers.',
'last' => false
),
),
'username' => array(
'userRule-1' => array(
'on' => 'create',
'rule' => 'uniqueUser',
'message' => 'This username already exists',
'last' => false,
),
'userRule-2' => array(
'on' => 'update',
'rule' => 'oneUser',
'message' => 'The eBay username you are trying to modify already belongs to another seller',
'last' => false,
),
)
);
public function uniqueID() {
return ($this->find('count', array('conditions' =>array('Seller.id' => $this->data['Seller']['id']))) == 0);
}
public function uniqueUser() {
return ($this->find('count', array('conditions' =>array('Seller.username' => $this->data['Seller']['username']))) == 0);
}
public function oneUser() {
return ($this->find('count', array('conditions' =>array('Seller.username' => $this->data['Seller']['username']))) == 1);
}
and the second (only for the id):
public $validate = array(
'id' => array(
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'This Oracle ID already exists.',
)
)
);
And here are the add() and edit() methods of the controller:
public function add() {
if ($this->request->is('post')) {
$this->Seller->create();
if ($this->Seller->save($this->request->data)) {
$this->Session->setFlash(__('The seller has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
}
}
$accountManagers = $this->Seller->AccountManager->find('list');
$primaries = $this->Seller->Primary->find('list');
$thirdParties = $this->Seller->ThirdParty->find('list');
$sites = $this->Seller->Site->find('list');
$meetings = $this->Seller->Meeting->find('list');
$this->set(compact('accountManagers', 'primaries', 'thirdParties', 'sites', 'meetings'));
}
/**
* edit method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function edit($id = null) {
if (!$this->Seller->exists($id)) {
throw new NotFoundException(__('Invalid seller'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Seller->id = $id;
if ($this->Seller->save($this->request->data)) {
$this->Session->setFlash(__('The seller has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The seller could not be saved. Please, try again.'));
}
debug($this->Seller->validationErrors);
} else {
$options = array('conditions' => array('Seller.' . $this->Seller->primaryKey => $id));
$this->request->data = $this->Seller->find('first', $options);
}
$accountManagers = $this->Seller->AccountManager->find('list');
$primaries = $this->Seller->Primary->find('list');
$thirdParties = $this->Seller->ThirdParty->find('list');
$sites = $this->Seller->Site->find('list');
$meetings = $this->Seller->Meeting->find('list');
$this->set(compact('accountManagers', 'primaries', 'thirdParties', 'sites', 'meetings'));
}
Any and all tips will be greatly appreciated!
If the data you are trying to save contains an id, CakePHP assumes that you are updating an existing record. For a validation rule to be triggered on "create", the id should not be set. If the id is set, Cake will check for "update" rules.
So you should let your database affect ids for new records with an autoincrement, and then only manage validation when editing your existing users.
I'm trying to set up validation for user registration but I'm having troubles. When I only have the email,role and password fields in the $validation array (remove the others) it works and will save a new user. When I try to add the other fields it fails and gives the flash message error "The user could not be saved. Please, try again."
I'm pretty sure it's the re_password check. When I remove the validation for that it works. However, the re_password validation does display an error when the passwords are different, so I'm not sure where to look
Here's my users table
id | email | password | location | website | role | created | modified
Here's the validation requirements. To get it to save a new user I have to remove everything but email, password and role.
public $validate = array(
'email' => 'email'
,
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
)
),
're_password' => array(
'required' => array(
'rule' => array('equalTo', 'password' ),
'message' => 'Both password fields must be filled out'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'location' => array(
'valid' => array(
'rule' => array('notEmpty'),
'message' => 'Please select a location'
)
)
);
Here's the form (the options array is above, figured it's not necessary to show)
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'', 'autocomplete'=>'off'));
echo $this->Form->input('location', array('options' => $options, 'label' => 'Select Nearest Location'));
echo $this->Form->input('website',array('label'=>'Enter your website, such as www.example.com. '));
echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user'));
Here's the re_password checking function in the User model
function check_user_password($userid) {
$salt = Configure::read('Security.salt');
$this->User->id = $userid;
$hashed_password = $this->User->field('password');
// check password
if($hashed_password == md5($data['User']['re_password'].$salt)) {
return true;
} else {
return false;
}
}
And finally, here's the add function in UsersController
public function add() {
if ($this->request->is('post')) {
$this->User->create(); //create initiates a form on User/add.ctp
if ($this->User->save($this->request->data)) { //save the form data
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('controller' => 'demos', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
Please let me know if there's anything else you need to see
I believe that your re_passwords valiadtion rule equalTo compares its value to string password and not the actual field. I like to use custom functions for this.
so try replacing re_passwords rule array
//'rule' => array('equalTo', 'password' ),
'rule' => array('equalToField', 'password'),
and declare equalToField function in that model
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
** Also in the future when you seem to have a problem with validation rules
try this in your controllers action (its faster than removing every single rule)
if ($this->User->save($this->request->data)) {
...
} else {
debug($this->User->validationErrors);
...
}
I hope this helps.
Hi Please use following code for your requirement :
override equalTo function by putting your own method in user model:
function equalTo( $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;
}
Attention, in #luboss answer, where he declares:
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
That cannot work as we are comparing inconsistent fields:
the left member of strcmp has already been hashed, but not the right member.
This happens as a CakePHP automation because the field is called password.
The way I got this to work was to reuse the hashing function in the equalToField helper:
public function equalToField($array, $field) {
$valueFirstOccurrence = $this->data[$this->alias][$field];
$valueSecondOccurrence = Security::hash($this->data[$this->alias][key($array)], $type = 'sha1', $salt = true) ;
return !strcmp($valueFirstOccurrence, $valueSecondOccurrence);
}
Other point :
If you are interested in adding a minLength validation field for your password field, you want to read this good post first:
minLength data validation is not working with Auth component for CakePHP