my problem is: I have a form where i have select2 and the user can select from a dropdown list which is populated from a table, so, basically what i want to happen is that when the user tries to select a driver a validation rule will take place which will check if the driver was already registered through the system that day.
I have one idea of how to do it, i have my archive form which registers people that enter in the archive table, it checks wether it exists or not in the drivers table and if not you have to register them, if they exists you proceed to fill out the form, each field in the this archive table has a datecreated column which is the date the field was entered, so basically the last time he came in.
How can i make a rule which calls for this column compares to current day and gives an error if he had already entered that day? The rule should be in drivers model.(My code is confusing that is why i'm clarifying, i'm new to Yii2)
My view (archive form):
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use wbraganca\dynamicform\DynamicFormWidget;
use app\models\Drivers;
use app\models\Vehicles;
use app\models\Invoices;
use dosamigos\datepicker\DatePicker;
use kartik\select2\Select2;
use yii\bootstrap\Modal;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model app\models\Archive */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="archive-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<?= $form->field($model, 'driver_identitynum')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Drivers::find()->all(),'driver_identitynum', 'fullname'),
'language' => 'en',
'options' => ['placeholder' => 'Ingrese el numero de cedula...'],
'pluginOptions' => [
'allowClear' => true],
]); ?>
<div align="right"><?= Html::a('Add driver', ['/drivers/create'],
['target'=>'_blank']); ?>
</div>
<?= $form->field($model, 'vehicle_lp')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Vehicles::find()->all(),'vehicle_lp', 'fulltruck'),
'language' => 'en',
'options' => ['placeholder' => 'Ingrese la placa del vehiculo...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<div align="right"><?= Html::a('Add vehicle', ['/vehicles/create'],
['target'=>'_blank']); ?>
</div>
<div class="row"> <div class="panel panel-default">
<div class="panel-heading"><h4><i class="glyphicon glyphicon-envelope"></i>Facturas</h4></div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 4, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsInvoices[0],
'formId' => 'dynamic-form',
'formFields' => [
'invoice_number',
'invoice_loadamount',
'invoice_date',
],
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelsInvoices as $i => $modelInvoices): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Facturas</h3>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (! $modelInvoices->isNewRecord) {
echo Html::activeHiddenInput($modelInvoices, "[{$i}]id");
}
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelInvoices, "[{$i}]invoice_number")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelInvoices, "[{$i}]invoice_loadamount")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelInvoices, "[{$i}]invoice_date", ['enableAjaxValidation' => true])->widget(DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'options' => ['class' => 'form-control picker'],
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-mm-yyyy'
]
]);?>
</div>
</div><!-- .row -->
<div class="row">
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
My drivers model:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "drivers".
*
* @property integer $driver_id
* @property string $driver_name
* @property string $driver_lastname
* @property string $driver_identitynum
*/
class Drivers extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'drivers';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['driver_name', 'driver_lastname', 'driver_identitynum'], 'required'],
[['driver_name', 'driver_lastname', 'driver_identitynum'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'driver_id' => 'Driver ID',
'driver_name' => 'Driver Name',
'driver_lastname' => 'Driver Lastname',
'driver_identitynum' => 'Driver Identitynum',
];
}
public function getfullName()
{
return $this->driver_identitynum.' - '.$this->driver_name.' '.$this->driver_lastname.' ';
}
/**
* @return \yii\db\ActiveQuery
*/
public function getArchives()
{
return $this->hasMany(Archive::className(), ['driver_identitynum' => 'driver_identitynum']);
}
}
My archive model:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "archive".
*
* @property integer $id
* @property string $driver_identitynum
* @property string $vehicle_lp
* @property string $DateCreated
*
* @property Invoices[] $invoices
*/
class Archive extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'archive';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['driver_identitynum', 'vehicle_lp'], 'required'],
[['DateCreated'], 'safe'],
[['driver_identitynum', 'vehicle_lp'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'driver_identitynum' => 'Cedula del conductor:',
'vehicle_lp' => 'Placa del vehiculo:',
'DateCreated' => 'Date Created',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getInvoices()
{
return $this->hasMany(Invoices::className(), ['archive_id' => 'id']);
}
public function __toString()
{
return 'CI:'.$this->driver_identitynum.' Placa: '.$this->vehicle_lp;
}
public function didheloadtoday($attribute,$params)
{
$inputlp=($this->invoice_loadamount);
$row = (new \yii\db\Query())
->select('vehicle_lp')
->from('vehicles')
->where("vehicle_lp=$vehiclelp")
->all();
}
}