I am trying to add a simple "required" validation rule to a file input in a form. Logically, it should fail if the no file has been inputted (and the input is thus null). However, it manages to pass without issues, because the HTTP request ignores and thus doesn't validate the file when it is null....which defeats the purpose of having a "required" rule at all.
Even stranger, the code I'm working with allows for multiple files grouped up with "name='file[]'", and even here it ignores whatever inputs are null. For example, if I have 5 file inputs and only fill in 3 of them, the resulting array in the HTTP request will only have the 3 non-null inputs, regardless of order.
So I'm aware of what the issue is, but not really how to resolve it. Does anyone know how to force Laravel to recognize and thus validate null file inputs?
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Can someone please explain me why is highly recommended to use Django forms to get an html object instead of direct accessing from request.POST or request.GET?
In short: forms make retrieving and validating less error-prone, and often will perform a lot of logic that would otherwise require (a lot of) boilerplate code.
The items in request.POST (or request.GET) are send by the browser, so you can in no way guarantee that the items will (all) be present, and in the expected format.
Indeed, request.POST['somefield'] can raise a KeyError in case the POST request does not contain an key-value pair with somefield as key. Of course you can wrap this in a try … except …, but that easily will generate a large amount of code for a form with multiple items.
Even if the item is present in the request.POST, it is not per see in the expected format. For example if you use a number field (<input name="somefield" type="number">), you expect a number. But one can easily "forge" a HTTP request, where somefield contains 'blabla' or 'forty-two' instead. Django can validate the format of the fields. For an IntegerField [Django-doc] for example, it will require the value to be an optional minus sign followed by a sequence of digits.
A Django form will also "clean" the values. THis means that if you use an IntegerField, and later you access form.cleaned_data['somefield'], it will not have as type a string, but an int. You thus do not have to do the conversion yourself explicitly. This means that you can immediately add the values of two IntegerFields together for example, whereas if you do it yourself, these are first strings, and adding these together would concatenate the strings.
If cleaning or validating somehow fails, and a field contains an error, then a Form can explain what the error(s) are. You can access form.errors, which is a dictionary-like object that maps the names of the fields to a list of problems. It also makes it more convenient to list multiple errors. For example if a password should be at least 10 characters, and contain an uppercase, you can report both problems.
A ModelForm [Django-doc] also makes creating and updating an object more convenient. It has a .save() method for that. This thus will create a new record at the database in case the .instance wrapped in it was not yet saved, or update the record otherwise. It furthermore also will save ManyToManyFields (at least if you do not set commit=False). This is a tricky task since in order to save the objects in the many-to-many relations, you first need to create the object at the database side.
Finally you can also use a form to easily render it at the HTML side. There are Django apps like django-crispy-forms [GitHub] that can give the form another look. It thus makes it convenient to render the form, and easily change the appearance if you change your mind.
I have this solution that helps me creating a Wizard to fill some data and turn into JSON, the problem now is that I have to receive a xlsx and turn specific data from it into JSON, not all the data but only the ones I want which are documented in the last link.
In this link: https://stackblitz.com/edit/xlsx-to-json I can access the excel data and turn into object (when I print document.getElementById('output').innerHTML = JSON.parse(dataString); it shows [object Object])
I want to implement this solution and automatically get the specified fields in the config.ts but can't get to work. For now, I have these in my HTML and app-component.ts
https://stackblitz.com/edit/angular-xbsxd9 (It's probably not compiling but it's to show the code only)
It wasn't quite clear what you were asking, but based on the assumption that what you are trying to do is:
Given the data in the spreadsheet that is uploaded
Use a config that holds the list of column names you want returned in the JSON when the user clicks to download
based on this, I've created a fork of your sample here -> Forked Stackbliz
what I've done is:
use the map operator on the array returned from the sheet_to_json method
Within the map, the process is looping through each key of the record (each key being a column in this case).
If a column in the row is defined in the propertymap file (config), then return it.
This approach strips out all columns you don't care about up front. so that by the time the user clicks to download the file, only the columns you want are returned. If you need to maintain the original columns, then you can move this logic somewhere more convenient for you.
I also augmented the property map a little to give you more granular control over how to format the data in the returned JSON. i.e. don't treat numbers as strings in the final output. you can use this as a template if it suites your needs for any additional formatting.
hope it helps.
I've been using data attributes with Google Tag Manager to track clicks on specific links.
By creating a Data Layer variable and setting its name to the appropriate dot separated path, for example gtm.element.dataset.alpha, the name and value of the data attribute are made available in the variables attached to Link Clicks.
However today I encountered a data attribute that would always have a value of undefined.
To my surprise it appears to be that GTA will always return undefined for variables for data attributes that include additional dashes.
So the following data attribute will always have a value of undefined:
data-alpha-bravo="example"
But these are fine:
data-alpha="example"
data-alphabravo="example"
Using additional dashes is perfectly legal in data attributes, and I haven't managed to find any documentation related to additional dashes not being supported by GTA.
Is this correct or is there something else at work?
Turns out you have to use camel-case for the variable name. So for a data attribute named data-alpha-bravo, the variable name would be:
gtm.element.dataset.alphaBravo
The Box v2 REST API appears to contain methods that return "full objects". That is, they return all the fields and properties of the object requested with one "simple" call.
When trying the official .Net SDK, it appears that if you don't specify fields by name in the "FoldersManager" or "FilesManager" (for example), you get minimal details of the objects returned.
Is there a way to make the request return all fields/properties? I realize maybe ItemsCollection is one you'd want to retrieve specifically, but the rest should really be included in one call (like the REST capability).
Thanks for any ideas!
-AJ
If no fields are specified in the request, the default fields are returned in the response (ie. what the API decides is the most commonly used fields). If a field is specified the API returns all required fields along with the specified fields (usually type, id, and etag).
There is currently no simple flag that will return all fields as this would likely be abused out of convenience. The only way to return all fields is to manually specify all of the fields you are looking for. If using any of the official SDKs, these fields names can usually be found in the object models
HTH
I use parsed values from previous responses for parts of the URL, or as the key in a JSON object. However, I can't seem to be able to use them as the value of a field in a JSON object.
When I right click on the key, I have the option to do that,
but not when I right click on the value field.
I have tried following the instructions in the documentation, but the options listed in the documentation aren't available in the value portion of the JSON object in the body. Would someone please explain how to do this.
Unfortunately, this is not possible because you're in a Number field. You can do this only on String fields for now. We're working on a fix for a future version, but it's not there yet.
The only alternative you can use is to format all your JSON, except this field, and convert to Text. The JSON structure will be preserved, and you can explicitly set the field inline with the text.
A quick screencast so show what I'm saying: http://cl.ly/141Y06380u2x
In this case you'll need to manually set a Content-Type: application/json header.