Custom error messages in Codeigniter - codeigniter

I am trying to set custom error messages for invalid length of the following fields:
$this->form_validation->set_rules('name', 'Name', 'required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
I want to set custom error messages for both of above fields, for example "Invalid name" and "invalid password".
I am trying following:
$this->form_validation->set_message('min_length', 'Invalid name');
But it will put same error message to both fields ie. 'Invalid name'.
How can I set second message (eg. Invalid Password), to the password field?)
Thanks for help.

From CI user_guide:
If you include %s in your error
string, it will be replaced with the
"human" name you used for your field
when you set your rules.
$this->form_validation->set_message('min_length', 'Invalid %s');
%s will become "password" or "name" or whatever field name the min_length is set

Related

form_validation : error message array shows the default

I'm working on a form_validation. I have set the error messages, but it only shows the first error I've set. Other than that, it shows the default message.
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[user.email]',
array(
'required'=>'Empty email',
'is_unique[user.email]'=>'Email has been registered')
);
If I don't fill the email form, it shows 'Empty email'. If I write the email the same as in database, it shows the default error of CI : 'The Email field must contain a unique value.'
How I can show 'Email has been registered'?
Try:
$this->form_validation->set_rules('email','Email','required|valid_email|is_unique[user.email]',
array(
'required'=>'Empty email',
'is_unique'=>'Email has been registered')
);
DOCS: https://www.codeigniter.com/userguide3/libraries/form_validation.html#cascading-rules
I suspect you aren't getting the right error message because you shouldn't repeat user.email for the errors array.

How to verify whether validation message is displaying or not using protractor

Test case : I just want to validate the "Name" field by sending "alpha numeric characters" data one at a time into the name field and verify whether "invalid data" validation message is displaying or not
My Spec file code is below.
//Testcase 3 : To verify whether "Name" field validation message is displaying or not
// "name field is a required" field
using(CertificationTestData.testData, function(testdata){
it('validation of Certificate Name field content',()=>{
//To clear the "Ceritication Name" field data
page.getCertificationNameField().clear();
//enter each type of test data one at a time
page.getCertificationNameField().sendKeys(testdata.data);
browser.driver.sleep(5000);
page.getAnyWhereCertificationPage().click();
//To check whether any validation message is displaying for "Name" field
expect(page.getValidationMessage().isDisplayed()).toBeFalsy();
});
});
My external test data file is below
testData :[
{data:"abcd"},
{data: "1234"},
{data:"!##$%#"},
{data:"ad123!###$"}
],
My Po file code is below.
//To return "Certification Name" field
getCertificationNameField(){
return element(by.css('input[formcontrolname="CertificationsName"]'));
}
//To return validation message
getValidationMessage(){
return element(by.className('ui-message ui-messages-error ui-corner-all'));
}
When i run my test script it is displaying below error message.
should test certificate tab validation of Certificate Name field content
- Failed: No element found using locator: By(css selector, .ui-message.ui-messages-error.ui-corner-all)
Is there any way to verify whether the validation message is displaying or not ??
isDisplayed() always requires the presence of the element as it executes the ElementFinder and returns the error of it.
If the element is for sure displayed, when it's present, just replace isDisplayed() with isPresent().
Else check if(element.isPresent()){expect(element.isDisplayed()).toBeFalsy()} else {expect(element.isPresent()).toBeFalsy()} ... though it's a bit strange and verbose that way.

kohana custom validation error message

I have a callback within kohana validation. I'm sending error message this way:
public function way(Validate $array, $field)
{
if (something)
{
$array->error($field, 'super error message', array($array[$field]));
}
}
It works but when I print out the message
echo $errors['field'])
it returns formName.field super error message
How to get rid of formName.field?
These are messages configured in Kohana Core or in Modules or Application.
You can change them in messages folder
e.g. default validation message are in System -> messages -> validation.php
you an copy this file in your application and remove :field from them it will get rid of field name.
'not_empty' => ':field must not be empty',
change it to
'not_empty' => 'must not be empty',

Laravel before/after validation message

In Laravel you can make a custom messages for validators. But I found that the prepared messages are little bit wrong. With before and after validation rules, the parameter is converted with strtotime like that said in the documentation.
So if I set rule 'expires' => 'before:+1 year' the rule is working and correct. But if the user inputs a wrong value, Laravel prints the message like that:
The expires must be a date before +1 year
This message is very unclear for the average client. I was expected that the message will be converted with the strtotime also.
There is a clean way to print more clear error message?
You can override the validation messages with a custom ones if you want to.
In the Form Request class, add the following method and change the messages like so:
public function messages()
{
return [
// 'fieldname.rulename' => 'Custom message goes here.'
'email.required' => 'Er, you forgot your email address!',
'email.unique' => 'Email already taken m8',
];
}
Update:
If you want to change the global messages, you can open the validation.php file inside resources/lang/en/validation.php and edit the message there.
You can user AppServiceProvider.php
and you can write your new validation rule there and set your error message in validation.php file
/app/Providers/AppServiceProvider.php
/resources/lang/en/validation.php
Ex:
Validator::extend('before_equal', function ($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
and the message is
'before_equal' => 'The :attribute field is only accept date format before or equal to end date',
Please try this and you can alter this to based on your require.

insert user profile tank auth

I've seen question from this CodeIgniter: Passing fields to user_profiles database with Tank_auth
and my problem same as Davezatch have, but mine like this on auth controller:
...
if ($this->form_validation->run()) { // validation ok
if (!is_null($data = $this->tank_auth->create_user(
$use_username ? $this->form_validation->set_value('username') : '',
$this->form_validation->set_value('email'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('webiste'),
$email_activation))) {
...
when sending to my email, it show like this:
A PHP Error was encountered Severity:
Notice Message: Undefined variable:
new_email_key Filename: email/activate-html.php
Line Number: 14
any suggestion?
thank you
Looks like you are missing the new_email_key , that is used to activate the account. Check if it is present in your database also check your config for email activation .

Resources