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
Related
I prepare some registration form (the simplest) and in my Model i prepare some validation for email field:
'email' => array(
'mail' => array(
'rule' => array('email', true),
'required' => false,
'message' => 'Not correct e-mail!'),
'unique' => array(
'rule' => 'isUnique',
'message' => 'E-mail was registered!'))
But the rule isUnique doesn't work.
Addition i change in MySQL field email to unique, but still doesn't work.
I use CakePHP 2.3.7
Problem is when we use Translate behavior with model.
The query:
SELECT COUNT(DISTINCT(`User`.`id`)) AS count FROM `sometable`.`users` AS `User` INNER JOIN `sometable`.`i18n` AS `I18nModel` ON (`User`.`id` = `I18nModel`.`foreign_key` AND `I18nModel`.`model` = 'User' AND `I18nModel`.`locale` = 'pl') WHERE `User`.`email` = 'kicaj#kdev.pl'
Field email isn't translated.
Try this.
array(
'email' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Provide an email address'
),
'validEmailRule' => array(
'rule' => array('email'),
'message' => 'Invalid email address'
),
'uniqueEmailRule' => array(
'rule' => 'isUnique',
'message' => 'Email already registered'
)
)
);
I copied your code and pasted it in my Model and it just worked fine!!! I don't think there is any error in it. I just added semicolon after your code. Here is the code that I have checked:
'email' => array(
'mail' => array(
'rule' => array('email', true),
'required' => false,
'message' => 'Not correct e-mail!'),
'unique' => array(
'rule' => 'isUnique',
'message' => 'E-mail was registered!')),
I think there would be some other error!!
I have a model ex. posts that acts as translate and I want to validate the title which is a translated field.
How is this possible. The below does not work.
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
),
));
Change rule name to camelCase: notEmpty and add message
you translate your message in /Locale default.po
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'your translated message',
),
));
whats the big deal?
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'),
);
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();
How validate file upload in CakePHP?
When i wrote some like this:var $validate = array(
'file' => array(
'select' => array(
'rule' => array('selectFile'),
'message' => 'There is no file!'),
'type' => array(
'rule' => array('typeFile'),
'message' => 'Bad type!'),
'size' => array(
'rule' => array('sizeFile'),
'message' => 'Bad with size!')));
Works only with the last validate, here 'size'.
Maybe You know solution, for validation for files with many messages?
try adding required=true to each of the 3 rules