cakephp 2.4 isUnique validation not working - validation

I have following code in my model .
'uniqueNameRule' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create', // here
'last' => false,
'message' => 'name already exists'
)
The above code validates the field name but there is an issue that when i try to insert new name it does not save it , means validation message is shown for each time .
I tried to figure out this and find that 'on' => 'create', is creating issue . Please help me on this thanks.

Make sure that you set the validation on the right way:
public $validate = array(
'column_name' => array(
'uniqueNameRule' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create', // here
'last' => false,
'message' => 'name already exists'
)
)
);
The 'on' => 'create', line says to cakePhp that the validation must be executed only when you a creating a new register. If you are updating an existing register, then validation won't be executed at all.
Also, check if the validation error is not running on another validation block, as you set 'last' => false, which means that validation will continue in spite of this rule failing.

If you've issue in isUnique rule in validation. You can use Multivalidatable Behavior. below is the complete example how to use please visit below link.
http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model
Thanks

Related

nothing is working except 'not empty' validation in cakephp

I'm new to CakePHP. I'm validating my form, but the problem is that no validation is working except the not empty validation.
My model file is:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'rule_name' => array(
'alphaNumeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'Letters and numbers only'
)
)
);
}
My View file is:
echo $this->Form->create('User');
echo $this->Form->input('rule_name',array('class'=>'form-control','autocomplete'=>'off'));
Please suggest how I can fix this.
General usage pattern adding a rule for a single field:
public $validate = array(
'fieldName1' => array(
'rule' => 'ruleName',
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
)
);
Must be look at Data Validation in cakePHP

Magento issue with adding validation into multi-select checkbox

I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.

CakePHP How to override already declared model validation error messages?

I can successfully put new messages in view files only if they are not present in model. I mean validations have to be in model, but messages attached to those validations should not be there.
Example model
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true, 'allowEmpty' => false,
'message' => 'Please enter a username.'),
'alpha' => array(
'rule' => array('alphaNumeric'),
'message' => 'The username must be alphanumeric.'),
'unique_username' => array(
'rule'=>array('isUnique', 'username'),
'message' => 'This username is already in use.'),
'username_min' => array(
'rule' => array('minLength', '3'),
'message' => 'The username must have at least 3 characters.')),
'email' => array(
...................
...................
and here's an example of view
echo $this->Form->input('username',array(
'label' => __d('app_users','Username'),
'error' => array(
'required' => __d('app_users', 'Please enter a username'),
.....................
.....................
));
So, my question is how can one make it into a work without removing 'message' => 'Please enter a username.' in a model but to TRULY OVERRIDE IT.
I mean validations have to be in model, but messages attached to those validations should not be there.
And how do you then have messages for a JSON API service? The text contained in validation has nothing to do with how your site looks so should not be in a view.
You can change the validation message from the model or from custom validation methods without much hassle and its all in the book

conditional validate on password update in cakephp

In the user registration form, I have password and password2 so that user has to enter password 2x and they have to equal each other before he/she is registered. The validation rules work fine in this case. In the update user info form. I have both password and password2 set to blank so that if the user does not enter a value in the first password field or password2 field, then the system simply saves the current password which I have stored in a temporary variable in my user model. So far so good. What I want is to trigger all the same validation rules as when registering but ONLY if the user enters a value in the first password field, but leave blank and ignore otherwise. So I guess this is some kind of conditional validation.
In the user model:
$validate = array(
'password' => array(
'password_notempty'=>array(
'rule' => 'notEmpty',
'message' => 'Required field',
'on' => 'create')
,
'password_between'=>array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters',
'on' => 'create')
,
'password_alphanumeric'=>array(
'rule' => 'alphaNumeric',
'message' => 'Characters and numbers only',
'on' => 'create')
),
'password2' => array(
'password2_isequal' =>array(
'rule' => array('comparePasswords','password'),
'message' => 'Must be same value as password.')
)
);
Anyone have ideas on how to accomplish this?
this should help getting you started:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/
you can also look at the code for examples on how to create the validation needed.
basically you always use a different field and only map it to the real field if something is actually entered. also dont forget to include a confirmation field (to retype and confirm the correctly typed password)
the article describes how you can achieve all that will a behavior in 3-4 lines of code.
read below url :-
http://book.cakephp.org/1.3/view/1143/Data-Validation
http://book.cakephp.org/1.3/view/1152/Core-Validation-Rules
//Or Try this:-
$validate = array(
'new_password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'retype_password'=>array(
'rule'=>array('equalTo','password'),
'message'=>'Password does not match'),
'repeat_password'=>array(
'rule'=>array('equalTo','new_password'),
'message'=>'Password does not match'),
);

CakePHP - How to check which validation rule failed?

How can I check in my controller which one of the validation rules failed? I want to know if 'rule2' failed, based on the validation rule key.
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => array('email', true),
'message' => 'Please enter a valid email address'
),
'rule2' => array(
'rule' => 'isUnique',
'message' => 'Email address already registered'
)
),
);
I know I can do debug($this->User->validationErrors) but all that shows is the message, and I don't want to check the message because I might change that in the future.
Is there any way to see that 'email.rule2' failed, based on the validation rule key?
for me it never made sense that the default param for last is false
IMO the correct approach is:
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => array('email', true),
'message' => 'Please enter a valid email address',
'last' => true
),
'rule2' => array(
'rule' => 'isUnique',
'message' => 'Email address already registered'
)
),
);
ONLY check on unique if the email is valid in the first place.
and no, the message is the only thing returned. why is it important what failed?
I'm not sure if it does exactly what you need but try this:
$errors = $this->ModelName->invalidFields();

Resources