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

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.

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!

Custom error message for Laravel validation rule: Dimensions

I'm trying to validate an image upload that looks like the following:
$this->validate($request, [
'admin_image'=> 'nullable|image|dimensions:min_width=600,min_height=600',
]);
when the selected image too small then laravel shows error:
The Admin Image has invalid image dimensiona
I think that message is not saying specifically that in which dimension the image small, eg: width or height.
I'm expecting error message like:
The Admin Image width cannot be less than 600px and
The Admin Image height cannot be less than 600px
here 'Admin image' is the attribute name & '600' is the value I given in rules
So, I wanted to make a custom error message for min_width and max_widthin custom messages array on validation.php, that look like the following:
'admin_image' => [
'dimensions.min_width' => 'The :attribute dimension (width) cannot be less than :min_width px',
'dimensions.min_height' => 'The :attribute dimension (height) cannot be less than :min_height px',
],
But unfortunately that doesn't work & Laravel continues to show the default message.
Please understand that
I need 2 separate error message for dimensions:min_width=600 & dimensions:min_height=600
like I tried in the custom error messages array.
I know this is very simple but I'm doing something wrong.
Any help will be highly appreciated
Add the custom error message in validation.php as below:
'admin_image' => [
'dimensions' => [
'min_width' => 'The :attribute dimension (width) cannot be less than :min_width px'
]
]

Mailchimp api v3 - can't create segment based on a TEXT merge field

I'm trying to create a segment based on a new merge field of type TEXT that I just created, by using condition_type = TextMerge since it seems to be the only option from their documentation that matches my field:
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#create-post_lists_list_id_segments
However, the description for TextMerge looks identical to EmailAddress. To be more specific they both apply only for the EMAIL / MERGE0 field.
TextMerge vs EmailAddress
I tried the following combinations for 'conditions':
{
'condition_type': 'TextMerge',
'field': 'EVENTS',
'op': 'contains',
'value': 'test'
}
and
{
'condition_type': 'TextMerge',
'field': 'EMAIL',
'op': 'contains',
'value': 'test'
}
The first one returns an error:
{
"type":"http:\/\/developer.mailchimp.com\/documentation\/mailchimp\/guides\/error-glossary\/",
"title":"Invalid Resource",
"status":400,
"detail":"The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"instance":"",
"errors":
[
{
"field":"options.conditions.item:0",
"message":"Data did not match any of the schemas described in anyOf."
}
]
}
The second one works.
My question is: how can I create a segment based on a custom merge field of type TEXT? To me this looks like a bug from their side. Did anyone else have this problem? Does anyone have a solution?
I finally managed to solve my issue specific to TEXT fields in my mailchimp audience merge tags.
Apparently, when adding a TEXT field, it defaults to a maximum size of 25 characters. I was trying to put more text than that into it. It fails with the error:
#body={
"title"=>"Invalid Resource",
"status"=>400,
"detail"=>"The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
"errors"=>[{"field"=>"merge_fields.MMERGE_16",
"message"=>"Data did not match any of the schemas described in anyOf."}]
}
I increased the field size to 255 via Mailchimp's API.

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

How to customize validation message in the form in zend framework 2?

I have just started zend framework 2. I just want to know how to customize message in form for elements having require ON(true). Right now its showing "Please fill out this field" (If the particular textbox is empty and I click the submit button).
I just want to change this message. Initially I thought this message is coming from library
but I was wrong. Can this possible?
Please provide the method how you are creating your form. Ultimately you should simply overwrite the messages of the Validators. Each validator has the option to overwrite messages. The basic syntax is as follows:
// This assumes to be INSIDE a Validator
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => "Dude, it's empty. It shouldn't be!"
)
)
The example overwrites the NotEmpty validator message if no input is given. Furthermore you should know, that if you use the HTML5 Attribute required then some browsers will add a pre-submit-validation to your form and the error-message displayed by the browser cannot be changed.
$username = $this->createElement('text', 'username');
$username->addErrorMessage('The username is required!');

Categories