How to make Validation in Laravel 5 - laravel

I am using Laravel 5. And I am a beginner.
In my OrganisationsController I have a method
public function create()
{
return view('organisations.create');
}
In my routes.php I have
Route::model('organisations', 'Organisation');
Route::resource('organisations', 'OrganisationsController');
In my create.blade.php I have a code like this
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
#extends('app')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Organisation</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong>
There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!--<form class="form-horizontal" role="form" method="POST" action="{{ url('/organisations/store') }}"> -->
<form class="form-horizontal" role="form" method="POST" action="{{ url('/organisations/store') }}" accept-charset="UTF-8">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}">
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
I need to validate email and name field
I don't know where to write a validation. Please suggest a example for this. I am using laravel 5. I have searched so many sites. But I cannot get solution for this. Please tell me where to write validation code.

You should use FormRequest for this:
http://laravel.com/docs/5.0/validation#form-request-validation

Related

Method 'POST' error 419 on local server Laravel

When i submit a post form on my page it doesn't work, it redirects me on the action route with error 419, this is an example of my form:
<form action="{{route('client.login')}}" method="POST">
#csrf
#method('POST')
<h4 class="login-title">Login</h4>
<div class="login-form">
<div class="row">
<div class="col-md-12 col-12 mb--20">
<label>Email*</label>
<input class="mb-0" type="email" name="email" value="{{ old('email') }}">
</div>
<div class="col-12 mb--20">
<label>Password</label>
<input class="mb-0" type="password" autocomplete="current-password" name="password"
value="{{ old('password') }}">
</div>
<div class="col-md-12">
<div class="d-flex align-items-center flex-wrap">
<button type="submit" class="btn btn-black me-3">Login</button>
<div class="d-inline-flex align-items-center">
<input type="checkbox" id="remember" name="remember" class="mb-0 me-1">
<label for="remember" class="mb-0 font-weight-400">Ricordami</label>
</div>
</div>
#if (Route::has('password.request'))
<p>Password dimenticata?</p>
#endif
</div>
</div>
</div>
</form>
I've checked the csrf tokens, and they match.
I've the exact same code on my server-side files and they work perfectly, but doesn't work on my local server.
I can't find anywhere the log of this error.
EDIT:
My issue was in the .env file, I’ve written a ; rather than a :
Remove #method('POST') this line and try because you don't need to mention method="POST", you already mentioned method in form tag.
Welcome you in advance.
Try to add <meta name="csrf-token" content="{{ csrf_token() }}"> in the head of app.blade.php file

Build Laravel multistep form with foreach?

Good morning,
I want build a multistep form in Laravel and I discover a lot of tutorial on Youtube to make this but In my form, I use a Foreach because I build a exam system and I display Question and Response with foreach.
This is my Code :
<form method="POST" action="{{ route('stagiaire.session.test.store',['id'=>$eval->id]) }}">
#csrf
#foreach($questioncat as $category)
<div class="card mb-3">
<div class="card-header">{{ $category->nom }}</div>
<div class="card-body">
#foreach($category->getQuestion as $question)
<div class="card #if(!$loop->last)mb-3 #endif">
<div class="card-header">{{ $question->question_text }}</div>
<div class="card-body">
<input type="hidden" name="questions[{{ $question->id }}]" value="">
#foreach($question->reponse as $option)
<div class="form-check">
<input class="form-check-input" type="radio" name="questions[{{ $question->id }}]" id="option-{{ $option->id }}" value="{{ $option->id }}"#if(old("questions.$question->id") == $option->id) checked #endif>
<label class="form-check-label" for="option-{{ $option->id }}">
{{ $option->option_text }}
</label>
</div>
#endforeach
#if($errors->has("questions.$question->id"))
<span style="margin-top: .25rem; font-size: 80%; color: #e3342f;" role="alert">
<strong>{{ $errors->first("questions.$question->id") }}</strong>
</span>
#endif
</div>
</div>
#endforeach
</div>
</div>
#endforeach
<div class="form-group row mb-0">
<div class="col-md-6">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
</form>
So my question, is it possible to build a multistep form with foreach?
Thanks
I am at the thinking stage

The PATCH method is not supported for this route. when second form is submitted

I've been working on a CMS system for a website im building and it all went good untill I added 2 forms on the same page (both updating different things) and yet it still gives me the error in the title while the other form works fine?
The page banner is the one which returns the error and the 'normal' update works fine!
Web.php
Route::patch('/beheer/paginas/{product}', 'PageController#update')->middleware('auth')->name('beheer.pages.update');
Route::patch('/beheer/paginas/update-banner/{product}', 'PageController#update-banner')->middleware('auth')->name('beheer.pages.banner.update');
Also shows in php artisan route:list that it's a PATCH method route.
My view:
#extends('layouts.beheer')
#section('content')
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Pagina bewerken</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form action="{{ route('beheer.pages.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<div class="form-group">
<label>Zichtbaarheid:</label>
<select class="custom-select" name="page_hidden">
<option value="0" #if (!$page->page_hidden) selected #endif>Zichtbaar</option>
<option value="1" #if ($page->page_hidden) selected #endif>Verborgen</option>
</select>
</div>
<div class="form-group">
<label>Pagina naam:</label>
<input type="text" name="page_name" value="{{ $page->page_name ? $page->page_name : old('page_name') }}" class="form-control" placeholder="Pagina naam">
</div>
<div class="form-group">
<label>Pagina tekst:</label>
<textarea name="page_text" class="form-control">{{ $page->page_text ? $page->page_text : old('page_meta_title') }}</textarea>
</div>
<div class="form-group">
<label>Pagina title SEO:</label>
<input type="text" name="page_meta_title" value="{{ $page->page_meta_title ? $page->page_meta_title : old('page_meta_title') }}" class="form-control" placeholder="Pagina titel SEO">
</div>
<div class="form-group">
<label>Pagina beschijving SEO:</label>
<input type="text" name="page_meta_description" value="{{ $page->page_meta_description ? $page->page_meta_description : old('page_meta_description') }}" class="form-control" placeholder="Pagina beschijving SEO">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Banner</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<input type="hidden" name="image_section_banner" value="1">
<input type="hidden" name="page_id" value="{{ $page->id }}">
<div class="form-group">
<img src="{{ asset($page_banner ? $page_banner->image_large_url : asset('assets/img/banner-home.jpg')) }}" class="img-fluid" style="max-height:250px;">
</div>
<div class="form-group">
<label>Afbeelding:</label>
<input type="file" name="image" value="{{ old('image') }}" class="form-control">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
If there is any additional information needed please let me know!
There is an attribute missing on your 2nd <form>. Here is what you have:
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
But it should look like this:
<form action="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
Since you did not specify an action attribute, it is submitting to the current url which probably doesn't have a patch route defined.

bootstrap-vue not passing array in checkbox

I'm using bootstrap-vue in my laravel project to pass data from the view to the database with checkboxes (b-form-checkbox), i want to select from the permissions passed from the database and assign it to a role, which means a role can have more than one permission, unfortunately the data is not persisting to the database as an array because if i select more than one in the checkbox it only shows the first one clicked. Please i need help as i have spent too much time on this issue. This is my code:
edit.blade.php
#extends('layouts.master')
#section('content')
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Admin
<small>Edit</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Role</li>
<li class="active">Edit</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
#include('layouts.partials.message')
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="boxtitle">Edit Role</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<form action="{{ route('role.update', ['id' => $role->id]) }}" enctype="multipart/form-data" method="post" accept-charset="utf-8">
{{csrf_field()}}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="display_name" value="{{ old('display_name', $role->display_name) }}" class="form-control" placeholder="Name (Human Readable)" required>
<span class="help-block text-red">
#if($errors->has('display_name'))
{{ $errors->first('display_name')}}
#endif
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="name" value="{{ old('name', $role->name) }}" class="form-control" placeholder="Slug (can not be edited)" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="description" value="{{ old('description', $role->description) }}" class="form-control" placeholder="Role Description">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Permissions:</h2>
<b-form-group>
<b-form-checkbox-group v-model="permissionsSelected">
#foreach ($permissions as $permission)
<div class="form-group">
<b-form-checkbox id="permissions" name="permissions" value="{{ $permission->id }}">
<div class="form-group">
{{ $permission->display_name }} <em> ({{ $permission->description }})</em>
</div>
</b-form-checkbox>
</div>
#endforeach
</div>
</div>
<hr>
<div class="row">
<div class="col-md-1">
<div class="form-group">
<button class="btn btn-primary" type="submit" id="submit">
<i class="fa fa-check"></i> Submit
</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
</div>
<div class="col-md-11">
<div class="form-group">
<div class="checkbox">
<label>
<input name="redirect" type="checkbox" checked> Redirect to role list after submission
</label>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
</section>
#endsection
#section('vue')
<script>
var app = new Vue ({
el: '#app',
data: {
permissionsSelected: {!!$role->permissions->pluck('id')!!}
}
});
</script>
#endsection

How to get Ckeditor textarea value in laravel

i am using Ckeditor for blog posting in my project when i submit the form nothing i am get in controller can any one suggest me solution for that.
my view is looking like
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Post</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('store-post') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="category_id" class="col-md-2 control-label">Select Categories</label>
<div class="col-md-8">
<select class="form-control" id="category_id" name="category_id">
#foreach($categories as $category)
<option value="{{$category->url_name}}">
{{$category->category_name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-2 control-label">Post Title</label>
<div class="col-md-8">
<input id="post_title" type="text" class="form-control" name="post_title" value="{{ old('post_title') }}">
</div>
</div>
<div class="form-group">
<label for="post_content" class="col-md-2 control-label">Post Description</label>
<div class="col-md-8">
<textarea id="post_content" rows="10" cols="60" class="span8" placeholder="Image Title Goes Here" name="post_content"></textarea>
</div>
</div>
<div class="form-group">
<label for="p_url" class="col-md-2 control-label">Post Url</label>
<div class="col-md-8">
<input id="p_url" type="text" class="form-control" name="p_url" value="{{ old('p_url') }}">
</div>
</div>
<div class="form-group">
<label for="p_title" class="col-md-2 control-label">Meta Title</label>
<div class="col-md-8">
<input id="p_title" type="text" class="form-control" name="p_title" value="{{ old('p_title') }}">
</div>
</div>
<div class="form-group">
<label for="p_keyword" class="col-md-2 control-label">Meta Keyword</label>
<div class="col-md-8">
<input id="p_keyword" type="text" class="form-control" name="p_keyword" value="{{ old('p_keyword') }}">
</div>
</div>
<div class="form-group">
<label for="email" class="col-md-2 control-label">Meta Description</label>
<div class="col-md-8">
<textarea class="form-control" id="p_mdesc" name="p_mdesc" rows="3">
</textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-2">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</div>
<!--Error start-->
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!--error ends-->
</form>
</div>
</div>
</div>
</div>
</div>
my controller code is
public function store(Request $request){
/*$this->validate($request, [
'category_id' => 'required',
'post_title' => 'required',
//'post_content' => 'required',
'p_url' => 'required',
'p_title' => 'required',
'p_keyword' => 'required',
'p_mdesc' => 'required',
]);*/
$post=new Post;
echo $post_content=$request->input('post_content');
}
in previous project ie designed in CI i just use
$tc=$this->input->post('tc'); in controller for getting the Ckeditor value but in laravel i am not sure how to get it done.
Your view contain 2 name attribute for the post_content field (textarea). Please check.
You can do it like this -
{!! Form::textarea('tc', $tc,array('required', 'class'=>'form-control', placeholder'=>'Your message')) !!}
and then you will have to initialise it
$(document).ready(function () {
CKEDITOR.replace( 'tc' );
});
The documentation has clear examples.
In your Blade you should add ckeditor like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>A Simple Page with CKEditor</title>
<!-- Make sure the path to CKEditor is correct. -->
<script src="../ckeditor.js"></script>
</head>
<body>
<form>
<textarea name="editor1" id="editor1" rows="10" cols="80">
This is my textarea to be replaced with CKEditor.
</textarea>
<script>
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'editor1' );
</script>
</form>
</body>
</html>
So the javascript code triggers a replace of a textarea to the editor
Now for the retrieving data part
<script>
var data = CKEDITOR.instances.editor1.getData();
// Your code to save "data", usually through Ajax.
</script>
You need to create an endpoint if you want to send this data indeed trough Ajax. Don't forget to add a CSRF token
As mentioned by #user3888958,
<textarea name="tc" id="post_content" rows="10" cols="60"
class="span8" placeholder="Image Title Goes Here" name="post_content">
the textarea has two name attribute.
You could access the textarea content using the name attribute, remove any one name attribute and pass that in as a parameter to the request
$request->input('tc'); // OR
$request->input('post_content');
and to access the value of
<textarea class="form-control" id="p_mdesc" name="p_mdesc" rows="3">
</textarea>
you could access it using the name
$request->input('p_mdesc');

Resources