form_validation : error message array shows the default - codeigniter

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.

Related

Laravel email sending not working when 'to' not overridden

Email sending works fine when I have a global 'to' set in config/mail.php
'to' => [
'address' => 'someone#example.com',
'name' => 'Someone',
however as soon as this is removed and email is supposedly sent to the actual email address, nothing happens. No errors, everything appears fine, except the email is never received.
I've checked spam folders, I've tried sending to different email addresses, I've tried setting the notifiable route (even though 'email' exists on the model), I've cleared cache and config cache, I've tried listening to message sending event and dumping results - I'm lost.
solved.
When sending a MailableObject (as apposed to a MailMessage) you need to chain the 'to' command like so:
public function toMail($notifiable)
{
return (new MailObject(blah blah))->to($notifiable);
}

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 .

Custom error messages in 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

Resources