How do I Set custom error messages on Codeigniter - php

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!

Related

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

Moodle display moodle_exception Error on form post

I am new to moodle and trying to customize my course creation in moodle.
I validated by moodle_form data in a function and from there I need to throw an error which will be displayed after the form (course/edit_form.php) is submitted. I tried the moodle way, as they have done:
throw new moodle_exception('refresher_value_less_than_one', '', '', $data->refresh_value);
I expected it to receive the error at the form page. It showed be the error string but not inside the form, but on a blank page.
Any idea, how to catch this exception in form.?
If you want to show an error within the form, then you need to create a function within your form class called 'validation($data, $files)'. Within this form, you need to check the values in the array $data, then return an array of error messages, indexed by the form field that the error should display beside.
For example, if you had a field called 'myfield', you could check the contents of $Data['myfield'] and, if there was a problem, return array('myfield' => get_string('myfielderror', 'myplugin')).
Throwing an exception will immediately halt the execution of the page and output only that error message.

Highrise API Returning Error Code 500

I am attempting to use the PHP Wrapper for the Highrise API located here:
https://github.com/ignaciovazquez/Highrise-PHP-Api
I need to set a custom field for a HighrisePerson object. According to the code this should be pretty straightforward...
$person->setCustomField("Field Name", $value); // Pulled almost straight out of the documentation
Unfortunately when I try to save this back to highrise using $person->save(); I get the following error:
Uncaught exception 'Exception' with message 'API for Person returned Status Code: 500 Expected Code: 200'
So the error isn't in the code... Highrise just isn't accepting the custom field. Any ideas as to why this is?
In order to use 37signals throught Highrise-PHP-Api, you should provide an account name and access token;
$hr = new HighriseAPI();
$hr->setAccount("accountname");
$hr->setToken("token");
and then you can execute your other functions
$person->setCustomField("Field Name", $value);
If you carefully look at tests for this api, you will see;
if (count($argv) != 3)
die("Usage: php users.test.php [account-name] [access-token]\n");
Ok... I figured it out...
In the API the following:
$person->setCustomField("Field Name", $value);
creates a new custom field within Highrise. So if there isn't already a custom field named "Field Name" it would create it. If that field already exists, it returns the 500 error.
To the best of my knowledge there's no way to set the value of an existing field using that wrapper. You can only create new fields, which is kind of jank.
I found a fork off that wrapper that's working pretty well for me. It's hosted here: https://github.com/AppSaloon/Highrise-PHP-Api
The usage in this one is confusing and took me a while to figure out.
Basically you want to do a search for all the custom fields in Highrise. Once you find the one you want, you assign it the requisite value... So the code looks like this:
// Load up all the custom fields out of Highrise
$cfields = $highrise->findAllCustomfields();
// Search each custom field until we find the "Field Name" one. When we do, add it to our Highrise Person.
foreach ($cfields as $c) {
if ($c->getSubjectFieldLabel() == "Field Name")
{
// Assign that custom field to the person
$highrisePerson->addCustomfield($c, "Field Value");
}
}
I hope this helps someone else down the road who runs into the same issue. I discovered the forked PHP wrapper from another Stack Overflow question, but they were never able to get custom fields to work either.

Kohana Validation _external messages

I have 3 models using validation messages. 2 of them work perfectly for all messages, including _external messages. The third never returns the custom _external message. The folder structure and way in which I get the errors are identical for each model.
Folder Structure
messages\models\verify
Catching the Errors
catch (ORM_Validation_Exception $e)
{
return $e->errors('models/verify');
}
Errors Passed to View
array(1) { ["_external"]=> array(1) { ["activation_hash"]=> string(33) "activation hash must not be empty" } }
Output External Error
<?= Arr::path($errors, '_external.activation_hash'); ?>
messages\models\verify_external.php
return array(
'activation_hash' => array(
'not_empty' => 'The email fields did not match'
),
);
Validation
$extra_rules = Validation::factory($values)
->rule('activation_hash', 'not_empty');
$email->check($extra_rules);
There that should be everything. Again, everything is exactly the same amongst 3 models so I assume its a typo or a bug. Either way I pasted everything here from the code, do you see something I missed? In a debug attempt I put the _external.php file in every directory along the path to the verify folder but got no where so I dont think its my folder structure.
My solution was to include the validation messages in an existing message file. The purpose of the verify action in my example above was to verify a users email address. I added the external verify messages to the external email array and it worked as intended.

Codeigniter form validation set message

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');

Categories