Laravel 5.2 PHP Build a social network - Input Validation
HTML-код
- Опубликовано: 6 фев 2016
- Build a complete Social Network Web Application with PHP Laravel. In this video we'll add some input validation to make sure that only valid forms are submitted to our controller.
Complete source code: github.com/mschwarzmueller/la...
Complete Laravel-course on Udemy: www.udemy.com/modern-web-deve... (Coupon: RUclips1)
In this course we'll build a neat little social network and cover the basics of developing with Laravel.
You can follow me on Twitter (@maxedapps), Facebook ( / academindchannel ) or visit our Website (www.academind.com).
See you in the videos! Наука
The best thing I like that you discuss the problems that you face as tutorial goes.
Also, You tell us what you will be doing in the next step. It's really awesome, because that way we can also use our brain and pause video to test if we can do it.
Most other tutors just came with a plan and show you their own way and finish the tutorial, they don't give you a change to do or think.
I think it's really awesome you tell us your plan or what you gonna do in the next step, and also WHY.
I'm loving your series, spent all saturday watching it and coding along. Thanks for your efforts mate!
Amazing to hear that - I'm very happy that you're liking it Denin!
Just finished with the tut few hours ago and I can't thank you enough, learned a ton!
If you are using Bootstrap 4 you need to add 'is-invalid' instate of 'has-error'
Here is the right validation-form for the Sign up email.
I really enjoy watching your tutorial so far, you make Laravel feel simple and also cover necessary details.
Awesome to hear, thanks so much for your great feedback!
You taught me something new where others tutorial didnt included, thanks
That's really amazing to hear, thank you so much! :)
You do an amazing job teaching laravel. Please continue. Need more tutorials like yours. Thanks so much.
You're awesome feedback means a lot to me Joseph, thanks a lot! :)
Just wanted you to know YOU"RE AWESOME! I just found your videos the past 2 days and it's more detailed and informative. I had a problem with the $errors not displaying and read the comment section and it's now working. Please post more videos! I'll enroll at your udemy course. Keep up the good work!
P.S. Sorry for my bad english.
+Anthony Dale Chua
Anthony, thank you so much for this awesome feedback! It's people like you who really keep me going! :) I got plenty of ideas for the future and I can't wait to create a lot of new videos, video series and courses! :)
This series is really helpful with my final year project, thanks!!
Great to hear that! :)
this is the best, no one make a very detailed tutorial like this. Thank you :)
What more can I say but thanks so much for this amazing feedback Wahyu, this really means a lot to me!
Your tutorials are great. I've learned a lot from you. Keep up the good work. Thank you.
I'll try my best, thanks so much for your great feedback!
hii i m new to laravel and php but dis tutorial really helping me a lot :) ur teaching style is great !! thank you very much sir for dis tutorial.
Am I the only one who cracked up when you corrected yourself at 4:05 by saying '"Angular2?? !!' Laravel ships with .... " ?
Btw, thanks a million for these awesome tutorials!
I used to record a lot of Angular videos at that time :D
Thank you for your comment, happy to read that you like the videos!
You are awesome! Really like your step by step procedure to reach the goals!
+Parth Chokshi
Thank you Parth, I'm really happy to hear this! :)
You seem to be very experienced in such topics, everything I was wondering is very well covered
Great to hear that, thanks so much!
sir, this is very clear and easy to understand this tutorial thanks........
Thank you very much for taking time and making excellent tutorials. I am new to Laravel and learning from laracasts. I would love to see more tutorials on Laravel 5.2. Are you thinking to continue this series. once again thank you.
+Irfan Ullah
Hi, I'll continue creating Laravel tutorials. If you're interested in a complete course right now, you might also want to give my Udemy course a try: www.udemy.com/modern-web-development-with-laravel/
Use coupon RUclips_FEB to get it for just 17$
You're a great teacher. Thanks for this video. You got me as your subscriber.
Thanks so much John! :)
Your tutorials are great!. Keep up the good work. I am new to frameworks and right now am learning laravel and then symfony.....for validation i also like to add html5 validation and using input name fields like 'email' instead of text. This way users using modern browsers dont even need to submit before an error pops up.
+This guy
Hey, I'm happy you're liking my videos.
You are absolutely right about the HTML5 validation. It's missing in the videos just because I'm only focusing on Laravel validation. In my opinion you also always should have some form of server validation since I can't be tricked - which client side validation often can be. Nonetheless using HTML5 validation is definitely a good practice, as it also helps mobile phones show the right kind of keyboard - additionally to its main function: validation ;-)
So, very good comment!
Hi guys! just to add more cool validation unless we using a list and display all errors in one, div, we can use bootstrap help block and has-error class to display errors on each input. so if someone would like to do so this the code for that.
First Name
@if ($errors->has('first_name'))
{{$errors->first('first_name')}}
@endif
I hope it can help others.
+jacinto Joao Thank mate, it works properly!
yes btw great effort. you covered each and every aspect of building a fully functional application.
but if the error is in register form, the forms in login form appear to have an error help block too, how do i change this?
can you be more specific!
because you mixing login form and register form
nevermind, i fixed it by making a manual validator for each form, its on the validation docs
Thanks.... I have first time implemented it very easily.
That's great to hear! :)
Thanks for your efforts! Great channel!
So happy to read that you like the channel Didzis, thanks so much for your comment!
Great tutorial :) thanks a lot mate!
Happy to hear you're enjoying them Chameera! :)
Thank you sooo much.. You are a great teacher..
+Rajni Nair
Really awesome to hear this, many, many thanks! :)
seriously man, I love your tutorial series..Just curiosity which tool have you used to make this tutorial series?
+Soft Pdf
I'm very happy to hear this! :) I use PHPStorm which really is great (though not free) IDE for PHP development.
"Angular 2....." "I said it again!" cracks me up. haha.
you are the great sir umaah
It's great to hear that you're liking it - thanks for your amazing feedback!
Hello, I love you, thank you!
Love you, too! ;) Happy to hear you're liking it!
For who has problem with showing the same errors for both forms:
You can also use different name for Signup and Sign In variables to avoid problem. For example signin-email and signin-password for Sign In form and signup-email and signup-password for signup. Then you just need check if $errors->has('signin-email') or $errors->has('signup-email')...
Yep, many thanks for sharing this - certainly helpful to others! :)
i wish if there is an easier solution ...
Then what about the database?
is there no other way to do this? I tried changing the names, but then the error messages that appeared were using my new names, and for some reason even though I changed the names for the validation part to, it started to not recognise anything written in the sign in form
In controller you have getName, postName etc. (Laravel 4 types)
I think that is also not required either in version 5 and after.
Also it is not best practice to write validations in controller. In big apps someone in group or a person in a team of developers will be confused. The role of controller is not that. So what do you think?
I am really learning a lot from these presentations - they are not just click and follow - it's more listen, learn and do at just the right pace. Big Canadian Thank you!
Everything has been going well until this snag when I change the form-group class from:
Undefined variable: error (View: C:\xampp\htdocs\cmt
esources\views\welcome.blade.php).
Skipping this step of form validation for now, but I am very interested in why? Thoughts?
+Bruce Betts
Hi Bruce,
great to hear that you're enjoying the videos and learning a lot! Many thanks for this great feedback!
It's a tiny yet important error that snuck in: It's $errors, not $error (the s at the end is important).
Then it should work :)
+Mindspace Ahhh! These weak eyes fronting a tired old brain. Thank you!
I have also to add (correct me if I'm wrong)....that the current bootstrap version has some changes I think.. the has-error class is not working anymore.. you have to move the " {{ $errors->has('email') ? 'is-invalid' : '' }}" in the form-control class in the input. as you notice the has-error is now is-invalid..
thank you!
both don't work...
There is one problem. If you put wrong credentials in your signup 'email' or 'password' field, it shows up in your "Signin" form as well. One option would be changing the 'name' attribute of field, but I don't think that right. Is there any other option?
Hi there, great tutorial.
Have a problem with the old() functionality... For example, if I fill only the email field on the signup form and press submit the errors are showed related to the signup form but both email fields, in sign up and in sign in, get filled with the data entered.
Any suggestion?
Thanx
Hi, I think the best solution simply is to split the forms into two different forms or just make sure that you use distinct form input names in both forms
That was what I also thought. Thank you.
great video.. what editor is this?
Thank you! I'm using PHPStorm by Jetbrains
hello sir, first thank you for the great tutorial....
Sir, I have some issues that my Input Validation are not working properly and I have done exactly you have shown, the errors are not appearing on the division tag, input fields are not showing red coloured error type that yo have added all in 5th video.
Please help.
Thank you.
I'm happy to read that you enjoy my videos :)
Regarding your issue: Are you still using the "web" middleware in the routes.php file? You shouldn't do that anymore (see annotation in video). Try removing it, everything should work then.
Had the same problem. Deleting the Route group middleware did fix the problem. Thanx for your reply.
I was having the same problem, and solved after removing the Middleware from the Route.php. But, I can remember that you told to write the Route inside the middleware, if want to check the validation and now you have asked to remove it. Anyhow, Thanks for your help.
I want to add Gender input too in form using Radio input .but i don't know how to validate and insert it in database , using laravel
Can you please tell me how can i add a background image to my login form..I tried using Jumbotron but it doesn't seem to work..:(
Help me, array $errors not appearing, I put all routes in the 'web' middleware group...?
+Максим Дуров
Hi, this is a little "issue" with the newest Laravel version. This group is no longer needed (see the overlay in the video) and using it will cause the effect you're describing. Try putting all your routes outside of this group (you may just delete the group afterwards) and then it should work.
+Mindspace Thank you very much! It works really.
+Mindspace Thanks, I have the same issue but fixed now!!
Okay, great to hear that it's working now ! :)
have you pass these videos?
please help send me the welcome.blade.php file @umair.amazingspidey@gmail.com
I have a BadMethodCallException while using validate() using v5.2.45 on Mac v10.11.6.
I have tried the following
1. Compare code from Github for mistakes doesn't work
I have a problem.After I used Request::old('email') and so on for preserving previous input values, and I submit the Sign Up form with taken/invalid email and other fields those values are automatically inserted to Sign In form and both forms have error highlighted text fields and of course the error msgs are displayed.What is such error?Please explain it to me with possible solution.Thank you.
I have the same problem, have you solved it yet?
thats because input fields in signin form and signup form both has same names. change their names to name="signUpEmail" and name="signInEmail"
Thank you for useful tutorial, I have a problem, doesn't show validation error message. How to show error messages?
I checked every code and php.ini error messages. But?
+Batjargal Enkhkbat
Hi, might be related to a change with Laravel 5.2 You no longer need the web middleware in the routes.php file. Try to put all your routes outside of that group (you may remove the empty group thereafter).
+Mindspace Thank you, Your's right. Laravel is developing quickly.
Thank you again Wonderful code.
Thank YOU!
Hi, can I get the full source code. I am wondering why my $this->validate() is not function at all.
Hello. I'm pretty sure that I have typed everything correctly but I'm not outputting validation errors. I'm not getting an error and I know it's checking validation because a new user isn't being created in the database. What do you think? Thanks!
+Michael Medel Nevermind! FIxed! Guy below had the same problem. Thanks!!!
+Michael Medel
Great to hear that it's working now :)
+Mindspace When I finish this tutorial I am def going to take your Laravel course on Udemy to help solidify all of these details. Thank you.
I'm extremely happy to hear that you really seem to like it :) Thank you!
im getting this error htmlentities() expects parameter 1 to be string, array given help please
As of Laravel 5.2+ this is no longer true. The "web" middleware group is no longer required, place your routes outside of that group. this means we dont need to use midleware group anymore ?
yep
can someone help me please ?
im new in php and laravel so im start to folow this very helpfull tutorial but i got a problem when im outputing the error, the error message doestn appear, im sure to follow the instruction in this video properly. sorry for my bad english
thanks before GBU
+Taufik Hidayat
Hi, do you get any errors? If not, could you share the code of view and controller?
i dont get any error, the validation is succes , when i input the same email it doest'n write in database also when i input null in the field,
but the error doest'n appear T_T
View
@if(count($errors)> 0)
@foreach($errors->all() as $error)
{{error}}
@endforeach
controller
$this->validate($request, [
'email' => 'required|email|unique:users',
'first_name' => 'required|max:120',
'password' => 'required|min:4'
]);
Hi, I learned, that this is due to a change in Laravel 5.2.1. The 'web' middleware is no longer needed, and using it, will apply it twice, hence breaking the app (in this regard).
Therefore, try putting all your routes outside of that middleware and let me know if this works.
+Mindspace Wow, its work now thank you for explanation sir , i really enjoy your video hope soon i can buy your tutorial sir, thanks before god bless
Great to hear that Taufik! :)
Hi, everything fine until here but with the new updates I have a couple of problems:
- when submitting one form the validation applies to both forms or more precisely I guess that the error message doesn't make a difference between the signup email and the signin email and both forms get the "red border";
- by using Request::old('email') and Request::old('password') it fills both forms as again it doesn't make a difference between the two forms
is there an easy way to solve this?
Small update:
I managed to solve the first point by creating a "Validator" instance and renaming the error bag for the specific validation:
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users',
'first_name' => 'required|max:120',
'password' => 'required|min:4'
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator, 'signup');
}
Still need to manage the Request::old() issue. I need to find a way to make a distinction between the two forms.
+Dimitri136
Hi, I think the easiest way would be to pick unique name for both forms. Like prefixing the input field names with su (signup) and si (signin) for example.
Mindspace Obviously, don't know why I haven't thought of that :)
Anyway, great job and thank you for these tuts!!
+Mindspace Actually I have a new issue. If I set the name attribute to 'su_email' I get an error when he tries to check with the database if this email already exists, during the validation process, as it will look for the column 'su_email' in the users table which doesn't exist as it is simply named 'email'. Any idea on how to tell to the Validator that my column is named 'email'?
That's very easy, just add the column name after the table name, separated by a comma.
Like this: 'required|email:TABLE,COLUMN'
More info here: laravel.com/docs/5.1/validation#rule-email
Sir i am getting always validation fails. i am new to laravel please help me. I am so eager to learn this type of stuffs
Hi,
I need some tutorials on Hashing (change/update old passwords). I haven't seen this option in your this blog tutorial .
Well, hashing is included if you have a look at the user signup video => bcrypt() makes sure that the password is hashed. Password changing isn't covered, that's correct. Might be covered in some future video.
+Mindspace will be waiting for that. Thanks sir
Hi I am using bootstrap modal to display sign up and sign in form. I typed the following under a section:
@if(count($errors) > 0)
@foreach($errors->all() as $error)
{{$error}}
@endforeach
@endif
When I just click submit, the modal disappears and goes back to the home page. Which it should, but not displaying the errors on the modal. Can someone help me out here?
I don't know if you're still interested in the answer but you gotta take your routes out of web group. With 5.2 all the routes are automatically included in web group if i recall it right. So go to your routes file and delete Route::group(['middleware' => ['web']], function () {} without touching actual routes, that should fix it.
I wrote:
@if (count($errors) > 0)
@foreach($errors -> all() as $error)
{{$errors}}
@endforeach
@endif
But an error occrus: htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\lar
esources\views\welcome.blade.php)
What to do sir?
not sure if you worked this out, but you are using $errors ( an object) instead of $error you defined in the foreach
Hey there. Everything was going smooth till it showed me attempt() method does not exist.. Any resolution to this problem?
+Suraj Gusain
Hm, that sounds strange. Do you have the Auth facade import at the top of the file? Could you post the complete error message (only the message, not all the code lines below it) here?
Yes I had it. However, I had to include the Auth in kernel.php to make it work.. it was missing. Thanks for the reply. :-)
Ah okay, great to hear everything's working now :)
where code for string " The email has already been taken " or is it generated by laravel ?
That's a default message output by Laravel. You can change it in the resources/lang folder
I have created a Authentication page according to our tutorial, form validation is working but does not show any error message. how to solve that problem.
No validation errors you mean? Are you redirected back in the case of invalid values? Do correct values lead to the correct submission of the form (i.e. other page being loaded)?
Maybe some errors in your code regarding the displaying of the messags?
yes its redirected back in case of invalid user but it doesnot show any error
Then you might have some error in the view. Make sure to compare it to my code (link in video description). Maybe some tiny error leading to this area never being displayed
I have created a Authentication page according to our tutorial, form validation is working but does not show any error message. how to solve that problem. please give some tips brother..
if i remove the web middleware from the route group then my welcome page doses not show...
Removing the web middleware is required. Are you getting an error?
i have same problem
can u plz tell me the name of IDE which u used in this project
Sure, I use PHPStorm by Jetbrains :)
can we use emmet in php file?
I'm currently following the tutorial while coding it also.. I found an error also,,, for example i want to signin without inputting my credentials,, it gives an error and to the signup also(which is not good) ... I think it might be because we named them the same? like in email in signin and in sign up... Anyways, see you next episode... It might be fix there It's been a year.. LOL
Can you tell me how to use inArray validation rule
Here's an example: themsaid.com/laravel-5-2-23-20160305/
I think that example is wrong to begin with. It gives positive result for both cases .But it shouldn't give positive result for second case.
Yeah ... this is no coding forum though, I can only go that far beyond the scope of a video ;)
I didn't test this example, it does show the usage though - which is what you wanted to know
Thanks. It seems I need to code that part manually , there is just no other way.
03:04 Angular2? I thought we did Laravel haha
That happens if you're recording videos about two different topics after each other :D
guys $errors->has('email') ? 'has->error':' ' is not responding!!!! please anyone to help??
It should be $errors->has('email') ? 'has-error':' ' (no -> ). Does this work?
Validation in controller, database job in controller. What about mvc? what about Single Responsibility Principle?
People if anyone of you want to learn how to work with laravel, you need to find another place.
How to logout?
Auth::logout() => Will also be shown in this series
Mindspace Thanks. This series is very helpful for 5.3 although the minor differences.
Hi there great video, keep it up.
Looking at you playlist, we are almost done with the user saspect of this app.
I've been fallowing this tutorial and adding my own feature mainly styling wise.
I love your step by step explanation of what you're doing to the point that i'm begging my mum for some money to buy your online tutorials.
That being said:
I would like the add an extra feature :
I want to user to be able to log in with his/her username or his email
I added the username to the database (migration) and to the welcome page
not mush changed in the route (so i didn't add anything there)
if tried something like this in the postSignIn function
if(Auth::attempt($userEmailData) )
{
return redirect()->route('dashboard');
}
else if(Auth::attempt($userUsernameData) )
{
return redirect()->route('dashboard');
}
return redirect()->back();
This doesn't seems to work
any advies ?
All of google code looks so complicated and i think you may have a easier solution
Thanks in advance
Ps no pressure answer when you can, i'll just continue with the series in the meantime
+Amara Kromah
Hi again!
I'm using these assumptions for the solution:
-mail is (possibly) stored in $userEmailData
-username is (possibly) stored in $userUsernameData
- password is stored in $password
The database user table has the following fields:
-username
-email
-password
Solution:
if (Auth::attempt(['username' => $userUsernameData, 'password' => $password])) {
echo "success with username!";
}
elseif (Auth::attempt(['email'=> $userEmailData, 'password' => $password])) {
echo "success with email!";
}
else {
echo "fail!";
}
So it's important that you pass an array to Auth::attempt() where you specify the database fields as well as the passed data.
I hope this works for you, let me know if you're still having troubles with this.
you said Angular2 in validation section lol
Yeah I know ... that's the problem if you're switching context a lot ;)
You are saying "Angular2" at 3:05 whereas it should be laravel!!!
+hamza nasim
Happens if you're recording multiple videos directly after each other ;)
He does it soo easily ,. but it take 2 hrs for us to get it right,,, not good videos , why would you make it for mac OS anyway , 76% of pcs use windows and so do I, FUCKING shit no use psent 1 day couldnt validate through this method
Sorry to hear you didn't enjoy the video Naman. The steps actually are the same on Windows/ Mac - and whilst 76% of machines might use Windows, 95% of devs use Linux/ Mac
welcome.blade.php
@extends('layouts.master')
@section('title')
TITLE : WELCOME
@endsection
@section('content')
@if(isset($errors) && count($errors) > 0)
@foreach( $errors->all() as $error )
{{ $error }}
@endforeach
@endif
Sign In
Sign In
Sign UP
Sign Up
@endsection
Routes.php
UserController.php
why are you talking like this, im using Windows and everything works fine at me,if you didn't like the video so go find a laravel tutorial on windows, don't comment like this!