Using a datepicker with Phalcon Volt template engine - php

I am pretty new to the Phalcon PHP framework and the Volt template engine.
So far I really like it though.
One thing that I can't figure out however, is how to implement a date picker to a date field.
I have a date field defined as below, but rather than having to manually input a date I would like the user to select a date from a date picker.
<?php echo Tag::dateField(array("finishdate", "size" => 8, "maxlength" => 8, "type" => "date")) ?>
I thought that maybe it would automatically get a date picker if it was defined as a date field, or maybe there was an option somewhere, but I have looked all over internet and I can't find anything.
I would be very grateful if someone knew how to solve this?
I am also trying to figure out the Volt textArea and I have problems finding any information regarding this as well. I can't seem to get the text area to show as a text box. There just seems to be very little information from users regarding Phalcon Volt. Is no one using this?

I did it! I used datepicker (https://jqueryui.com/datepicker/). I share with you the code.
Form:
$fecha = new Text('fecha_final');
$fecha->setLabel('Fecha Final');
$fecha->addValidators(array(
new PresenceOf(array(
'message' => 'Por favor pon una fecha límite'
))
));
$this->add($fecha);
Controller:
public function nuevakeywordAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new NuevakeywordForm;
if ($this->request->isPost()) {
$trackeable = $this->request->getPost('trackeable', array('string', 'striptags'));
$idcliente = $this->request->getPost('idcliente');
$fecha = $this->request->getPost('fecha');
$keyword = new Keyword();
$keyword->trackeable = $trackeable;
$keyword->cliente_idcliente = $idcliente;
$keyword->fecha_inicial = new Phalcon\Db\RawValue('now()');
$keyword->fecha_final = $fecha;
if ($keyword->save() == false) {
foreach ($keyword->getMessages() as $message) {
$this->flash->error((string) $message);
}
} else {
$this->tag->setDefault('trackeable', '');
$this->tag->setDefault('idcliente', '');
$this->tag->setDefault('fecha', '');
$this->flash->success('Keyword agregada correctamente');
return $this->forward('admin/verkeywords');
}
}
$this->view->form = $form;
}
View:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<section class="container animated fadeInUp">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div id="login-wrapper">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Registrar Nueva Keyword
</h3>
</div>
<div class="panel-body">
{{ form('admin/nuevakeyword/', 'id': 'nuevakeywordForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('trackeable', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('trackeable', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="control-group">
{{ form.label('fecha_final', ['class': 'control-label']) }}
<div id="datepicker" class="controls">
{{ form.render('fecha_final', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Insertar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<script type="text/javascript">
$(function() {
$("#fecha_final").datepicker();
});
</script>
I hope it will help you.

The date field merely uses HTML5's date field. If you want a date picker, you must use a Javascript date picker library.
As for text areas and text fields, you should use the function text_field() to create a text field, and text_area() to create a text area. The list of functions you can use are in the Volt – Using tag helpers documentation page.

Related

How to show database info in Laravel Blade with VUE

Sorry for the next question, Im really new on VUE.
I have a button on my blade, when I do it click I want to retrieve the info of the customer depend of the ID that get on my controller, I get the ID #3 and I click on CUSTOMER INFO Button, I want to see the info of this customer from the database
This is my function on my Controller CustomersController.php
public function showcustomers($idcustomer)
{
$Customers = Customers::find($idcustomer);
return view('showcustomer',
['Customers' => $Customers]);
}
And this is my button on Blade showcustomer.blade.php :
<div id="cita">
<div class="container-fluid">
<div class="container">
<div class="row">
<div class="col-xs-6">
<button v-on:click="showinfo = !showinfo" type="button" class="w3-btn w3-blue" style="width:100%">CUSTOMER INFO</button>
</div>
</div>
</div>
</div>
<br/><br/>
<div class="container align-content-between">
<div class="row text-center">
<div class="col-md-12 col-xl-12">
<information v-if="!showinfo"></information>
</div>
</div>
</div>
</div>
My app.js
Vue.component('information', require('./components/information.vue').default);
var app = new Vue({
el: '#cita',
data: {
showinfo: true,
}
});
And finally my information.vue
<template>
<div id="information">
<h4>Information {{ Customers.name }}</h4>
</div>
</template>
<script>
export default {
name: "information"
}
</script>
<style scoped>
</style>
Standard way of doing that is you will be implementing an ajax request where your front-end or Vue will fetch the necessary information from back-end via ajax request.
Another way of achieving that is you will be using properties or attributes in VUE
from your information.vue add a props maybe customerName or orderId or both
<template>
<div id="information">
<h4>Information {{ customerName }}</h4>
<h4>ID {{ orderID }}</h4>
</div>
</template>
<script>
export default {
props:["customerName","orderID"],
name: "information"
}
</script>
<style scoped>
</style>
in you showcustomer.blade.php you can pass the data from php to your vue by adding attributes based on the property name declared on you vue template
<information customer-name="John Mahu" order-id="0099812-ABC" v-if="!showinfo"></information>
or populate from your PHP variable
<information customer-name="{{ $Customers->customer_name }}" order-id="{$Customers->some_order_id}" v-if="!showinfo"></information>
Read more about vue props
https://v2.vuejs.org/v2/guide/components-props.html

Laravel how to echo selected a value in a dropdown using blade when updating a specific resource

I want to echo the selected value when I edit a specific resource in my table. When I edit the specific resource it should display the current data in my dropdown but in the real scenario it displays the first one on the list which is wrong. So how can I echo the selected value in the options of the dropdown using blade in laravel?
Here is some sample of my code in the view below
<!-- Shows Field -->
<div class="form-group col-sm-6">
{!! Form::label('show_id', 'Shows:') !!}
{!! Form::select('show_id', $shows, $shows, ['class' => 'form-control input-md','required'])!!}
</div>
{{-- {{ $channelshows->channel->name }} --}}
<!-- Client Id Field -->
<div class="form-group col-sm-6">
{!! Form::label('channel_id', 'Channels:') !!}
{!! Form::select('channel_id', $channel, $channel, ['class' => 'form-control input-md','required'])!!}
</div>
<!-- Submit Field -->
<div class="form-group col-sm-12">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
and here is the code in my controller below.
public function edit($id)
{
$channelshows = $this->channelshowsRepository->findWithoutFail($id);
$shows = Show::pluck('name', 'id');
$channel = Channel::pluck('name', 'id');
if (empty($channelshows)) {
Flash::error('Assigned show not found');
return redirect(route('admin.channelshows.index'));
}
return view('admin-dashboard.channelshows.edit', compact('shows', $shows), compact('channel', $channel))->with('channelshows', $channelshows);
}
Everything here works fine even if I updated the specific resource. I just want to auto populate or select the current value of the resource that I will update because when I edit the specific resource it shows the first one on the list.
Am I going to use an #if statement in blade? But how can I do it using the blade template in my select form. Can somebody help me?
Appreciate if someone can help.
Thanks in advance.
Here is an example:
Open form:
{{ Form::model($service, array('route' => array('services.update', $service->id))) }}
Select form field:
<div class="form-group">
{{ Form::label('Related Agreement') }}
{{ Form::select('agreement', $agreementsList, null, array('class'=>'form-control', 'placeholder'=>'Please select ...')) }}
</div>
In Controller:
$agreementsList = Agreement::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE)->pluck('name', 'id');
(Include this when passing data to your view)

Display none not working Laravel

I have a condition, if active is checked then it'll display a text form.
This is my view form code:
<div class="form-group">
<label>Active:</label>
{{ Form::checkbox('active', 0,null, array('id' =>'active_check','class' => 'active_check')) }}
</div>
<div id ="join_date" class="form-group" style="display: none;">
<label>Join Date:</label>
<div class="input-group">
{!! Form::text('join_date', null, array('class' => 'form-control datetimepicker' )) !!}
</div>
</div>
Here is my jQuery toggle and check condition code so far:
$('#active_check').click(function() {
$("#join_date").toggle(this.checked);
$('#join_date').find('input[type="text"], textarea').val('');
});
var activeCheck = $('checkbox[name="active_check"]');
function checkActive(select) {
if (select.val() == 0) {
$('#join_date').hide();
} else {
$('#join_date').show();
}
}
checkActive(activeCheck);
The issue is when it is first loaded the join_date form doesn't hide. I need to toggle the checkbox to hide it. Any idea?
Try this simple logic:
if($('#active_check').is(':not(":checked")')) {//test if active_check is not checked
$('#join_date').hide();//hide it
}
$('#active_check').click(function() {
$("#join_date").toggle();//toggle it
});

Laravel (php) save search options

So i am using laravel 5.2 and i am building a search form, there is multiple search queries and options.
I have been wracking my head trying to think of ways to save the users search and auto fill the form when they visit the search page OR even auto search when they visit it.
So far I have been setting the GET's as variables and auto fill in the text fields but that only lasts for the time they are on the page which is not what i am after,
I have thought about saving the gets into the database as separate fields and when they search and pulling them when they are on the page and filling in each field but then how do I auto search?
Or do I save it as 1 string as the get from the URL eg, u=username&p=postcode
and set the route up to check if the user has a search saved and if they visit /search they get redirected to the url+ get options saved in the db. (no clue how to do this either.)
Now the hardest part even with the gets I have fields that are multiple select like genders and sort by. How do you auto fill in those?
Below is my code for just the form
<?php
if(isset($_GET['u'])) { $username = $_GET['u']; } else { $username = ''; }
if(isset($_GET['p'])) { $postcode = $_GET['p']; } else { $postcode = ''; }
if(isset($_GET['o'])) { $orderby = $_GET['o']; } else { $orderby = ''; }
?>
{{ Form::open(['method' => 'GET']) }}
<div id="filter-panel" class="filter-panel collapse in">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-inline" role="form">
<div class="row">
<div class="col-xs-6 col-sm-2">
{{ Form::input('search', 'u', $username, ['class' => 'form-control input-sm', 'placeholder' => 'username...']) }}
</div><!-- form group [search] -->
<div class="col-xs-6 col-sm-2">
{{ Form::input('search', 'p', $postcode, ['class' => 'form-control input-sm', 'placeholder' => 'postcode...']) }}
</div><!-- form group [search] -->
<div class="col-xs-6 col-sm-3">
<select id="attractedToGender" multiple="multiple" name="g[]" class="input-sm" >
<option value="1">Females</option>
<option value="2">Males</option>
<option value="3">Others</option>
</select>
</div><!--END form-group col-xs-6 col-sm-4-->
<div class="col-xs-12 col-sm-3">
<select id="pref-orderby" class="input-sm" name="o[]" multiple="multiple">
<option value="1">Photo Only</option>
<option value="2">User Type</option>
<option value="3">Last Online</option>
</select>
</div> <!-- form group [order by] -->
<div class="col-xs-6 col-sm-2 pull-right text-right">
{{Form::button('<i class="glyphicon glyphicon-search"></i> Search', array('type' => 'submit', 'class' => 'btn btn-primary btn-sm', 'style' => 'margin:0;'))}}
</div><!--END form-group col-xs-6-->
</div>
</form>
</div>
</div>
</div>
{{ Form::close() }}
There are few ways to do that. The best ways are dedicated model and saving data as JSON object.
Model. You can create dedicated table with User has many Searches relationship. And just save object into this table.
JSON. You can serialize object with ->toJson() method and persist in in a users table. Just add json type column:
$table->json('options');
In both cases you're working directly with the object.

How do I provide inline error feedback with Laravel 4 and Twitter Bootstrap 3?

I'm building a form using Laravel 4 and Twitter Bootstrap 3 and I want to error messages to appear next to the field. This is the solution that I came up with but it ends up being 15 lines of code per field.
#section('content')
<div class="container">
<h1>Edit User</h1>
{{ Form::model($user, array('route' => array('users.update', $user->id), 'method' => 'PUT', 'class' => 'form-horizontal')) }}
{{-- First Name field --}}
{{-- Start a form group. If there any errors, then highlight the field red. --}}
<div class="form-group {{ $errors->has('first_name') ? 'has-error' : '' }}">
{{-- Display the label and the field. --}}
{{ Form::label('first_name', 'First Name', array('class' => 'col-sm-2 control-label')) }}
<div class="col-sm-5">
{{ Form::text('first_name', NULL, array('class' => 'form-control', 'placeholder' => 'First Name')) }}
</div>
{{-- If there is an error, display any messages to the right of the field with a warning icon. --}}
#if($errors->has('first_name'))
<div class="col-sm-5">
#foreach ($errors->get('first_name') as $message)
<span class="help-block">
<span class="glyphicon glyphicon-warning-sign"></span>
{{ $message }}
</span>
#endforeach
</div>
#endif
</div>
{{-- Form buttons --}}
<div class="form-group">
{{-- Line up the buttons with the right edge of the fields. --}}
<div class="col-sm-offset-2 col-sm-5">
<div class="pull-right">
{{-- Cancel button takes user back to profile page. --}}
{{ HTML::linkRoute('users.show', 'Cancel', array($user->id), array('class' => 'btn btn-default')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
</div>
</div>
</div>
{{ Form::close() }}
</div>
#stop
This is how it appears:
I'm just starting out with both Laravel and Bootstap. I used Jeffery Way's tutorial on NetTuts to make the form and Coder's Guide's tutorial to apply the formatting.
Should I be using client-side validation or would this be considered an acceptable implementation of Laravel 4 and Bootstrap?
Thanks!
This may not be the coolest way to do this but I think it's pretty slick.
Laravel has a feature called Form::macro that allows you to sort of make reusable code snippets. You can define a macro all kinds of places but I just slapped mine in my routes.php file to get this going real quick.
Form::macro('errorMsg', function($field, $errors){
if($errors->has($field)){
$msg = $errors->first($field);
return "<span class=\"error\">$msg</span>";
}
return '';
});
Then to use in a form, pass the macro your error messages:
{{ Form::label('first_name', 'First Name:') }}
{{ Form::text('first_name') }}
{{ Form::errorMsg('first_name', $errors) }}
{{-- where $errors is your Illuminate\Support\MessageBag object --}}
To get even more tech, you can use Form::objects in a Form::macro like so:
Form::macro('textError', function($field, $label, $errors){
$label_html = Form::label($field, $label);
$text_html = Form::text($field);
$msg_html = '';
if($errors->has($field)){
$msg_html.= '<span class="error">';
$msg_html.= $errors->first($field);
$msg_html.= '</span>';
}
return $label_html.$text_html.$msg_html;
});
Then you're at 1 line per input:
{{ Form::textError('first_name', 'First Name:', $errors) }}
You'd need to make other macros for password, textarea, etc. (which is why I just use the first example; it's only a couple more lines of code per input and serves all input types.)
If you wanted to style your input on error, eg. a red border, you'd probably want the second example then wrap it in your div.form-group or whatevers. Anyway, options out the wazoo.
UPDATE: An even slicker way to get errors in macros is to access them via Session::get('errors'); like so:
Form::macro('errorMsg', function($field){//yay! we don't have to pass $errors anymore
$errors = Session::get('errors');
if($errors && $errors->has($field)){//make sure $errors is not null
$msg = $errors->first($field);
return "<span class=\"error\">$msg</span>";
}
return '';
});

Categories