validation with username when i submit data to database - codeigniter

please help me if i remove the user validation then data are saved but when attach the validation with name then error show...
**controller code**
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category extends CI_Controller{
//this method will show category list page
public function index()
{
$this->load->view('admin/category/list');
}
//this method will show create category page
public function create()
{
$this->load->model('Category_model');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="invalid-feedback">','</p>');
$this->form_validation->set_rules('name','Name','trim|required');
if($this->form_validation->run()==TRUE)
{
//will save category in database
$formArray['name']=$this->input->post('name');
$formArray['status']=$this->input->post('status');
$formArray['created_at']=date('Y-m-d H:i:s');
$this->Category_model->create($formArray);
$this->session->set_flashdata('success','category added successfully');
redirect(base_url().'admin/category/index');
}else{
//will show error
$this->load->view('admin/category/create');
}
}
//this method will show category edit page
public function edit()
{
}
//this is for delete a category page
public function delete()
{
}
}
**Model code**
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category_model extends CI_Model{
public function create($formArray){
$this->db->insert('categories',$formArray);
}
}
**view code**
<?php $this->load->view('admin/header');?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0">Categories</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item">Categories</li>
<li class="breadcrumb-item active">Create New Categories</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<div class="card card-primary">
<div class="card-header">
<div class="card-title">
Create New Category
</div>
</div>
</div>
<form name="categoryForm" id="categoryForm" method="post" action="<?php echo base_url().'admin/category/create'?>">
<div class="card-body">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" id="name" value="" class="form-control <?php echo (form_error('name')!="") ? 'is-invalid':'';?>">
<?php echo form_error('name');?>
</div>
<div class="form-group">
<label>Image</label><br>
<input type="file" name="name" id="image">
</div>
<div class="custom-control custom-radio float-left">
<input value="1" type="radio" id="statusActive" name="status" checked="">
<label for="statusActive" class="custom-control-lable">Active</label>
</div>
<div class="custom-control custom-radio float-left ml-3">
<input value="0" type="radio" id="statusBlock" name="status">
<label for="statusBlock" class="custom-control-lable">Block</label>
</div>
</div>
<div class="card-footer">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
Back
</div>
</form>
</div>
</div>
<!-- /.col-md-6 -->
</div>
<!-- /.row -->
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php $this->load->view('admin/footer');?>
enter code here
please help me where we do mistake?
i think in name validation is not correct but i have no idea to trace error.
I found CodeIgniter form validation to show error message with load->view method, and will lost field error message if use "redirect". Currently I use one function to show form page, and another function to deal form post. class Users extends.

The problem is you mistake name your file input as name at here:
<input type="file" name="name" id="image">
Just change it to another name so the form_validation will work fine. Also, make sure you have loaded all needed libraries.
And one more thing, you should place:
$this->load->view('admin/header');
$this->load->view('admin/footer');
in your controller files, not in view files to follow codeigniter's MVC

Related

Web pages gets distorted when ever I try to specify a variable on my route

I tried parsing a variable through my route and but the CSS isn't rendered on the page and my page shows purely html=
Here is my web route;
Route::get('/create-transaction/{id}', 'AdminController#create')->name('Create Transaction');
And here is my controller;
public function create($id){
$users = User::orderBy('id', 'desc')->paginate(100);
return view('create-transaction')->with(compact('users', 'id'));
}
I get this sam error with every other web page on my website when. once I parse in a variable through my route, the whole page losses it's formatting. I've tried the same page without parsing variables and it works fine but it's a different case when I parse in a variable
Here is my view page;
#extends('layouts.admin')
#section('content')
<section class="content-top-margin page-title-small border-bottom-light border-top-light">
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-12 wow fadeInUp" data-wow-duration="300ms">
<!-- page title -->
<h1 class="black-text">Create Transaction</h1>
<!-- end page title -->
</div>
<div class="col-md-6 col-sm-12 breadcrumb text-uppercase wow fadeInUp" data-wow-duration="600ms">
<!-- breadcrumb -->
<ul>
<li>Dashboard</li>
<li>Withdrawals</li>
<li>Change Password</li>
</ul>
<!-- end breadcrumb -->
</div>
</div>
</div>
</section>
<section class="wow fadeIn border-top">
<div class="container">
<div class="row">
<div class="col-md-5 col-sm-10 center-col">
#if (session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
#endif
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<form action="/create-transaction" method="POST">
#csrf
<div class="form-group">
<div class="col-md-6">
<label for="exampleInputEmail1">Date</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-6">
<label for="exampleInputEmail1">Amount</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="exampleInputEmail1">Type</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
<div class="col-md-9">
<label for="exampleInputEmail1">Description</label>
<input type="text" name="current-password" class="form-control input-round big-input" required>
</div>
</div>
<button class="btn btn-black btn-small btn-round no-margin-bottom" type="submit">Create Transaction</button>
<!-- end button -->
</form>
</div>
</div>
</div>
</section>
#endsection

Getting error when click in my second post for the single page

When I am trying to open first post in single page, it's opening and when trying to open my second post in single page it;s showing "Trying to get property 'title' of non-object"
Here is code
FrontendController
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
}
single.blade.php
in that I am using same frontend controller for same page
#extends('layouts.frontend')
#section('content')
<div id="product-post">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="heading-section">
<img src="{{$post->featured}}" alt="" />
</div>
</div>
</div>
<div id="single-blog" class="page-section first-section">
<div class="container">
<div class="row">
<div class="product-item col-md-12">
<div class="row">
<div class="col-md-8">
<div class="product-content">
<div class="product-title">
<h3>{{$post->title}}</h3>
<span class="subtitle">4 comments</span>
</div>
<p>
{!! $post->content!!}
</p>
</div>
<div class="leave-form">
<form action="#" method="post" class="leave-comment">
<div class="row">
<div class="name col-md-4">
<input type="text" name="name" id="name" placeholder="Name" />
</div>
<div class="email col-md-4">
<input type="text" name="email" id="email" placeholder="Email" />
</div>
<div class="subject col-md-4">
<input type="text" name="subject" id="subject" placeholder="Subject" />
</div>
</div>
<div class="row">
<div class="text col-md-12">
<textarea name="text" placeholder="Comment"></textarea>
</div>
</div>
<div class="send">
<button type="submit">Send</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
You have to check whether the value is coming from table or not before getting exact column value. In that case if your table return empty result you can redirect it to 404 page.
Please refer the code below :
public function singlePost($slug)
{
$post= Post::where('slug', $slug)->first();
if($post) {
return view('single')->with('post', $post)
->with('title', $post->title)
->with('settings', Setting::first())
->with('categories', Category::take(4)->get());
} else {
// You can redirect to 404 page
}
}

Laravel form post - MethodNotAllowedHttpException

I'm attempting to post a form to the database, but I'm getting the following error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException thrown with message
The form is prepopulated with data and targets a post route. From research, I found out the issue is from posting to a get route, but the route i'm targeting is a post.
The form
<form action="/account/tenancy/{{$user->id}/" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<label for="property_address">Property Address</label>
</div> <!-- ./col6 -->
</div> <!-- ./ row-6 -->
<div class="row">
<div class="col-md-10">
<select class="form-control" id="property_address" name="property_address">
<!--Gets all counties from DB -->
#foreach ($properties as $property)
<option value={{$property->id}}>{{$property->address . ', ' . $property->town . ', ' . $property->county}}</option>
#endforeach
</select>
</div> <!-- ./ col-6-->
</div> <!-- ./ row-5 -->
<div class="row mt-2">
<div class="col-md-6">
<label for="landlord-name">Landlord Name</label>
</div> <!-- ./col=6 -->
</div> <!-- ./ row-4-->
<div class="row">
<div class="col-md-6">
<select class="form-control" name="landlord-name">
<option value="{{Auth::user()->name}}">{{Auth::user()->name}}</option>
</select>
</div> <!-- ./ row 3-->
</div> <!-- ./col-3 -->
<div class="row mt-2">
<div class="col-md-6">
<label for="tenand-name">Tenant Name</label>
</div> <!-- ./col=6 -->
</div> <!-- ./ row-4-->
<div class="row">
<div class="col-md-6">
<select class="form-control" name="tenant-name">
<option value="{{$user->name}}">{{$user->name}}</option>
</select>
</div> <!-- ./ row 3-->
</div> <!-- ./col-3 -->
<button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
</form> <!-- ./form -->
The controller method
//Renders Form
public function create($id){
$user = User::where('id', $id)->first();
$properties = PropertyAdvert::where('user_id', Auth::id())->get();
return view('/pages/account/tenancy/create', compact('user', 'properties'));
}
//Stores data
public function store(Request $request){
$Tenancy = Tenancy::create([
'tenant_id' => $request->user_id,
'landlord_id' => Auth::id(),
'property_address' => $request->property_address
]);
return back();
}
The tenancy model
class Tenancy extends Model
{
protected $fillable = ['tenant_id', 'landlord_id', 'property_address', 'accepted'];
public function user(){
return $this->belongsTo('App\User');
}
}
Routes
Remove Slash from end of your url in form action :
<form action="/account/tenancy/{{id}}" method="POST">
Use : /account/tenancy/{{id}} instead of /account/tenancy/{{id}}/
and try.
You are using the wrong method. As per your routes, change your method in your form to GET.
<form action="/account/tenancy/{{id}}" method="POST">
Try this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tenancy extends Model
{
protected $table = 'tenancy';
--------------------
--------------------
}
Remove slash after /{{id}} and make sure you have defined table name in model.

Deleting files that belong to a post?

I have two tables called 'posts' and 'photos'. The 'photos' table store the images for the 'post'. A post can have many photos so the relationship is one-to-many. I've managed to create the store method, I already create the view to show a post with its photos as well. Below is the function :
public function show($titleslug) {
$post = Post::where('titleslug', '=', $titleslug)->first();
$images = $post->photos;
return view('dashboard/posts/show', compact('post','images', $post, $images));
}
The show view :
<div class="contentpanel">
<div class="row blog-content">
<div class="col-sm-12">
<div class="panel panel-default panel-blog">
<div class="panel-body">
<div class="panel-body">
<h3 class="blogsingle-title">{{$post->title}}</h3>
<ul class="blog-meta">
<li><i class="fa fa-clock-o"></i> Jan 02, 2014</li>
</ul>
<div class="mb20"></div>
{!! $post->content !!}
</div><!-- panel-body -->
<div class="mb20"></div>
</div><!-- panel-body -->
</div><!-- panel -->
</div><!-- col-sm-8 -->
</div><!-- row -->
<div class="row blog-content">
<div class="col-sm-12">
<div class="panel panel-default panel-blog">
<div class="panel-body">
<h3 class="blogsingle-title">This post featured images :</h3>
<div class="mb20"></div>
<?php foreach ($images as $i): ?>
<div class="col-xs-12 col-sm-4 col-md-3">
<div class="blog-item">
<img src="{{asset('image/'. $i->image)}}" class="img-responsive" alt="" style="height: 150px;"/>
<div class="blog-details">
<ul class="blog-meta">
<li><a class="btn btn-sm btn-warning" title="Edit" href="#"><i class="fa fa-edit"></i></a></li>
<form action="#" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<li>
<button type="submit" class="btn btn-sm btn-danger" title="Delete" onclick="return confirm('Are you sure deleting this photo ?');"><i class="fa fa-trash-o"></i></button>
</li>
</form>
</ul>
</div>
</div><!-- blog-item -->
</div><!-- col-xs-6 -->
<?php endforeach; ?>
</div>
<div class="panel-body">
<div class="alert alert-info fade in">
<p>
<button type="button" class="btn btn-info">Add New Images</button>
</p>
</div>
</div>
</div><!-- panel -->
</div>
</div>
</div><!-- contentpanel -->
I want to be able to delete the images from the show view, how to do that ? Do I need to create a function to it ? If so, then how will the function looks like ?
That's all and thanks, sorry for asking too much
First, edit your delete form in the view by adding :
...
<input type="hidden" name="image_id" value="{{ $images->id }}" /> // Assuming that photo id is in your $images object
...
Then, you could create a POST route image/delete associated to delete function from your controller to remove the expected image :
public function delete() {
$image_id = $request->only('image_id');
$image = Photo::where('id', '=', $image_id)->delete(); // Assuming that you have a Photo model
return view('dashboard/posts/show');
}

Laravel 5.2 Admin create users like cms

I don't really understand the login and register authentication mechanism in laravel since they kind of hide it and I'm still new to this.I wanted to make more users using an admin account that I registered, using the user model that has already been provided by laravel. Is it possible? Do I need to make another model?
When I submitted the form
screenshot
I wanted it to store in the database and go to the users list page. But none of them happened. Thanks for the help.
routes
Route::get('/pengguna/tambah', 'Auth\UserController#viewCreateForm');
Route::post('/pengguna/tambah', 'Auth\UserController#store');
UserController
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Requests\UserDataRequest;
use Auth;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
public function viewCreateForm()
{
return view('/auth/create_user');
}
public function store(UserDataRequest $request)
{
$user=new User;
$user->username=$request->get('nama_pengguna');
$user->password=bcrypt($request->get('password_pengguna'));
$user->name=$request->get('nama_lengkap');
$user->email="aae#mail.com";
$user->role="user";
$msg="Pengguna baru berhasil ditambahkan";
$user->save();
$request->session()->flash('flash_message',$msg);
return redirect('/');
}
users table
User
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $table='users';
protected $fillable = [
'name', 'username', 'email', 'password', 'role'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
/auth/create_user
#extends('master')
#section('content')
<section id="main-content">
<section class="wrapper">
<div class="row">
<div class="col-lg-12">
<h3 class="page-header"><i class="icon-users"></i> Admin</h3>
<ol class="breadcrumb">
<li><i class="fa fa-home"></i>Beranda</li>
<li>Admin</li>
<li>Tambah Admin</li>
</ol>
</div>
</div>
<!-- Form validations -->
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
Form Registrasi Admin
</header>
<div class="panel-body">
<form class="form-validate form-horizontal" id="register_form" method="POST" action="{{ url('/pengguna/tambah') }}" role="form" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group ">
<label for="cname" class="control-label col-lg-2">Username <span class="required">*</span></label>
<div class="col-lg-4">
<input class="form-control" id="username" name="nama_pengguna" value="{{old('nama_pengguna')}}" minlength="5" maxlength="12" type="text" required />
</div>
</div>
<div class="form-group ">
<label for="password" class="control-label col-lg-2">Password <span class="required">*</span></label>
<div class="col-lg-4">
<input class="form-control " id="password" name="password_pengguna" type="password" value="{{old('password_pengguna')}}" />
</div>
</div>
<div class="form-group ">
<label for="confirm_password" class="control-label col-lg-2">Konfirmasi Password <span class="required">*</span></label>
<div class="col-lg-4">
<input class="form-control " id="confirm_password" name="confirm_password" type="password" value="{{old('confirm_password')}}" />
</div>
</div>
<div class="form-group ">
<label for="fullname" class="control-label col-lg-2">Nama Lengkap <span class="required">*</span></label>
<div class="col-lg-4">
<input class=" form-control" id="fullname" name="nama_lengkap" type="text" value="{{old('nama_lengkap')}}" />
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input name="tambah" type="submit" class="btn btn-primary" value="Daftarkan">
<button class="btn btn-primaryCancel" type="button">Batal</button>
</div>
</div>
</form>
</div>
</section>
</div>
</div>
</section>
</section>
#endsection
Use Role Management For Multiple User
https://cartalyst.com/manual/sentinel/2.0

Resources