I am working with a Laravel 5.8 version.
I need to modify the output of an error message form, unfortunately, the validation is not working properly.
The documentation says (https://laravel.com/docs/5.8/validation):
Specifying Custom Values In Language Files
Sometimes you may need the :value portion of your validation message to be replaced with a custom representation of the value. For example, consider the following rule that specifies that a credit card number is required if the payment_type has a value of cc:
$request->validate([
'credit_card_number' => 'required_if:payment_type,cc'
]);
If this validation rule fails, it will produce the following error message:
The credit card number field is required when payment type is cc.
Instead of displaying cc as the payment type value, you may specify a custom value representation in your validation language file by defining a values array:
'values' => [
'payment_type' => [
'cc' => 'credit card'
],
],
Now if the validation rule fails it will produce the following message:
The credit card number field is required when payment type is credit card.
I have a validation rule like:
$request->validate([
'foo' => [
'required_if:bar.bizz,1',
'numeric',
]
]);
And in my validation.php.
'values' => [
'bar.biz' => [
'1' => 'test',
],
'bar[biz]' => [
'1' => 'test',
],
'*.*' => [
'1' => 'test',
],
]
But I always get the same output:
The foo field is required when bar is 1.
Thanks.
Solved
If you are working in your form with arrays, you have also use arrays like:
'values' => [
'bar' => [
'biz' => [
'1' => 'test',
]
]
],
Also, note we are not using foo variable in the values array.
PS: The wildcard symbol (*) does not work.
Greetings.
Related
I have an array of values that I send together with other fields in a form to laravel.
The array contains a role_id field and a status field, the status can be I (Insert) U (update) D (Delete). When I validate the values in the array I want it to skip the ones where the status equals D. Otherwise I want it to validate.
private function checkValuesUpdate($userid = null)
{
return Request::validate(
[
'displayname' => 'required',
'username' => 'required',
'email' => ['nullable', 'unique:contacts,email' . (is_null($userid ) ? '' : (',' . $userid ))],
'roles.*.role_id' => ['exclude_if:roles.*.status,D', 'required']
]
);
}
I can't seem to get it to work. I've been searching all over the place for functional code as well as the Laravel documentation. But no luck so far. Has anybody ever done this?
Your problem comes from a misunderstanding of the exclude_if rule. It doesn't exclude the value from validation, it only excludes the value from the returned data. So it would not be included if you ran request()->validated() to get the validated input values.
According to the documentation, you can use validation rules with array/star notation so using the required_unless rule might be a better approach. (Note there's also more concise code to replace the old unique rule, and I've added a rule to check contents of the role status.)
$rules = [
'displayname' => 'required',
'username' => 'required',
'email' => [
'nullable',
Rule::unique("contacts")->ignore($userid ?? 0)
],
'roles' => 'array',
'roles.*.status' => 'in:I,U,D',
'roles.*.role_id' => ['required_unless:roles.*.status,D']
];
I am instantiating a constraint collection here to validate my request body for an API I am building. My idea is to validate the birthdate param only for its format, I don't need it to be required. The issue I am having is that, when I do not pass the birthdate in the request body, It throws a missing field error. Basically it treats it as a required field.
I don't know why.
$constraint = new Collection([
'fields' => [
'birthdate' => [
new Date(message: 'Please use YYYY-MM-DD format!'),
],
],
'allowMissingFields' => false,
'allowExtraFields' => true,
]);
It didn't cross my mind at first, but following #jean-max comment I figured that you should make a field optional in this case because the default config is required. So yeah this is the answer:
$constraint = new Collection([
'fields' => [
'birthdate' => [
new Optional([
new Date(message: 'Please use YYYY-MM-DD format!'),
]),
],
],
'allowMissingFields' => false,
'allowExtraFields' => true,
]);
I have two fields: Email and Telephone
i want to create a validation where one of two fields are required and if one or both fields are set, it should be the correct Format.
I tried this, but it doesnt work, i need both though
public static array $createValidationRules = [
'email' => 'required_without:telephone|email:rfc',
'telephone' => 'required_without:email|numeric|regex:/^\d{5,15}$/',
];
It is correct that both fields produce the required_without error message if both are empty. This error message clearly says that the field must be filled if the other is not. You may change the message if needed:
$messages = [
'email.required_without' => 'foo',
'telephone.required_without' => 'bar',
];
However, you must add the nullable rule, so the format rules don't apply when the field is empty:
$rules = [
'email' => ['required_without:telephone', 'nullable', 'email:rfc'],
'telephone' => ['required_without:email', 'nullable', 'numeric', 'regex:/^\d{5,15}$/'],
];
Furthermore: It is recommended writing the rules as array, especially when using regex.
I'm trying to send a campaign to a dynamic list segment based on a custom numeric merge field (GMT_OFFSET, in this case) but the code below yields the following error from the MailChimp API:
"errors" => [
0 => [
"field" => "recipients.segment_opts.conditions.item:0"
"message" => "Data did not match any of the schemas described in anyOf."
]
]
My code, using drewm/mailchimp-api 2.4:
$campaign = $mc->post('campaigns', [
'recipients' => [
'list_id' => config('services.mailchimp.list_id'),
'segment_opts' => [
'conditions' => [
[
'condition_type' => 'TextMerge',
'field' => 'GMT_OFFSET',
'op' => 'is',
'value' => 2,
],
],
'match' => 'all',
],
],
],
// Cut for brevity
];
If I am to take the field description literally (see below), the TextMerge condition type only works on merge0 or EMAIL fields, which is ridiculous considering the Segment Type title says it is a "Text or Number Merge Field Segment". However, other people have reported the condition does work when applied exclusively to the EMAIL field. (API Reference)
I found this issue posted but unresolved on both DrewM's git repo (here) and SO (here) from January 2017. Hoping somebody has figured this out by now, or found a way around it.
Solved it! I passed an integer value which seemed to make sense given that my GMT_OFFSET merge field was of a Number type. MailChimp support said this probably caused the error and suggested I send a string instead. Works like a charm now.
I need an input field for a price field for a german website. Therefore I added a NumberFormat filter and the IsFloat validator. The entered price (e.g. 7,5) gets stored correct to the database double field in the format 7.5. But now the problem is, if I wan't to edit the field, it gets populated in the format 7.5 to the user, which he can't save, because it's not a german format. So he has to replace the "." with a ",".
Is there a way, to populate the number with a "," instead of the "."?
public function getInputFilterSpecification() {
return [
'price' => [
'required' => true,
'filters' => [
[
'name' => 'NumberFormat',
'options' => [
'locale' => 'de_DE'
]
]
],
'validators' => [
[
'name' => 'IsFloat',
'options' => [
'locale' => 'de_DE'
]
]
]
],
];
}
I would suggest using a ViewHelper that renders the float with a , instead of a .. Changing the storage of the data in the database and internally handling floats with a , separator seems not the right way to go and will definitely cause issues.
There is even an inbuilt currency formatter view helper in ZF2 that you can use.
For your German currency notation it will be as simple as:
echo $this->currencyFormat(1234.56, 'EUR', null, 'de_DE');