CakePHP validation on => 'create', on => 'update' - cakephp

I'm hoping you cakephp experts can answer this re Cake 2.1 and data validation in the model.
Cake gives you an "on" key for use in the validate array. I understand what the docs say about this but my question is, what's the point of these two items.
Let's say I have a validation rule for when a record is created. The validation passes and the record is created.
Then the user goes and edits that record and changes it to something that no longer passes that particular validation. But since I've got my validation set to run on create only, the validation passes and the record is updated with invalid data. It seems to me like this would apply to any on create / on update rules. If a user wanted to bypass validation, just create a valid record, then go and edit it so it's now invalid.
Can someone perhaps help me understand when it might make sense to use on update and on create?

This is most useful in conduction with the required rule. You should set certain fields that are minimally required to be required 'on' => 'create'. This makes the rule fail if those fields don't exist in the data set and the record cannot be created, but allows you to update existing records without having to pass that field each and every time.
For example:
'email' => array(
'required' => array(
'on' => 'create',
'rule' => 'notEmpty',
'message' => 'Enter your email address',
'required' => true,
'last' => true
),
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Enter your email address',
'allowEmpty' => false,
'last' => true
),
'email' => array(
'rule' => 'email',
'message' => 'Not a valid email address',
'last' => true
)
)

I found it really useful in cooperation with profile pictures. You need one to be uploaded on create (if it is needed), but on update i can remain empty => no update to picture will be done.

'on' => null will do the job
EDIT :
Ok, let's say you have a Profile model.
If you have a Date of Birth field, you would probably use a rule that would be enforced for both addition and edition.
You might also add a delete_picture checkbox in your form that a user should select in order to delete their profile picture. When adding, you have no profile picture so this field is only relevant at edition time and you would then use on => update.
Let's say you also have a group field that is set when you create the object but that is meant to never be changed. You would then use on => create.

Related

Auto Increment number on SuiteCRM Accounts

I tried several answers from suitecrm forum And here. And I can't implement them to the present version of Suite CRM.
Here is the one I tried last and stuck for further clarification.
add a new file in custom/Extension/modules/yourmodule/Ext/Vardefs e.g. autoincrement.php with the following :
<?php
$dictionary['YOUR_MODULE']['fields']['NAME_OF_AUTO_INC_FIELD'] = array(
'name' => 'NAME_OF_AUTO_INC_FIELD',
'vname' => 'LBL_LABEL_OF_AUTO_INC_FIELD',
'type' => 'int',
'readonly' => true,
'len' => 11,
'auto_increment' => true,
'disable_num_format' => true,
);
?>
and also add unique index for the field in that file
<?php
$dictionary['YOUR_MODULE']['indices']['NAME_FOR_INDEX'] = array(
'name' => 'NAME_FOR_INDEX',
'type' => 'unique',
'fields' => array('NAME_OF_AUTO_INC_FIELD'),
);
?>
Run a Quick Rebuild and Repair in Admin -> Repair and execute the changes.
after that it shows an empty text box. There it iterates automatically, when new account saved. But I want to show the next auto increment number here in this Accounts page itself.
Instead of default value, I want to show the next auto value in the new Account form.
This isn't out the box behaviour - you'll need to add some customisations to do this.
I would create a new vardef auto_inc_preview which is a function type field. This can then be used to grab the largest number from the DB and display this + 1.
A possible issue with this would be that the number a user sees may not be the id that gets generated - for example if two or more people create an account at the same time.

How to write validation in cakephp php model part

There is a table location containing city and name columns.
Query is:
$count=$this->Location->find("all",array("conditions" => array("Location.city" => '$city',"Location.area" => '$location')));
If condition becomes true, it has to show an error message.
In Model
role=>"unique"
option is there, but it is for only one column.
But in above query its depends on two columns. How to write validation for this?
Have you tried to read the documentation?
You can validate that a set of fields are unique by providing multiple fields and set $or to false:
public $validate = array(
'email' => array(
'rule' => array('isUnique', array('email', 'username'), false),
'message' => 'This username & email combination has already been used.'
)
);
Make sure to include the original field in the list of fields when making a unique rule across multiple fields.
If a listed field isn’t included in the model data, then it’s treated as a null value. You may consider marking the listed fields as required.
You can simple define a validation for the city field in the City model.
public $validate = array(
/* Other fields */
...................
'city' => array(
'rule' => array('isUnique', array('city', 'area'), false),
'message' => 'City and area combination already exists.'
)
);

cakephp date 'dmy' validdation is not working

I want to validate with dmy format. Here is what I wrote in my model:
'birthdate' => array(
'date' => array(
'rule' => array('date','dmy'),
'message' => 'Solo data valida',
'allowEmpty' => true,
)
)
However, when I submit the form with the date, I get an error.
One thing you should verify is the value of "birthday" DIRECTLY PRIOR to validation. Try debugging $this->request->data in your controller before calling $this->Model->save, or $this->Model->validate. It's quite possible that you are transforming that date value before you pass it to validation, and thus it could be failing.
For instance, I typically use a beforeSave, and afterFind callback to transform dates going into and coming from MySQL, or you may have something in your appController or beforeFilter callback that's inadvertently transforming the data.

CakePHP: Deleting a User deletes Users associated via belongsTo

I want to be able to delete a User, but a User has a Manager:
var $belongsTo = array(
'Manager' => array(
'className' => 'User',
'foreignKey' => 'manager_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
And whenever a User is deleted, all of it's "children" are deleted too.
For example, say I delete User A. User A is the manager of users B, C, and D. When A is deleted, so are B, C, and D, because they have A as their manager_id.
So my question is - is this supposed to be happening? And is there a way I can prevent this from happening?
Thanks!
Read the book, that is intended behavior and you can stop it by adding
'dependent' => false,
to the associations configuration array.
See http://book.cakephp.org/1.2/en/view/78/Associations-Linking-Models-Together and search for "dependent" on this page.
And I recommend you to use 2.0 if it's a new project, 1.2 is deprecated for a long time now.
It was an ACL issue - totally unexpected. Since our users operate in a tree structure with the Manager, the User has a lft and rght field that is only updated in the afterSave. The easy solution is to dissociate the user by setting their lft and rght to 0, but after deeper thought, I am setting their manager_id to NULL and saving it so that the tree reorganizes itself (via the afterSave).
Wow. That was quite the problem.

Having trouble with the CakePHP data validation

i'm currently having some issues by using the cakePHP data validation.
I've tried to use the rule with the argument 'on' => 'created' but it didn't seems to work.
My objective is to check during the creation of the user, to check if the mail is not already existing in my database.
But when it's a edit, i don't want to check this, i mean, if the user don't change the mail, obviously this mail is already existing in the database, but it's just the same then before...
So i don't want to check if the mail, have not been changed.
'mail' => array(
'valid_mail' => array(
'rule' => array('email', true),
'message' => 'Veuillez insérer un email valide.'
),
'isUnique_mail' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Ce mail est déjà utilisé.'
)
)
Hope you guys understand my problem.
Part of your problem might be that you're not specifying the ID of the record that needs to be saved using $this->Model->id = $id so when you call the save (or saveAll) function, it's trying to insert a record.
The isUnique validation function actually will filter out the ID of the current record when updating if it's been set using $this->Model->id. You can see this at the bottom of the function (line 2446).
Personally, I'd leave the isUnique validation on all the time to prevent users from changing their emails to one that already exists once they've registered.

Resources