Date Validation and date database insertion in Laravel 5 - date

I have a promo_starts_at and promo_ends_at fields in DB with type date
The date is entered via bootstrap date-picker in the following format 01/20/2019
"promo_starts_at" => "01/22/2019"
"promo_ends_at" => "01/30/2019"
I have the following set of rules in my ProductManager:
if($parameters['type'] == 'integer'){ $data[$field] = intval($request->input($field)); }
if($parameters['type'] == 'string'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'decimal'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'text'){ $data[$field] = $request->input($field); }
if($parameters['type'] == 'bool'){ $data[$field] = $request->input($field) ? 1 : 0; }
if($parameters['type'] == 'date'){ $data[$field] = date('Y-m-d', strtotime($request->input($field))); }
Additionally, I also have a set of validation rules:
'promo_starts_at' => [
'type' => 'date',
'validation' => 'date'
],
'promo_ends_at' => [
'type' => 'date',
'validation' => 'date'
]
I cannot post the date into the database for some reason.
I have also tried different validation rules, such as date_format:Y-m-d|after:today, but nothing changes.
I have found a fix for this problem, the last rule should be changed to the following:
$data[$field] = date('Y-m-d', strtotime($request->input($field)));
This has fixed the problem for me.

I have found a fix for this problem, the last rule should be changed to the following:
$data[$field] = date('Y-m-d', strtotime($request->input($field)));
This has fixed the problem for me.

Related

date_picker field doesnt show value from database in backpacker 4.1 on laravel 8

When i define a datepicker field in backpack 4.1 on laravel 8 the edit form doesnt show existing value in database and when selecting a new value it doesnt store it.
If i change field to date it all works.
Any ideas ?
New laravel 8 and backpacker 4.1 installation
In model
protected $casts = [
'valid_from' => 'date',
'valid_to' => 'date',
];
No Mutators set.
In Controller in setupcreateoperations
$this->crud->addField([
'name' => 'valid_from',
'type' => 'date_picker',
'label' => 'Valid From',
'date_picker_options' => [
'todayBtn' => 'linked',
'format' => 'dd-mm-yyyy',
'language' => 'en'
],
'wrapper' => [
'class' => 'col-md-6'
],
]);
As you found, adding the 'form-group' class back to the field wrapper configuration made the date_picker load normally.
This is happening because in vendor/backpack/crud/src/resources/views/crud/fields/date_picker.blade.php
it does
$field = $fake.closest('.form-group').find('input[type="hidden"]'),
This means that as it is now, this field type requires that the form-wrapper class be on the wrapper in order for it to find the initial value. Otherwise, the following check for if( $existingVal.length ) produces the error below and fails there.
Uncaught TypeError: Cannot read property 'length' of undefined
Submitted a Backpack bug showing the issue and a Pull request with a fix.
This fix may or may not be merged by Backpack. In the mean time, if you really don't want to use form-group on the field wrapper, you can do this:
copy vendor/backpack/crud/src/resources/views/crud/fields/date_picker.blade.php to resources/views/vendor/backpack/crud/fields/date_picker.blade.php
inside the new local date_picker.blade.php, change the code as follows:
Old Code
$field = $fake.closest('.form-group').find('input[type="hidden"]'),
$customConfig = $.extend({
format: 'dd/mm/yyyy'
}, $fake.data('bs-datepicker'));
$picker = $fake.bootstrapDP($customConfig);
var $existingVal = $field.val();
if( $existingVal.length ){
Updated Code
$field = $fake.closest('.input-group').parent().find('input[type="hidden"]'),
$customConfig = $.extend({
format: 'dd/mm/yyyy'
}, $fake.data('bs-datepicker'));
$picker = $fake.bootstrapDP($customConfig);
var $existingVal = $field.val();
if( $existingVal && $existingVal.length ){

understanding Zend\Form\Element\Date

I have two issues while using the Zend-Form date element.
First: field binding
The edit action within my controller doesn't fillin an existing date. For example birthday. The field is just empty. (with an element type text, there is no problem).
Here how I instanciated the field:
$this->add([
'name' => 'geburtstag',
'type' => 'date',
'options' => [
'label' => 'Geburtstag:',
'format' => 'dd/mm/yyyy',
],
]);
And here my controller action.
public function addAction()
{
$form = new AnsprechpartnerForm(NULL, $this->db);
$form->get('submit')->setValue('save');
$request = $this->getRequest();
if (! $request->isPost()) {
return ['form' => $form];
}
$ansprechpartner = new Ansprechpartner();
$form->setInputFilter($ansprechpartner->getInputFilter());
$form->setData($request->getPost());
if (! $form->isValid()) {
return ['form' => $form];
}
$ansprechpartner->exchangeArray($form->getData());
$this->ansprechpartnerTable->saveAnsprechpartner($ansprechpartner);
return $this->redirect()->toRoute('ansprechpartner');
}
No inputFilter at the moment, I tried with and without.
Second: validation
I have trouble filling in dates. While I don't use any filters for this field, I would expect, I could fill any date in.
Interesting I get the message double.
I solved it.
The date element expects the format y-m-d. Now I gave it directly to the field after binding the form. The format in the field is now also korrekt.
$form->bind($notizen);
$form->get('submit')->setAttribute('value', 'edit');
$filter = new \Zend\Filter\DateTimeFormatter();
$filter->setFormat('Y-m-d');
$dat = $filter->filter($notizen->datum);
$form->get('datum')->setValue($dat);
Could be more convenient I guess.

Codeigniter - Form validation not works

I already try to googling but not solve, my code below is to check duplicate before insert, when "tagnumber" field already exists will go to specific page and not inserted to table, it works but the problem is although not insert to the table but it wont go to specific page I want.
below is my problem on conditional statements:
if data exists on form validation will execute not insert the data.
if data not exists form validation will execute insert data but go to wrong page (same page with exists data page).
my controoler :
function tambahSubmit()
{
$tagnumber = $this->input->post("tagnumber");
$this->myigniter_model->addData($tagnumber);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('tagnumber', 'tagnumber', 'trim|required|is_unique[inventorytag.tagnumber]');
$this->form_validation->set_rules('date', 'date', 'trim|required');
$this->form_validation->set_rules('employee', 'employee', 'trim|required');
$this->form_validation->set_rules('semnumber', 'semnumber', 'trim|required');
$this->form_validation->set_rules('quantity', 'quantity', 'required');
$this->form_validation->set_rules('area', 'area', 'trim|required');
if($this->form_validation->run() == false)
{
$this->load->view('YearEndStock/tampilan_input_gagal');
}
else
{
$this->myigniter_model->addData($tagnumber);
$this->load->view('YearEndStock/tampilan_input_sukses');
}
}
my model :
function addData($tagnumber)
{
// Added $this->db->escape() and limit 1 for Performance
$query = $this->db->query("SELECT tagnumber FROM inventorytag WHERE tagnumber = ".$this->db->escape($tagnumber)." limit 1");
$data = array(
'tagnumber' => $this->input->post('tagnumber'),
'date'=> date('Y-m-d H:i:s'),
'employee' => $this->input->post('employee'),
'semnumber' => $this->input->post('semnumber'),
'quantity' => $this->input->post('quantity'),
'area' => $this->input->post('area')
);
return $query->num_rows() == 0 ? $this->db->insert('inventorytag', $data) : false;
}
Then you just need to separate your true statement. Remove your is_unique validation.
if($this->form_validation->run() == false)
{
SHOW ERRORS
$this->load->view('YearEndStock/tampilan_input_gagal');
}
else
{
$row = $this->db->get_where('inventorytag', array('tagnumber' => $tagnumber))->row();
if (empty($row)) {
//insert
$this->myigniter_model->addData($tagnumber);
} else {
//row exists
}
$this->load->view('YearEndStock/tampilan_input_sukses');
}

yii2 detailview conditional row class

I would like to change class for one single attribute in detailview, based on a condition:
If I wouldn't want to make it conditional, it would be working like so:
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => [
'class' => 'danger',
]
],
I want this one to change to conditional, and I have tried a lot of different ways, e.g.:
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => function ($model) {
if ($model->ungueltig == 1) {
return ['class' => 'danger'];
} else {
return '';
}
},
],
(I would think this is the most logical solution, but nothing happens, so page is loading fine but without class danger at the attribute, no error message)
or
[
'attribute' => 'ungueltig',
'format' => 'boolean',
'contentOptions' => ['class' => function ($model) {
if ($model->ungueltig == 1) {
return 'danger';
} else {
return '';
}
},]
],
= error message: htmlspecialchars() expects parameter 1 to be string, object given
so I have no clue and I don't even find any help on the web. Can you please point me to the right direction? Many thanks!
You should simply try :
'contentOptions' => [
'class' => ($model->ungueltig == 1) ? 'danger' : '',
],
DetailView display only one model, you don't need any function here.

How to disable unavailable dates in the CJuiDatePicker widget

I am building a scheduling app. I am able to sort the database using the CJuiDatePicker widget's "onSelect" option. Now I am trying to use the "beforeShow" option to ensure that only dates that have tasks can be selectable. Can anyone help?
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$dataProvider,
'attribute'=>'start_time',
'options'=>array(
'dateFormat' => 'yy-mm-dd',
'changeMonth' => true,
'changeYear' => true,
'beforeShowDay' => 'js:$.datepicker.noWeekends',
'beforeShow' => "js:function Check_Alert2(){
Check_Alert();
}
",
'onSelect' => "js:function SearchFunc() {
var data = $('input').serialize();
var url = document.URL;
var params = $.param(data);
url = url.substr(0, url.indexOf('?'));
window.History.pushState(null, document.title,$.param.querystring(url, data));
}"
),
'htmlOptions'=>array(
'style'=>'height:20px;',
'readonly' =>true,
),
));
i am still using the original code. I need an array generated within the beforeshowday. When i run your code it fails on the getdates() function call.
'beforeShowDay' => 'js:function(date){
var array = ["2013-10-30","2013-10-31","2013-12-18","2013-12-25"];
var string = jQuery.datepicker.formatDate("yy-mm-dd", date);
if (array.indexOf(string) == -1){
return [false,"", "No event"];
} else return [true,"", "Event"];