CakePHP Required Fields - Not Making Any Sense - cakephp-2.0

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

Related

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 makes a unique key validation that I did not ask for

I have a simple table where one of the field is country
It is not defined as unique key in the database and when I try to insert a record with the same country directly in MySql , the record is inserted without error. Fine.
But when I tried to insert through a edit screen in my Cakephp application, I got the error message on the country field 'Alphanumeric' which does not correspond to the reality because if I enter
for example: United States which is a country of another existing record, I got the error, if I enter xxxxx, the system accept to save the record. THat's is why I suspect that there is a check on unique Key.
My model for this field is the following
'country' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'alphanumeric' => array(
'rule' => array('alphanumeric'),
),
)
Do you have any idea ? Is there another place where I should check ?
"United States" contains a space. The alphanumeric rule won't validate strings with space.
**Use this code for unique validation form in cakephp.........**
$validator = $this->validator();
$validator['username'] = array(
'unique' => array(
'rule' => 'isUnique',
'required' => 'create'
),
'alphanumeric' => array(
'rule' => 'alphanumeric'
)
);

CakePHP one of the validation rules is not applied during edit/update

Here are the 3 validation rule that are applied for "initials" field:
'initials' => array(
'Not empty' => array(
'rule' => 'notEmpty',
'message'=>'Please enter a customer\'s initials'
),
'Unique' => array(
'rule' => array('isUniqueForCompany'),
'message' => 'Customer with these initials already exists'
),
'Long' => array(
'rule' => array('between', 2, 12),
'message' => 'Initials should be between 2 and 12 characters long'
)
)
When creating a new record, all 3 rules are applied, but when updating/editting the 'Unique' rule is skipped out.
What can cause such a problem?
If needed, I can provide the controller actions ar view forms.
I would suggest that you have a problem with your custom isUniqueForCompany validation function in the model.
It's probably returning true when it shouldn't.

Validation defined in model - CakePHP

Using CakePHP 1.3 I developed engine for blog with posts and comments tables and recently I have noticed that in database I've got records with null values in content column despite of the fact that Comment model has defined proper validation:
<?php
class Comment extends AppModel {
var $name = 'Comment';
var $sequence = 'comments_seq';
var $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id'
)
);
var $validate = array(
'content' => array(
'required' => array (
'rule' => 'notEmpty',
'message' => 'Content can't be empty.'
)
),
'post_id' => array(
'rule' => 'notEmpty'
),
'created' => array(
'rule' => 'notEmpty'
)
);
?>
Is there a bug in CakePHP framework or validation defined above is not correct or insufficient?
In your validation rules, you're not actually requiring the field. Requiring means that the key must exist when it comes time to validate. The notEmpty rule requires only that the key is not empty but not that it exists.
To require that the field exists, use the required option in your validation rules:
var $validate = array(
'content' => array(
'required' => array ( // here, 'required' is the name of the validation rule
'rule' => 'notEmpty',
'message' => 'Content can\'t be empty.',
'required' => true // here, we say that the field 'content' must
// exist when validating
)
),
'post_id' => array(
'rule' => 'notEmpty'
),
'created' => array(
'rule' => 'notEmpty'
)
);
Without the required key, you could potentially save completely empty records by simply not including the 'content' key when saving. Now that it is required, validation will fail if 'content' is not in the data that you're saving.

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