Mimes Validation not working correctly in Laravel - laravel

My webpage contains a file for upload, and I want the file uploaded to only be either pdf, doc or docx.
My form tag also has enctype="multipart/form-data"
My html looks like:
<div id="cv_upload" class="row">
<div class="col-xs-12">
<input type="file" name='cv'>
</div>
</div>
The $rules array associated with this is as follows:
'cv' => 'mimes:application/pdf,application/doc,application/docx|required'
And finally my messages looks like:
'cv.required' => 'A selection for C.V. is required before proceeding.',
'cv.mimes' => 'CV must be of the following file type: pdf, doc or docx.'
The only problem with this is, even after I upload a pdf or doc, the message I receive is the one for required. I have no idea why this isn't working as expected. I also tried removing the 'application/' but that yields no success either. Please help.

Make sure your form has an attribute enctype="multipart/form-data"
Also, if you use mimes validation the format must be 'mimes:jpeg,bmp,png'
https://laravel.com/docs/5.6/validation#rule-mimes
You can read about the enctype attribute here : What does enctype='multipart/form-data' mean?

In form try put files="true".
In validator try put the validations eg required|mimes:doc,docx,pdf
https://laravel.com/docs/5.4/validation#rule-mimes
Edit: When you use mimes:application/doc, actually is mimetypes:application/doc

Hi i had the similar issue , everything was correct after hiting following cmds issue got resolved
php artisan cache:clear , php artisan config:clear , php artisan route:clear

Use validation as below if you are facing any issue with .docx extension files
'cv' => 'mimetypes:application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/octet-stream,application/pdf'
This will help to validate doc, Docx, and pdf files.
To show proper error message use the below code:
public function messages ()
{
return [
'cv.mimetypes' => 'The cv file must be a file of type: pdf, doc, docx.'
];
}
Reference: Laravel 7

In case someone gets the same problem, you have to use the file exstension, but not the mime type like so:
'file' => 'required|file|mimes:pdf,doc,docx'
The full list of the extensions can be found: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

Related

Cypress-Xpath: Correct Xpath syntax for id? It can't find it

Tried cy.get(#Username) , doesn't work- cypress says it can't find it. could it be related to uppercase letter?
Installed Xpath plugin and used this
cy.xpath('//input[#id="Username"]') but it didn't work.
<input type="email" class="form-control" autocomplete="off" data-gd="identity-login-local-form-username" autofocus="" data-val="true" data-val-required="The Username field is required." id="Username" name="Username" value="">
Please before giving -1 , please explain what I need to improve. Thanks!
After downloading xpath plugin, did you add require('cypress-xpath') in your project's cypress/support/index.js file?
According to your example, code below should find the Username
cy.xpath('//input[#id="Username"]')
cy.get('#Username')
The capital letter may be causing the problem. Usually ids have a small letter.
Try using the data-gd attribute instead.
cy.get('[data-gy="identity-login-local-form-username"]')
If that does not work, you may have some shadow DOM before the <input> that blocks the search, in which case you can search inside the shadow like this
it('tests the input', {includeShadowDom:true}, () => {
cy.get('[data-gy="identity-login-local-form-username"]')
})
I tested with a capital letter cy.get('#Username') and cy.xpath('//input[#id="Username"]') - both worked for me, so likely there is shadow DOM or an <iframe> on your page.
Is it possible that the page has a default namespace? If the page is served as XHTML, it may have a default XML namespace, in which case the input's name is not simply input.
If that is the problem, then you could declare the http://www.w3.org/1999/xhtml namespace and associate it with a prefix, e.g. xhtml (I don't know cypress so not sure how you'd do that), and then query for //xhtml:input[#id="Username"]. An alternative is to query for an element whose local name is input in any namespace at all, e.g. //*[local-name()='input'][#id="Username"]
In case your username field is under a shadow DOM which means other fields will also be under the shadow Dom, it would be advisable to write includeShadowDom: true in your cypress config file to avoid repetition(cypress.json if cypress version < 10; cypress.config.js if cypress version > 10), then directly use the command:
cy.get('#Username').type('username-text')
In case your username field is under an iframe, you can get the cypress iframe plugin
To install npm install -D cypress-iframe
Go to cypress/support/commands.js file, add the following:
import 'cypress-iframe';
// or
require('cypress-iframe');
In your test write:
cy.iframe('#my-frame')
.find('#Username')
.should('be.visible')
.type('username-text')
I can also confirm the way you are selecting the Username input element is correct.
If you suspect shadow DOM is interfering with your test, the best way to debug IMO is to
inspect your DOM around the <input>
look for a parent element that has #shadow-root below it (in bold)
change the test to include this parent
add the .shadow() command after the parent to break through the barrier
cy.get('parent-with-shadow-root')
.shadow()
.find('#Username')
This debugs and confirms your issue. Everything else, e.g setting global config etc can be done after you know what you have to deal with.
After I tried suggestions and people's confirmation that my xpath was correct, I shifted my focus on the error I got while Cypress was trying to find the element. The error I got was uncaught exception.https://stackoverflow.com/questions/53845493/cypress-uncaught-assertion-error-despite-cy-onuncaughtexception
This error occurs when a module fails to load due to some exception. The error message above should provide additional context. A common reason why the module fails to load is that you've forgotten to include the file with the defined module or that the file couldn't be loaded.
Using ngRoute In AngularJS 1.2.0 and later, ngRoute has been moved to its own module. If you are getting this error after upgrading to 1.2.x or later, be sure that you've installed ngRoute.

I got error No hint path defined for error with diglactic breadcrumbs plugin?

In laravel 8, tailwindcss 2.1 app I want to add https://github.com/diglactic/laravel-breadcrumbs
so on install I modified config/breadcrumbs.php with :
'view' => 'partials.breadcrumbs::tailwind',
and as I want to modify defalt colors reading doc I created
resources/views/partials/breadcrumbs.blade.php file with content from the description
I filled file routes/breadcrumbs.php with breadcrumbs definitions I need.
But I got error :
No hint path defined for [/partials.breadcrumbs].
(View: /MyProject/resources/views/admin/dashboard/index.blade.php)
Looks like some path is invalid, but I do not see what I missed ?
I cleared cache, but it did not not help!
Thanks !
Looks like if I want to use and edit partial file /MyProject/resources/views/admin/dashboard/index.blade.php, I have not to set tailwind in 'View' parameter , just :
'view' => 'partials.breadcrumbs',

Reasons for erratic behaviour loading constants from env->config->blade in Laravel?

I am running Laravel Framework 5.8.37 (Haven't had time to upgrade yet...)
I have Sstripe as a payment solution in my laravel app. After some refactoring I am about to re-implement my payment stuff from Stripe.
I keep all my keys and stuff in the .env. like:
STRIPE_PUB_KEY= pk_test_MyKeYs
STRIPE_SECRET_KEY= sk_test_MyKeYs
STRIPE_END_POINT_SECRET = whsec_MyKeYs
In the app.config I define them:
'stripe_pub_key' => env('STRIPE_PUB_KEY'),
'stripe_secret_key' => env('STRIPE_SECRET_KEY'),
'stripe_end_point_secret' => env('STRIPE_END_POINT_SECRET')
Then from my code I call:
var stripe = Stripe("{{ config('app.stripe_pub_key') }}");
Everything SHOLD work. But what happens? I get the error:
Uncaught IntegrationError: Please call Stripe() with your publishable key. You used an empty string.
The call to the config returns empty string! Strange, well it will get worse. To be able to test better I've just added this to my blade:
<h1>My response:{{ config('app.stripe_pub_key') }}</h1>
It does indeed return empty string. Now to the real strangeness. If I change to:
<h1>My response:{{ config('app.stripe_secret_key') }}</h1>
... just to test. It returns the secret key!!! 😤 - It also works with the end point secret. It seems to just be the pub_key that does not work... Also if I make a random new one it does not seem to work...
What can this be?
You probably need to recache your config using php artisan cache:clear && php artisan config:cache.
Also, remove the spaces in your env file are the =.

Inserting a translation into a placeholder with Emblem.js

I'm trying to write a login form with ember.js/emblem.js. Everything works, unless I try I18ning the placeholders like so:
Em.TextField valueBinding="view.username" placeholder="#{t 'users.attributes.username}"
Em.TextField valueBinding="view.password" placeholder="#{t 'users.attributes.password'}" type="password"
I get the same response if I try:
= input value=view.username placeholder="#{t 'users.attributes.username}"
= input value=view.password placeholder="#{t 'users.attributes.password'}" type="password"
In both cases, I get this error message:
Pre compilation failed for: form
. . . .
Compiler said: Error: Emblem syntax error, line 2: Expected BeginStatement or DEDENT but "\uEFEF" found. Em.TextField valueBinding="view.username" placeholder="#{t 'users.attributes.username}"
I assume this is happening because I'm trying to compile something from within a statement that's already being compiled. In evidence of this, I don't get the runtime error if I change the code to:
input value=view.username placeholder="#{t 'users.attributes.username}"
input value=view.password placeholder="#{t 'users.attributes.password'}" type="password"
But the downside is that the value bindings no longer work, which still leaves the form nonoperational. Is there another way of approaching this problem that I haven't considered?
As Alexander pointed out, this is a limitation of Ember and Handlebars. The workaround that I've been using is to make the placeholder refer to a controller property which then returns the translated string. For example:
{{input
type="text"
value=controller.filterText
placeholder=controller.filterPlaceholder }}
And then in the controller:
filterPlaceholder: function () {
return i18n.t('players.filter');
}.property('model.name'),
This is beyond the scope of what Emblem can do because it's an inherent limitation of Ember+Handlebars. What you're trying to do is use the input helper and, inside the helper invocation, use another helper t to get the value for the placeholder option. You can't (presently) do this in Ember, so Emblem's not going to be able to do that for you.
edit: you should try the Ember i18n library. I haven't used it yet, but it seems like what you'll want to do is to mix in the TranslateableAttributes mixin into Ember.View, like:
Ember.View.reopen(Em.I18n.TranslateableAttributes)
and then in your emblem template you can do something like
= input placeholderTranslation="button.add_user.title"
I noticed a typo in the first placeholder="#{t 'users.attributes.username}". It's missing the closing single quote.
The Emblem syntax error, line 2: Expected BeginStatement or DEDENT but "\uEFEF" found. can be misleading. I've found that the error is somewhere else entirely to what was being reported. For instance, linkTo without a | for plain text reports a similar error.
You should use the views to format things and drop them into the template. Controllers are not meant to know what happens at the template.
You would also want that to be a property, so i18n will work just once and then you can use the cache version.
Templete:
{{input value=view.username placeholder=view.usernamePlaceholder}}
{{input value=view.password placeholder=view.passwordPlaceholder type="password"}}
View:
export default Ember.View.extend({
usernamePlaceholder: function() {
return Ember.I18n.t('users.attributes.username');
}.property(),
passwordPlaceholder: function() {
return Ember.I18n.t('users.attributes.password');
}.property()
});

Paperclip Giving Required field error

in my project i have form in that there is file field to upload file and I am using paperclip gem for that and add only validation 'validates_attachment_content_type' but when i submit a form without any file selected it gives error of 'validates_attachment_content_type',
it should not give any error as i am not add validation 'validates_attachment_presence'. I am very confused for what it giving error of 'validates_attachment_content_type' when i submit a form without any file uploaded.
after googling for the same i got answer for this prob just we have to add :allow_nil => true in validation
for eg.
validates_attachment_content_type :logo, :content_type => ['image/jpeg','image/png','image /jpg','image/gif'],
:message=>"Image file must be of .jpeg,'.jpg', '.gif' or .png type",:allow_nil => true
:allow_nil => true
is realy work great and very easy to test.YOu can put it after your validation.

Resources