Codeigniter form validation set message - php

A quick one guys, I have the same (ish) form validation rule for the username and email input fields respectively, and i need to set the message of the rules individually. How do i do that?
**CODE**
$this->form_validation->set_rules('username','Username', 'trim|required|xss_clean|min_length[2]|is_unique[users.username]');
$this->form_validation->set_rules('email','Email', 'trim|required|valid_email|xss_clean|is_unique[users.email]');
I'm trying to override the message for "is_unique" here,
$this->form_validation->set_message('is_unique','Username taken');
how do i set the message for email as well?

$this->form_validation->set_message('is_unique', '%s is taken.');
%s is replaced with the "human name" you applied when setting the rule.

Sorry for my mistake, I use extended Form_validation of CI and there are 3 fields :)...
As #Rick Calder said the only way to do it is to use, %s which will equal to the inputed name/email..
$this->form_validation->set_message('is_unique', '%s is taken');

Related

How do I Set custom error messages on Codeigniter

I'm trying to set a custom error message for a particular field on some particular rule as per codeigniter's doc, and I'm running into issues. My code looks like the following
function start(){
$this->form_validation->set_message('rule1', 'Rule 1 Error Message');
$this->form_validation->set_rules('amount', lang('users amount'), 'required|trim|numeric|greater_than[0]', array('rule1' => 'Error Message on rule1 for this field_name'));
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else{
$amount =$this->input->post("amount", TRUE);
}
I've tested the rules for each field and they work fine, the problem starts when I add a custom error message for each form field. Previously, if there was an error I'd redirect and display a generic message for the entire form.
$this->session->set_flashdata('error', "Please provide correct data about the transfer");
With the custom message it doesn't throw the custom message at all when I enter data that doesn't obey the rules, however it does throw an error for a pre-defined rules like min_length or max length.
I've followed the docs to a tee, and I don't seee why this issues is happening. I'm aware that there is a way to throw custom messages using callback functions, but preferably I'd like to use this method.
Please try the following steps:
$this->session->set_flashdata('field_name', "Any custom error message");
redirect('page_URL');//use url to be redirected upon.
OR
$this->form_validation->set_message('is_unique', 'The %s is already taken');
The form_validation changes %s with the label seted for the field.
I hope one of these will helps!

Using Advanced Custom Field value as "Email to" in Contact Form 7

I'm creating a page in which you submit a Contact Form 7 to the email based on what email is added to the job_email field via my Advanced Custom Fields.
I have attempted to add the form via the PHP shortcode like this
[email* dynamic-email class:email-address placeholder "Email Address*"]
echo do_shortcode('[contact-form-7 id="234" title="Dynamic Submit CV" dynamic-email="'.get_field( 'job_email' ).'"]' );
The fields all display correctly, and upon submitting the email successfully, the email never receives the form. Meaning I am totally missing something here.
If I understand correctly, what you are after is send a notification with the entry details to the email address inputted on the job_email field.
If so, what you want to do is to set up an email on the email tab and in the To: setting put [job_email].
Update
As you stated in the comment, the email has to be the one set in the backend. You can guarantee the email notification to be sent to the ACF-based email via hooks, specifically the wpcf7_mail_components hook.
function so57013385_mail_components( $components, $number ) {
$job_mail = get_field( ... );
$components['recipient'] = (array) $components['recipient'];
$components['recipient'][] = $job_mail;
return $components;
};
add_filter( 'wpcf7_mail_components', 'so57013385_mail_components', 10, 2 );
Note: I'm casting the $components['recipient'] variable to array just to make sure the email is appended correctly, as this parameter is passed directly to wp_mail(), which can receive a string or an array. Also, you might need to add more validations on the form ID, sanitization, etc. The above code is not tested.

Laravel: Display html in app/http/requests custom error messages

I'm working on a custom request validator with custom error messages for Laravel 5. Is there a way I could display HTML in returned messages?
My goal is to be able to include links in the message, ie:
"You need a lottery ticket number. Don't have one? <a href='#'>Register Here.</a>"
I've searched different forums and couldn't find a solution. Below isn't applicable in my case:
https://laracasts.com/discuss/channels/general-discussion/html-in-httprequests-custom-error-messages
EDIT:
SOLVED.
I'm using custom messages set in a custom Request:
public function messages()
{
return [
'dob.eighteen' => 'You must be 18 or older to take part in this competition.',
'photo.required' => 'You must upload a photo of the space.',
'photo.mimes' => 'The uploaded file must be a photo in .jpeg or .png format.',
'name.required' => 'Please enter your full name.',
'ticket.required' => 'You need a ticket number to enter. Don\'t have one? Register Here.',
'email.required' => 'The e-mail field is required.',
'dob.required' => 'The date of birth is required.',
'postcode.required' => 'The date of birth is required.',
'postcode.postcode' => 'The postcode you entered is incorrect.', ];
}
The reason why the above link wasn't applicable is because I want to keep my code neat and store all the error messages in one file together with request validation rules.
The solution was so simple that I completely missed it before.
Thanks for helping.
The errors are displayed using {{ $error }} tag in a blade template.
To display a message that contains html = If you don't want the data to be escaped, you should use the following syntax: {!! $error !!}
Thanks for helping.

Magento registration returns 'Last name cannot be empty'

I am trying to deploy Magento 1.9.2.1. I used the package from the official site.
But a strange thing happened to me. Among all the instances I deploy, it fails at any page that requires validation of a customer information (registration, etc.). It returns the error 'Last name cannot be empty'. But even stranger, there were 1 or 2 times of success before the errors. That is, the error only happens after I have created 1 or 2 accounts.
I found that the validation method is in the model, as:
if (!Zend_Validate::is( trim($this->getLastname()) , 'NotEmpty')) {
$errors[] = Mage::helper('customer')->__('The last name cannot be empty.');
}
So I added some lines before it.
$errors[] = Mage::helper('customer')->__('The first name cannot be empty.');
$errors[] = Mage::helper('customer')->__($this->getFirstName());
$errors[] = Mage::helper('customer')->__($this->getLastName());
$errors[] = Mage::helper('customer')->__($this->getEmail());
This time there are 5 errors displayed at the registration page. As expected, the first is 'The first name cannot be empty.'. But the second and third are all empty. The fourth is the email address I typed in.
Here comes another problem. The validation of the first name comes before that of the last name. Were they all empty, how could the error message be only 'The last name cannot be empty.'?
And more substantially, why can I register at the very beginning? What is the substantial problem?
UPDATE: If I comment the validation for the two names, there are no more errors. But the account created has its last name empty, while first name as I typed in.
try to run this query,
UPDATE eav_attribute SET is_required = 0 WHERE attribute_code = 'lastname'
later clean cache

CodeIgniter set error message with value in message

So I read the user guide for form validation and there it shows how to set custom message. For example, if I put this in my code
$this->form_validation->set_message('is_unique', '%s address already registered.');
it will output E-mail address already registered. (If it is validation for email field)
What I want to do is have a message that will say E-mail address test#website.com already registered. Is there a code to put to show the value of the field that is being passed? (like %s, that shows the field name)
$this->form_validation->set_message('is_unique', '%s address ' . $this->input->post('email', true) . ' already registered.');
Out of the box, it only translates the field name with sprintf, not the field value:
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
Simply extend CI_Form_validation with MY_Form_validation and parse the string to fit your needs.

Categories