validate all arrayitems except last in laravel - php

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.

Related

My Laravel required_without_all validation is not working as expected

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.

Laravel unique validation with slug

I have two options, either the users gives a custom unique_id and I convert it via Str::slug($request->custom) or it gets generated via Str::random(6)
My code:
$post = new Post;
$unique_id = Str::random(6);
$idcustom = Str::slug($request->custom);
$this->validate($request, [
'custom' => 'max:25|string|unique:posts,unique_id' . Str::slug($post->unique_id),
]);
If the user creates the custom id, $idcustom gets inserted into the database, if he just leaves the field empty $unique_id gets inserted. But if there is already an entry with for example "stack overflow" I get an SQL error, that there is already an entry with that name, but if its like "stackoverflow" it works like it should. So it has to do with the slug, what am I doing wrong here?
I don't think you can put 'fallback' values into the validation rules. You'd perhaps want to check $request->custom manually before swapping it with your random string?
Not sure whether this'll help... your question is not clear on whether you're creating a new user or updating an existing one. If updating an existing one, don't forget to exclude from checking against the current user:
'custom' => ['max:25', 'string',
Rule::unique('users')->ignore($user->id)],

Send data from div to controller in laravel

Is that possible instead of using <input name="...."/> I get my value from div or span etc. ?
currently I get my data directly from database, base on product id, but as my product might have discount that will show different price with what is saved in my database and I cannot show it as input so i need to pass it through div or span etc.
code:
here is my code now:
public function addingItem(Request $request, $id)
{
$product = Product::findOrFail($id);
Cart::add(array(
'id' => $product->id,
'name' => $product->title,
'price' => $product->price, // this comes directly from products table
));
}
with my code i always will get 45.325 but I need to get 35.325 during discount time.
That's why i need to pass it through div and cannot use input here.
any idea?
As far as your PHP code is concerned, data doesn't come from any particular part of a page, it comes from the HTTP request sent by the browser. An HTML form is just the simplest way to get the browser to add some data to that request. This may seem like nitpicking, but it has important consequences.
First, it means that what you are asking for is absolutely possible. You just need to write some JavaScript to run in the browser and tell the browser to add that value to the request. A simple way would be to have a hidden input field on the form and set the value in the JavaScript, but you can also create a completely custom request and send it to the server (AJAX).
Second, though, it means that any user can submit any data to your application by telling their browser to submit their choice of value not yours. Consequently, you have to be very careful of what data you trust, and trusting the browser to send you a price sounds like a really bad idea. What's to stop someone giving themselves a 100% discount by editing the value on the page?
Somewhere, you know what discounts you're offering. That discount is a core part of your application, so however the view knows what discount to show, the rest of the application should be able to know the same way. This probably means moving some code out of your view into a new function, which can be used by various parts of the application; that makes each use more readable, and means you don't have to change it in lots of places if the requirements get more complex.
SOLVED
well I decided to bring my codes to controller instead of using ajax or other ways, here is how I've done it:
$discounts = Discount::all();
$mytime = Carbon::now();
//get discounted price or normal price of product
$price = $product->discounts;
if($price->count() > 0 ) {
foreach($discounts as $disc){
if($disc->value_to >= $mytime) {
$price = $product->price - $disc->amount;
}
}
}else{
$price = $product->price;
}
Hope it help others.

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

Conditionally set Zend Form select element multi-options based on current value

Currently I'm trying to build a web application in Zend framework.
But I can't figure out how to Manage status in my system
Eg I have the following status for my quote handling in the system
Awaiting for Confirmation
Asssigned
In Progress
Completed
Mark As Spam
I stored these values in a table called ProviderQuoteStatus and I created a function called ProviderQuoteStatus() in the zend_db class and uses the function to generate status values in zend form dropdown box.
$select = $this->select()->from("providerQuoteStatus",
array('key' => 'providerQuoteStatusId',
'value' => 'providerQuoteStatusName'));
$result = $this->fetchAll($select);
return $result->toArray();
Here is my Zend form code
$serviceType = new Application_Model_DbTable_ProviderQuoteStatus();
$serviceTypeValues = $serviceType->getProviderQuoteStatusFormValues();
$dropDownElement = new Zend_Form_Element_Select('providerQuoteStatus');
$dropDownElement->addMultiOptions($serviceTypeValues);
Everything working fine till this stage. If the quote in Asssigned Stage I only wanted the provider select these following options
Asssigned
In Progress
Completed
How can I remove 'Awaiting for Confirmation' and 'Mark As Spam' values in the Zend form dropdown box ?
Also where should I stored all these business logic (For example if the quote in Assigned Stage only can have Assigned, In Progress options etc)? In the Model DB class?
Thanks so much in advance :D
You could populate the select element with all the possible options in $form-init(), as you are doing. But then modify the element during $form->setDefaults($defaults), at which point you will know the current value of the element and determine which options are no longer appropriate: if the element value is "Assigned", then remove the "Awaiting" and "Spam" options.

Categories