I am trying to conditionally validate an array input in Laravel. I am following the documentation provided here. But the documentation does not provide details on how to do it on an array input. Below is the code that I am trying
$rule = [
'report.*' => 'max:255',
'comment.*' => 'exclude_if:report.*,file|max:65535'
];
$validator = Validator::make($array, $rule);
Here the report is a file input and comment is a text area field.
<input type="file" name="report[0]"/>
<input type="file" name="report[1]"/>
<textarea name="comment[0]"></textarea>
<textarea name="comment[1]"></textarea>
The comment field is required if the corresponding file input is empty. How can I achieve this in Laravel 7?
Did you try required_if ?
And i think you must write "report" instead of "file" because your input name is report.
If that doesn't work, maybe you need to make a custom rule.
I was able to do it using below code.
$rule = [
'report.*' => 'max:255',
'comment.*' => 'required_without:report.*|max:65535'
];
$validator = Validator::make($array, $rule);
Related
I used the laravel validator for credit card information as per instructed here.
I can finally let it work but my problem is how can I make its result readable by a user. I am also using Laravel validation like this:
public static function validate()
{
$val = [
'card_num' => ['required', 'numeric', new CardNumber],
];
return $val;
}
So, if I clicked the submit button and the field is empty, the result is card numは必須です。 which means card number is required. And if I supply an invalid card number, the result is validation.credit_card.card_length_invalid, how can I make it has the same result as leaving the field empty? I do like this when getting the validation errors in blade php.
<div>
<label>カード番号</label>
<input type="text" name="card_num" id="credit-card" value="{{ old('card_num', $user['card_num']) }}" >
</div>
#if ($errors->has('card_num'))
<p class="alert-error">
{{ $errors->first('card_num') }}
</p>
#endif
UPDATE
I have seen its source file and messages, but I don't want to directly change the vendor files.
I have tried adding the custom in validation.php, but it's not working.
'custom' => [
'validation.card_length_invalid' => '無効なカード長',
'validation.credit_card' => 'クレジットカード',
],
Your array in validation.php is close to correct. Try changing the top-level key to credit_card instead of custom like they have in the source code. I'm not 100% sure, but I don't think this package supports messages in the custom array like the ordinary Laravel rules do. For example:
'credit_card' => [
'card_length_invalid' => '無効なカード長',
'credit_card' => 'クレジットカード',
],
When customising Laravel 5.7 validation attributes, is it possible to include wildcards in the attribute names? I can't determine the correct way to indicate a wildcard here.
I have a contacts page which lists the details of a particular contact including all of their phone numbers and emails. From this page you can edit each of these, and add new ones. As such I've used a foreach statement to display each of these and included their id numbers in the form field names, ie:
phone_areacode_{{ $number->id }}
phone_number_{{ $number->id }}
phone_extension_{{ $number->id }}
This makes their names unique and means I can display the error messages relevant only to the specific number or email being edited when validation fails. However, the error messages that display include the number so I get things like "The phone areacode 10 format is invalid." or "The phone number 27 may not be greater than 20 characters."
Is there a way to use wildcards in the attribute names when defining them in the validation language file? Basically I'd like to be able to do this:
'attributes' => [
'phone_areacode_*.*' => 'phone areacode',
],
If not, how can I define the attribute names in the #update method of my controller?
Post values using arrays/objects, and specify validation rules/lang using dot notation:
Example POST data:
{
"phone": {
"ID1234": {
"areacode": "123",
"number": "1231234",
"extension": "1"
}
}
}
Example HTML form (equivalent to POST data above):
<form action="?" method="post">
<input name="phone[ID1234][areacode]" />
<input name="phone[ID1234][number]" />
<input name="phone[ID1234][extension]" />
<input type="submit">Submit</input>
</form>
Example validation:
$validator = Validator::make($request->all(), [
'phone.*.areacode': 'required',
'phone.*.number': 'required',
'phone.*.extension': 'required',
]);
Example lang file:
return [
'attributes' => [
'phone.*.areacode': 'phone area code',
'phone.*.number': 'phone number',
'phone.*.extension': 'phone extension',
],
]);
do you try this way it's from Laravel documentation
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
I have a form that asks for several files, and description of these files. Something like
<input type="file" name="file1">
Describe your file:
<input type="text" name="desc1">
I want the user to describe the contents of the file, instead of only showing something like Invoices-final-FinalV30.docx he might say "Invoices for January, 2018", so when I validate the form, I know how to ask if a field follows a regex, or if the field is required and so on, using the validate() method, but I want something custom, something that makes "desc1" required ONLY if there's a "file1", if there's no "file1" I can safely ignore whatever "desc1" carries.
Try required_with:anotherfield validation
https://laravel.com/docs/5.7/validation
$validator = Validator::make(
$request->all(),
[
'file1' => 'mimes:jpeg,bmp,png', //your file validation
'desc1' => 'bail|required_with:file1' //add other description validations
]
);
For array fields, example named upload[][file] , upload[][desc]
$validator = Validator::make($request->all(), [
'upload.*.file' => 'mimes:jpeg,bmp,png',
'upload.*.desc' => 'bail|required_with:upload.*.file',
]);
Take a scenario,
There are 2 fields available in the form.
1) input type file for manual upload.
2) input type = text to enter youtube video url.
is it possible using laravel built-in validations so that validation will be fired if user has left both fields empty!
I have gone through https://laravel.com/docs/5.3/validation but could not find what I wanted.
In your controller, you could do something like this:
$validator = Validator::make($request->all(), [
'link_upload' => 'required|etc|...',
]);
$validator2 = Validator::make($request->all(), [
'file_upload' => 'required|etc|...',
]);
if ($validator->fails() && $validator2->fails()) {
// return with errors
}
Try required-without-all validation rule. As given in documentation:
The field under validation must be present only when the all of the other specified fields are not present.
Assuming your fields name are url and file, your rule would be like below:
$rules = [
'url' => 'required_without_all:file',
'file' => 'required_without_all:url'
];
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are not present.
Try this,
In youre update method add this
$this->validate($request, [
'fileName'=>'required',
'urlName'=>'required'
]);
dont forget to set the fillable in your model
protected $fillable = ['fileName','urlName'];
Hope this helps
I have debit field and debit field
<input type="text" class="form-control" name="debit" value="{{ isset($expense->debit) ? $expense->debit : old('debit')}}">
and
<input type="text" class="form-control" name="credit" value="{{ isset($expense->credit) ? $expense->credit: old('credit')}}">
i need to make sure the values are equal when submit the form.
i know I can do that if name them like this:
debit and debit_confirmation and in rule array
return Validator::make($data, [
'debit' => 'required|confirmed',
]);
but I don't want to change their names.
any build-in validation in laravel 5 do that.
You could use the same validation rule to match the fields you want.
return Validator::make($data, [
'debit' => 'required|same:credit',
]);
You could take a look at the laravel documentation for more information about validation rules
Among many validators that Laravel offers there is one you're looking for: same. In order to validate if values of 2 different fields (field1, field2) match, you need to define the following rules
$rules = [
'field1' => 'same:field2'
];
You can see a list of all available validation rules here: http://laravel.com/docs/5.1/validation#available-validation-rules