CakePHP Validation - isUnique OR inList - validation

(New to CakePHP, so this may be an obvious thing, but I was unable to find a solution after searching for a couple of hours - the ambiguous nature of "or", I guess...)
I am trying to modify an existing CakePHP validation rule, which uses the isUnique rule, so that it will validate for values which satisfy either the isUnique rule or an inList rule.
The previous code:
'isUnique' => array(
'rule' => 'isUnique' ,
'message' => "We're sorry, but this QA number is already being used.",
'last' => TRUE,
),
My (faulty) code:
'isUnique' => array(
'rule' => array(
'isUnique' ,
array( 'inList' , array( '111213' , '141516' , '171819' , '202122' ) )
) ,
'message' => "We're sorry, but this number is already being used.",
'last' => TRUE,
),
So, (as simple as this may be), how can you string CakePHP validation rules together with an "OR" logical operator? I can see that you can apply a cascade of "AND" rules (with each testing for a specific issue and, if failing that test, rejecting the value), but "OR" rules have me scratching my head...
Any help appreciated.

You'll have to make that a custom validation rule. Add this in your model:
public function isUniqueOrInList(array $data, array $list) {
return in_array(current($data), $list) || $this->isUnique($data);
}
Then declare your rule like:
'rule' => array('isUniqueOrInList', array('111213', '141516', '171819', '202122'))

Related

Handling CodeIgniter form validation (rule keys and data types)

Okay, so I've been searching for a while this question, but couldn't find an answer (or at least some direct one) that explains this to me.
I've been using CodeIgniter 3.x Form Validation library, so I have some data like this:
// Just example data
$input_data = [
'id' => 1,
'logged_in' => TRUE,
'username' => 'alejandroivan'
];
Then, when I want to validate it, I use:
$this->form_validation->set_data($input_data);
$this->form_validation->set_rules([
[
'field' => 'id',
'label' => 'The ID to work on',
'rules' => 'required|min_length[1]|is_natural_no_zero'
],
[
'field' => 'username',
'label' => 'The username',
'rules' => 'required|min_length[1]|alpha_numeric|strtolower'
],
[
'field' => 'logged_in',
'label' => 'The login status of the user',
'rules' => 'required|in_list[0,1]'
]
]);
if ( $this->form_validation->run() === FALSE ) { /* failed */ }
So I have some questions here:
Is the label key really necessary? I'm not using the Form Validation auto-generated error messages in any way, I just want to know if the data passed validation or not. Will something else fail if I just omit it? As this will be a JSON API, I don't really want to print the description of the field, just a static error that I have already defined.
In the username field of my example, will the required rule check length? In other words, is min_length optional in this case? The same question for alpha_numeric... is the empty string considered alpha numeric?
In the logged_in field (which is boolean), how do I check for TRUE or FALSE? Would in_list[0,1] be sufficient? Should I include required too? Is there something like is_boolean?
Thank you in advance.
The "label" key is necessary, but it can be empty.
The "required" rule does not check length, nor does the "alpha_numeric". It checks that a value is present, it does not check the length of said value. For that, there is min_length[] and max_length[].
If you're only passing a 0 or 1, then this is probably the easiest and shortest route.

cakephp 2.x include userExists in validating login

I'm new to CakePhp, I'm using CakePhp 2.x.
I am probably going about solving the problem below the wrong way. And I just know I'm overlooked something real simple but,.....
I'm validating login details based on 'Between 5 to 15 characters' they are retuning errors as expected.
[The MODEL]
public $validate = array(
'username' => array(
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
)
);
[The CONTROLLER]
public function login() {
if ($this->request->data) {
$this->User->set($this->request->data);
if ($this->User->validates() && $this->Auth->login()) {
if ($user = $this->Auth->user()) {
$this->render($this->Auth->redirect());
}else{
//??
}
}else{
$this->User->create();
pr($this->User->invalidFields());
$errors = $this->User->validationErrors;
$data = compact('errors');
$this->set('errors', $data);
$this->set('_serialize', array('errors'));
$this->Session->setFlash('Your username/password combination was incorrect');
}
}
}
So, the problem is, if the fields follow the rules in the model above even if the login details (the user) doesn't exist, no errors will be returned (no good). Would it be correct to add an other validation for this, adding another rule to check if that user actually exists? If so how!?
Or, do I work this into the controllers login function checking if the user exists? I'm a little confused now. Maybe I've been looking at the screen for too long.
Thanks.
Would it be correct to add an other validation for this, adding
another rule to check if that user actually exists? If so how!?
You can add as many rules as you want. In this case you want the rule "unique". Read this section of the book about data validation.
Or, do I work this into the controllers login function checking if the
user exists?
All data manipulation and validation should happen in the model layer of the MVC stack. So put everything into a model method and pass the post data to it and validate it there. You can put all logic into the controller to but that's stupid in terms of not following the MVC pattern. Models can be shared between shells and controllers for example, a controller not. Again you could instantiate a controller in a shell but doing all of this negates any benefit and idea the MVC pattern has. Also a model is competitively easy to test. And yes, you should unit test your code. Check how our users plugin is doing it for example.
You can specify multiple rules per field...
Follow this link to learn more about it...
http://book.cakephp.org/2.0/en/models/data-validation.html#multiple-rules-per-field
a sample code is given below
<?php
[IN The MODEL]
//the following code checks if the username is notempty, is a valid email and is it already taken or not...
public $validate = array(
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Please enter a valid email.',
),
'email' => array(
'rule' => array('email'),
'message' => 'Please enter a valid email.',
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
)
);
?>

Needing different approach with validation rule for distinct actions in same Controller

I have a controle to handle some information that can be either saved or updated on DB.
I'm using public $validation to store an array with the validation rules that would look like this:
public $validation = array(
array(
'field' => 'modelname[column1]',
'label' => 'Column 1',
'rules' => 'required'
),
array(
'field' => 'modelname[column1]',
'label' => 'Column 2',
'rules' => 'required'
),
);
and I'm using my own validation function with callbacks in this same $validation. Like this:
array(
'field' => 'modelname[column3]',
'label' => 'Column 3',
'rules' => 'callback_column3|required'
),
array(
'field' => 'modelname[column4]',
'label' => 'Column4',
'rules' => 'callback_column4|required'
),
Which is handled with an action in the Controller.
The problem is that:
For add ( save ) I have to check the uniqueness of the value, that's the function of the callback_column4 ( let's say ) and if it's not unique it returns false. But, I can't return false for the edit (update) because I'm reading and editing something that's, obviously, in the DB.
So, what should I do to distinguish the two different action when validating.
PS: I have already tried to use subarrays with the Class/action ( http://codeigniter.com/user_guide/libraries/form_validation.html#savingtoconfig ) name but I'm using a Core_Model abstraction that plays the role of calling
$this->form_validation->set_rules($this->validation);
$this->form_validation->run()
You could add validation rules for that input in the controller method that is calling it rather than trying to put everything in the same array. That way you can keep your public validation var for all of the other rules but just set the one that is causing issues for you independently.
You could also create two separate arrays for your validation rules and call each one for its respective method. i.e.
$this->form_validation->set_rules($this->create_validation);
and
$this->form_validation->set_rules($this->edit_validation);

CakePHP $this->Plan->validates() Validation from non-model form

I've been at this most of the day now, and I cannot get this working for the life of me (well I can get it 1/2 working but not fully correctly).
Basically, I am trying to use Validation on a search form field like so:
if(isset($search['ApplicantAge']) && !empty($search['ApplicantAge'])) {
if ($this->Plan->validates()) {
$ApplicantAge = $search['ApplicantAge'];
}
}
And here is my model code:
...
'ApplicantAge' => array(
'required' => true,
'allowEmpty' => false,
'rule' => 'numeric',
'message' => 'A valid Age is required. Please enter a valid Age.'),
...
The validation is working BUT when I enter a number (numeric), it displays my error! And when it's blank NO error displays, and when I enter letters it seems to work : ( ??
Does anyone know a trick to this odd behavior?
Try using the 'notEmpty' rule instead of the required/allowEmpty stuff.
'ApplicantAge' => array(
'applicant-age-numeric'=> array(
'rule' => 'numeric',
'message' => 'A valid Age is required. Please enter a valid Age.'
),
'applicant-age-not-empty'=> array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank'
)
)
firstly why are you using the field 'ApplicantAge' when the conventions say its should be lower case under scored?
to answer your question the best way to do validation like that is http://book.cakephp.org/view/410/Validating-Data-from-the-Controller
the other option is to do $this->Model->save($data, array('validate' => 'only'));
The manual did not assist me at all : (
But your suggestion on the validate => only array seems to have done the trick. This is how I got it working:
plans_controller.php
if (isset($search['ApplicantAge'])) {
$this->Plan->save($search, array('validate' => 'only'));
if ($this->Plan->validates($this->data)) {
$ApplicantAge = $search['ApplicantAge'];
}
}
plan.php (model)
var $validate = array(
'ApplicantAge' => array(
'applicant-age-numeric' => array(
'rule' => 'numeric',
'message' => 'A valid Age is required. Please enter a valid Age.'),
'applicant-age-not-empty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank'),
),
Now, if no data is entered in the ApplicateAge field, the proper message is displayed. And if a non-numeric is entered, the correct message is also displayed.
This was a lot more challenging than I thought it would be!
For the record, I'll make a correction to my earlier accepted post. Little did I know the validate => only on the save() was actually still saving data to my plans table.
I was able to get it working using set(). Here is the code that completely solved the problem:
plans_controller.php
if (isset($search['ApplicantAge'])) {
$this->Plan->set($this->data);
if ($this->Plan->validates()) {
$ApplicantAge = $search['ApplicantAge'];
}
}
plan.php (model):
var $validate = array(
'ApplicantAge' => array(
'applicant-age-numeric' => array(
'rule' => 'numeric',
'message' => 'A valid Age is required. Please enter a valid Age.'),
'applicant-age-not-empty' => array(
'rule' => 'notEmpty',
'message' => 'This field cannot be left blank'),
)

Validation rule for a composite unique index (non-primary)

I am sure I am not the first who has composite unique keys in tables and who wants to validate them. I do not want to invent the bicycle so I ask here first. I have several tables that have 'id' columns as primary keys and two other columns as unique composite keys. It would be nice to have a validation rule to check that the submitted entry is unique and display a validation error if it is not. In Cakephp it could be done by a custom validation rule. I am pretty sure somebody has created such method already.
Ideally it would be a method in app_model.php that could be used by different models.
I am using that function:
function checkUnique($data, $fields) {
if (!is_array($fields)) {
$fields = array($fields);
}
foreach($fields as $key) {
$tmp[$key] = $this->data[$this->name][$key];
}
if (isset($this->data[$this->name][$this->primaryKey]) && $this->data[$this->name][$this->primaryKey] > 0) {
$tmp[$this->primaryKey." !="] = $this->data[$this->name][$this->primaryKey];
}
//return false;
return $this->isUnique($tmp, false);
}
basically the usage is:
'field1' => array(
'checkUnique' => array(
'rule' => array('checkUnique', array('field1', 'field2')),
'message' => 'This field need to be non-empty and the row need to be unique'
),
),
'field2' => array(
'checkUnique' => array(
'rule' => array('checkUnique', array('field1', 'field2')),
'message' => 'This field need to be non-empty and the row need to be unique'
),
),
So basically this will show the warning under each of the fields saying that it's not unique.
I am using this a lot and it's working properly.
In the CakePHP/2.x versions released in the last few years, the isUnique rule optionally accepts several columns:
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.'
)
);
I'm not sure of the exact version when the feature was available but there's a bug fixed in core as late as October 2014 filed against 2.3 and 2.4 branches.
You could put it in app model, but my suggestion would just be to add it to the model directly by placing the rule with it's $validate property.
Check out the built in isUnique rule.

Resources