My Laravel required_without_all validation is not working as expected - php

I am working on a project, where I have three fields 1. csv file upload(email) 2. comma separated string(emails) 3. dynamic input field(email), any one of these fields is required.
My approach for validation:
'comma_separated' => ['nullable','required_without_all:email.*,file'],
'file' => ['nullable','required_without_all:email.*,comma_separated', 'mimes:csv'],
'email.*' => ['nullable','required_without_all:comma_separated,file','email'],
If I enter email in dynamic field, its asking me for file and comma_separated fields.
Any help will be appriciated.
Thank You.

It seems that there's an issue with the validation rules you've defined. The 'required_without_all' validation rule is used to check if a given input field is required if all other specified fields are not present.
In your case, it seems like you want to validate that at least one of the three fields is present, not that all of the fields are absent. To achieve this, you can use the 'required_with' validation rule instead.
Try the following validation rules:
'comma_separated' => ['nullable','required_with:email.*,file'],
'file' => ['nullable','required_with:email.*,comma_separated', 'mimes:csv'],
'email.*' => ['nullable','required_with:comma_separated,file','email'],
This should validate that at least one of the fields is present, and the other fields can be left empty.

Related

Multiple Form Validation - Laravel

I am trying to validate a form field in laravel mb_plans based on 2 others form fields activity and options, if both activity and options have there respective value then validate mb_plans
I've tried required_if and required_with, but can't able to use AND condition.
'mb_plans' => 'required_if:activity,MB|required_with:options,0',
if you want to validate when activity value is MB and options is 0 then use :
'mb_plans' => 'required_if:activity,==,MB|required_if:options,==,0',
try below:
'mb_plans' => 'required_with_all:activity,options',

validate all arrayitems except last in laravel

With the asterisk I can validate all array items, like this:
'event_time_start.*' => 'required|date_format:G:i',
But I want apply this validation rule on all items, except the last one. How can I achive this?
It is always recommended to exclude unnecessory item from front end if possible. If not you can remove last item before sending request data to Validator.
$requestData = $request->all();
$count = count($requestData["event_time_start"]);
unset($requestData["event_time_start"][$count-1]);
$validator = Validator::make( $requestData , [
'event_time_start.*' => 'required',
]);
In your JavaScript, you're cloning an empty hidden row when a user clicks add. My solution would be to not have a name attribute on that field, so it is never posted to the server. When you clone the row, then add the name="event_time_start[]" attribute to the newly cloned row.
This would preserve bandwidth and allow you to not need the squirrelly validator exception, which will be strange to read in code 6 months from now.

How to show a field after validateForm() is called in Drupal 8?

Inside a hook_form_FORM_ID_alter() function I show or hide a field based on another checkbox field is checked or not with the following code in Drupal 8.
$form['field_my_url']['#states'] = [
'visible' => [
':input[name="field_my_checkbox[value]"]' => ['checked' => TRUE],
],
];
In case user checks field_my_checkbox checkbox, enters an invalid URL in field_my_url and decides to uncheck field_my_checkbox checkbox, the field_my_url will then be hidden with the invalid URL remaining. It will fail the validation because the enter URL is not valid.
The user will then be redirected back with the error message. But because the field_my_checkbox checkbox was not checked, the field_my_url field will be hidden with the error message and the user cannot see that.
In this case, how can I show the field_my_url field if it failed the validation because it contains invalid URL in Drupal 8?
I have found a solution. It might not be efficient. But it solves the problem for now, since there is no one come up with any solution on this moment. If the validation failed. I just unset the '#states' from the field. By doing this, no #states visible will be applied and the field_my_url will be show as normal.
unsert($form['field_my_url']['#states']);

WordPress Gravity Form Field Validation

I have a WordPress sites using Gravity Forms, and within this form I have a field that requires a special 10 digit code to be input by the user that matches up with a database of codes (there are 20 of these codes). If the code that has been input matches one of the 20 codes, then they can proceed with submitting the form. If it doesn’t match then they cannot proceed.
Please advise on how to create a PHP function that achieves this?
Many thanks
I wrote a snippet that you might find helpful.
http://gravitywiz.com/require-existing-value-submission-gravity-forms/
To use this snippet for your needs, you would need to setup a new form that will be used to store the 20 codes. Add a field a Single Line Text field to your form. Add an entry for each code.
On your original form, you can now configure the snippet to check the values of the new form for a valid code. Happy to answer any questions.
new GW_Value_Exists_Validation( array(
'target_form_id' => 613, // your original form ID where the codes will be validated
'target_field_id' => 1, // the field on your original form where the user should enter the code
'source_form_id' => 519, // the new form you will create to store your codes
'source_field_id' => 1, // the field on the new form that will store one code per submission
'validation_message' => 'Hey! This isn\'t a valid reference number.' // the error message displayed if the user does not enter a valid code
) );

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