cakePHP "required" validation - cakephp

is there any mistake in this validation???
var $validate = array(
'brand_id' => array(
'required' => array(true),
'message' => array('select a brand'),
)
);
brand_id is a select box
It show error as "message" instead of "select a brand"
if the message is not in array it shows error
Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE\cake\libs\model\model.php, line 2571]
using cakePHP 1.3

You're missing a rule, just required won't do. Use 'notEmpty' as rule if that's what you want. Also, required and message should (must?) not be arrays.

Why do you have arrays everywhere?
var $validate = array(
'brand_id' => array(
'required' => true,
'message' => 'select a brand',
)
);
Refer to:
http://book.cakephp.org/1.3/en/The-Manual/Common-Tasks-With-CakePHP/Data-Validation.html

Related

Cakephp Model Rule

I read from the cakePhp documentation , they only allow one rule per field.
However If my field require unique Date and must be date format. How should I place them ?
Current Code
<?php
App::uses('AppModel', 'Model');
class EventDate extends AppModel {
//public $useTable = 'eventdate';
public $primaryKey = 'event_date_id'; //I still need for Delete
public $validate = array(
'event_date' => array(
'rule' => array('date','ymd'),
'message' => 'Enter a valid date in YY-MM-DD format.',
'required' => true,
'allowEmpty' => false
)
// ,'event_date' => array(
// 'rule' => 'isUnique'
// )
);
}
There is no restriction on the number of rules a field can have in CakePHP. You can provide an associative array of rules for a specific field like this:-
public $validate = array(
'event_date' => array(
'format' => array(
'rule' => array('date','ymd'),
'message' => 'Enter a valid date in YY-MM-DD format.',
'required' => true,
'allowEmpty' => false
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This date already exists'
)
)
);
Just remember to make sure your array indexes are unique.
The array indexes for the individual rules (e.g. format and unique in the example code) can be anything, but make them descriptive to the rule being defined.

MultivalidatableBehavior with cakephp 2.3.0. Not Working

I am using 'MultivalidatableBehavior' in cakephp. Though it worked fine with previous version. But its not working with Cakephp 2.4.0 version. Is their any changes with this behaviour. Because When I use this code.
var $validationSets = array( 'ruleset mentioned in it. ');
Though my request go through the Behaviour class but its not putting validation on the desired field. I have also checked $this->request->data Its also valid and for same Model where ruleset has been written.
I tried to debug using die in my MultivalidatableBehavior class. Though during valiation my request falls in the function of setValidation(&$model, $rules = array()) {. Please suggest if it is compatible with greater version of cakephp2.3.
My tried code..
Model Code :
var $actsAs = array('Multivalidatable');
var $validationSets = array(
'login' => array(
'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Username is required !!')),
'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Password is required !!')),
),
);
Controller Call.
$this->User->setValidation('login'); Fields are also with same name.
Its not validating If I put if($this->User->setValidation('login')) it returns false.
Thanks to Piotr Beschel for helping me out. I shouldn't use any sort of behaviour. It can also be achieved without behaviour. My Model code would be
public $validationAdmin = array(
'adminLogin' => array(
'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your username !!')),
'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your password !!')),
),
'adminForgotPassword' => array(
'email' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your email address !!'),
'email' => array('rule' => array('email'), 'message' => 'Please enter valid mail address')),
),
);
That is simply creating variable named $valdiationAdmin I have named it as admin to know following rule set is for admin.
My sample controller code would be.
$this->Admin->validate = $this->Admin->validationAdmin['adminLogin'];
Just this way. If form has data to save it would move like butter. In case data is just validating I have to force form to validate it(In case login where data is not saved).
if ($this->Admin->validates()) {
} else {
$errors = $this->Admin->validationErrors;
$this->set('validationErrors', $errors);
}
It would validate my form. Even add error message to form field in ctp file. I need not to write $this->form->error('fieldname');
Thanks again to all the viewer. Suggest me something if more better. I am thinking to validate data of other form if associated with one form, with same implementation. Help if you can.
The method
$this->User->setValidation('login')
only chooses which validation Set will be used.
Use:
if ($this->User->validates()) {
...
to get the result of the validation.

CakePHP Validation depending on other field

I wonder if it is possible with CakePHP validation rules to validate a field depending on another.
I have been reading the documentation about custom validation rules but the $check param only contains the value of the current field to validate.
For example. I would like to define the verify_password field as required only if the new_password field is not empty. (in case
I could do it with Javascript anyway but i wonder if it is possible to do it directly with CakePHP.
When you validate data on a model, the data is already set(). This means that you can access it on the model's $data property. The example below checks the field we're validating to make sure it's the same as some other field defined in the validation rules (such as a password confirm field).
The validation rule would look something like this:
var $validate = array(
'password' => array(
'minLength' => array(
'rule' => array('minLength', 6),
'message' => 'Your password must be at least 6 characters long.'
),
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please fill in the required field.'
)
),
'confirm_password' => array(
'identical' => array(
'rule' => array('identicalFieldValues', 'password'),
'message' => 'Password confirmation does not match password.'
)
)
);
Our validation function then looks at the passed field's data (confirm_password) and compares it against he one we defined in the rule (passed to $compareFiled).
function identicalFieldValues(&$data, $compareField) {
// $data array is passed using the form field name as the key
// so let's just get the field name to compare
$value = array_values($data);
$comparewithvalue = $value[0];
return ($this->data[$this->name][$compareField] == $comparewithvalue);
}
This is a simple example, but you could do anything you want with $this->data.
The example in your post might look something like this:
function requireNotEmpty(&$data, $shouldNotBeEmpty) {
return !empty($this->data[$this->name][$shouldNotBeEmpty]);
}
And the rule:
var $validate = array(
'verify_password' => array(
'rule' => array('requireNotEmpty', 'password')
)
);

CakePHP Required Fields - Not Making Any Sense

I have a form that it seems to me Cake is making fields required or not required so randomly that I can't even control it. Please help me to understand how to get required fields working.
public $validate = array(
'fname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your first name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'First name cannot exceed 50 characters'
)
),
'sname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your last name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'Last name cannot exceed 50 characters'
),
),
'email' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your email address'
),
'rule2' => array(
'rule' => array('maxLength', 100),
'message' => array('Email cannot exceed 100 characters')
),
'rule3' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address'
),
'rule4' => array(
'rule' => 'isUnique',
'message' => 'An account already exists with that email address',
'on' => 'update'
)
)
);
All these 3 fields cannot be empty, but Cake is deciding to make fname and sname show as required on the form, but email is not required. I'm talking about the class on the inputs making it look required. This is so random. In the database the fields are all identical VARCHAR, NOT NULL.
I tried adding allowEmpty => false to the email but it doesn't do anything. This works on other fields that only have one rule but doesn't work on fields that have multiple rules. Still, I want to know why it is making fname and sname required but not email.
Screenshot: http://i.imgur.com/3JMTR.gif
Hi in the table for email field make it as could not be
NULL
(I think email column can accept null values like that u created the table) then it Required * symbol will come on view. Surely it will work, I face the same issue like this
Try adding 'required'=true on the rules of the fields you want to be required.
You can switch off all of the email validation rules by:
unset($this->ModelName->validate['email']);
But after watching the screenshoot,i can see that there is no red star above email field.The cause of this maybe you didn't properly named the email input
echo $this->Form->input('email');
Normally if you have this line(if it is generated by bake than must be right),it must have the validation rules for email field

Cakephp: Form Vailidation

I'm trying to create a validator for my models:
But taking the example from http://book.cakephp.org/:
var $validate = array(
'country' => array(
'rule' => 'notEmpty'
)
);
gives the following error: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/app_model.php, line 166]
By googling this error I found a mailinglist entry that recommended using: ( http://cakephp.1045679.n5.nabble.com/validation-notEmpty-td1320629.html)
'country' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'This field cannot be left blank.'
)
which didn't work. so I tried:
'country' => array(
'rule' => 'VALID_NOT_EMPTY',
'message' => 'This field cannot be left blank.'
)
Which marked the field as a required, but didn't stop me from leaving it blank.
My question is: how to do this correctly? I'm using CakePHP 1.3.6
Most probably you need to put the required key; something like this:
'country' => array(
'rule' => 'notEmpty',
required => true,
'message' => 'This field cannot be left blank.'
)
Hope this helps.
Why donĀ“t you use the commandline for baking your models (cmd: cake bake)? If you bake the models you can specify the validation rules there.
It is fast and easy... and you can see how validation works.
It helped me a lot...
Here an example code.
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Username required',
'allowEmpty' => false,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
You must use an array for the rule definition...
in your view file just add 'class'='required'
eg:
<?php echo $this->Form->input('new_email',array('class'=>'email required yellow', 'div'=>false, 'label'=>false));?>

Resources