Multiple textboxes validation in codeigniter 3 - codeigniter

I have 4 textboxes which take url as input.
If user skips all, an error will be thrown to fill at least one input.
Also validate the user input, like if it is a valid url.
How to achieve this Codeigniter 3.x?

You have to use input type = "url", and put validations (add validate.js file for this) like below:
you can get validation.js from here: Validation.js
<!DOCTYPE html>
<html lang="en">
<head>
<title>Add User</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script>
</head>
<body class="backgroundfff">
<section>
<div class="padding0 main_padding" id="main_container">
<div class="container-fluid padding0 bg-clr">
<div class="container backgroundfff user-porfile-padding">
<div class="col-lg-12 col-md-12 col-sm-9 col-xs-12 margintop20px marginbottom45px">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 padding0"></div>
<div class="cp-box col-lg-6 col-md-6 col-sm-6 col-xs-12">
<?php
$add_form =
array(
'name' => 'advertiser-form',
"id" => "validateForm",
'method' => 'post',
'class' => "form-horizontal",
'role' =>'form'
);
echo form_open_multipart(DOMAIN_URL."users/adduser",$add_form);
?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cyp-sub-title margintop20px">
<input type="url" placeholder="Textbox 1" name="Textbox1" id="Textbox1" class="">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cyp-sub-title margintop20px">
<input type="url" placeholder="Textbox 2" name="Textbox2" id="Textbox2" class="">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cyp-sub-title margintop20px">
<input type="url" placeholder="Textbox 3" name="Textbox3" id="Textbox3" class="">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 cyp-sub-title margintop20px">
<input type="url" placeholder="Textbox 4" name="Textbox4" id="Textbox4" class="">
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 padding0 margintop20px marginbottom20px text-center">
<button type="submit" class="cp-submit">Submit</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
</div>
</section>
</body>
<script src="<?=DOMAIN_URL?>/js/validate.js" type="text/javascript" charset="utf-8"></script>
</html>
Server side validation:
<?php
function adduser($postData)
{
if(empty($postData['Textbox1']) || empty($postData['Textbox2']) || empty($postData['Textbox3']) || empty($postData['Textbox4'])) {
// redirect user to add form
}
}
?>
You can use FILTER_SANITIZE_URL to check the url is valid or not, like below:
filter_var($posted_url, FILTER_SANITIZE_URL);

Related

Cant get Exception object on default error.html Thymeleaf page

I have a Spring Boot application and want to display the exception on the error page.
I put the error.html page in the template directory and I can see the change, but the exception variable keeps coming back as null. I can see all the other data like message and stacktrace.
I took this from several examples. What is missing?
Thymeleaf
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Date</span>
</div>
<div class="col-md-8">
<span th:text="${timestamp}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Error</span>
</div>
<div class="col-md-8">
<span th:text="${error}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Status</span>
</div>
<div class="col-md-8">
<span th:text="${status}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Message</span>
</div>
<div class="col-md-8">
<span th:text="${message}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Exception</span>
</div>
<div class="col-md-8">
<span th:text="${exception}"></span>
</div>
</div>
<div class="row rowDesign">
<div class="col-md-4 ml-auto">
<span class="legend">Trace</span>
</div>
<div class="col-md-8">
<span th:text="${trace}"></span>
</div>
</div>
Add this to your application.properties:
server.error.include-exception=true
server.error.include-stacktrace=always
These properties are not enabled by default (See https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html)

$loop->even or $loop->odd doesn't working

So, i have a timeline section that has a different html structure in first row and the other row that has a left side and right side view. the odd side would be on the left and the right side would be on the right. however when i using $loop->first, there is no error at all. but when i use $loop->even or $loop->odd it shows an error like:
Undefined property: stdClass::$even
or
Undefined property: stdClass::$odd
this is the blade view :
#if($loop->first)
<div class="row align-items-center how-it-works d-flex">
<div class="col-2 text-center bottom d-inline-flex justify-content-center align-items-center">
<div class="circle font-weight-bold">{!! $tl->id !!}</div>
</div>
<div class="col-6">
<h5>{!! $tl->company !!}</h5>
<p>{!! $tl->description !!}</p>
</div>
</div>
<!--path between 1-2-->
<div class="row timeline">
<div class="col-2">
<div class="corner top-right"></div>
</div>
<div class="col-8">
<hr/>
</div>
<div class="col-2">
<div class="corner left-bottom"></div>
</div>
</div>
#elseif($loop->even)
<!--second section-->
<div class="row align-items-center justify-content-end how-it-works d-flex">
<div class="col-6 text-right">
<h5>{!! $tl->company !!}</h5>
<p>{!! $tl->description !!}</p>
</div>
<div class="col-2 text-center full d-inline-flex justify-content-center align-items-center">
<div class="circle font-weight-bold">{!! $tl->id !!}</div>
</div>
</div>
<!--path between 2-3-->
<div class="row timeline">
<div class="col-2">
<div class="corner right-bottom"></div>
</div>
<div class="col-8">
<hr/>
</div>
<div class="col-2">
<div class="corner top-left"></div>
</div>
</div>
#elseif($loop->iteration % 2 != 0)
<div class="row align-items-center how-it-works d-flex">
<div class="col-2 text-center bottoms d-inline-flex justify-content-center align-items-center">
<div class="circle font-weight-bold">{!! $tl->id !!}</div>
</div>
<div class="col-6">
<h5>{!! $tl->company !!}</h5>
<p>{!! $tl->description !!}</p>
</div>
</div>
<!--path between 1-2-->
<div class="row timeline">
<div class="col-2">
<div class="corner top-right"></div>
</div>
<div class="col-8">
<hr/>
</div>
<div class="col-2">
<div class="corner left-bottom"></div>
</div>
</div>
#elseif($loop->last)
<div class="row align-items-center how-it-works d-flex">
<div class="col-2 text-center top d-inline-flex justify-content-center align-items-center">
<div class="circle font-weight-bold">{!! $tl->id !!}</div>
</div>
<div class="col-6">
<h5>{!! $tl->company !!}</h5>
<p>{!! $tl->description !!}</p>
</div>
</div>
#endif
#endforeach
This is the controller :
public function index()
{
$desc = Home::first();
$users = User::first();
$site = Site::first();
$timeline = Timeline::get();
$services = Services::get();
$port = Portfolio::get();
$cat = DB::Table("cat_port")->get();
$news = DB::table('news')->join('category', 'category.name', '=', 'news.category')->selectRaw('news.*, category.*, category.url as curl, news.created_at as created')->orderBy('created', 'DESC')->get()->take(3);
return view('home', ['desc' => $desc, 'timeline' => $timeline, 'users' => $users, 'services' => $services, 'port' => $port, 'cat' => $cat, 'news' => $news, 'site' => $site, 'isHome' => true]);
}
i can use the $loop->iteration, but it seems that if i use that, there would be a problem for the last row because when i use it, the script inside $loop->last won't appear. Can someone explain me how to solve this issue ?
odd and even properties are only available in laravel 5.8:
https://laravel.com/docs/5.7/blade#the-loop-variable
https://laravel.com/docs/5.8/blade#the-loop-variable
Maybe an upgrade is gonna help.

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
}
}

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');

i want to add bootsnipp slider in codeigniter

Here is my main view page :
the first class: item active ,
and another is only class: item
but i want foreach only class="col-sm-6" with two class item active and item ...
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-sm-6">
<div class="col-item">
<div class="photo">
<img src="img/a.jpg" />
</div>
<div class="info">
<div class="row">
<div class="price col-md-6">
<h5>title</h5>
<h5 class="price-text-color">
Contact for price</h5>
</div>
<div class="rating hidden-sm col-md-6">
<i class="fa fa-bed"></i> <b>3</b>
<i class="fa fa-bath"></i> <b>2</b>
</div>
</div>
<div class="separator clear-left">
<p class="btn-details">
<i class="fa fa-list"></i>More details</p>
</div>
<div class="clearfix">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="item">
<div class="row">
<div class="col-sm-6">
<div class="col-item">
<div class="photo">
<img src="2.jpg" />
</div>
<div class="info">
<div class="row">
<div class="price col-md-6">
<h5>title</h5>
<h5 class="price-text-color">
Contact for price</h5>
</div>
<div class="rating hidden-sm col-md-6">
<i class="fa fa-bed"></i> <b>5</b>
<i class="fa fa-bath"></i> <b>3</b>
</div>
</div>
<div class="separator clear-left">
<p class="btn-details">
<i class="fa fa-list"></i>More details</p>
</div>
<div class="clearfix">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
but when i foreach the div one is active class but i want one div is active but another is not . actually i want to foreach col-sm-6 class.. how to do this. first
I assume that you want the following:
for there to be one active item
for each item to contain several col-sm-6 elements, created via a php foreach loop
create item via a php foreach loop
<!-- language: lang-php -->
<?php $active_item = "active" ?>
<?php foreach($itemlist as $item): ?>
<div class="item <?php echo $active_item ?>">
<?php $active_item = "" ?>
<div class="row">
<?php foreach($item as $column): ?>
<div class="col-sm-6">
<!-- insert your logic here for creating your columns in a carousel -->
</div>
<?php endforeach ?>
</div>
</div>
<?php endforeach ?>
$active will append the active class to only the first instance of your carousel items.
I avoided using foreach($array as $key=>$value) and checking the key for whether the item is to be active, as your array may not have numerical keys that begin with 0.

Resources