2amigos / yii2-date-picker-widget

Bootstrap DatePicker Widget for Yii2
https://2amigos.us
Other
130 stars 77 forks source link

dont take field value #5

Closed pippuccio76 closed 9 years ago

pippuccio76 commented 9 years ago

this is my model :

<?php

namespace common\models;

use Yii;
use yii\validators;

/**
 * This is the model class for table "Eventi".
 *
 * @property integer $idEventi
 * @property string $nomeEvento
 * @property timestamp $datains
 * @property date $dataInizio
 * @property date $dataFine
 * @property date $dataPrevista
 * @property string $numPartecipanti
 * @property string $insegnante
 * @property string $svolgimento
 */
class Eventi extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    /*
    public $dataInizio;
    public $dataPrevista;
    public $dataFine;
    */
    public static function tableName()
    {
        return 'Eventi';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'nomeEvento', 'svolgimento'], 'required'],
            [['svolgimento'], 'string'],
            [['nomeEvento', 'numPartecipanti', 'insegnante'], 'string', 'max' => 45],
            //[['dataInizio', 'dataFine', 'dataPrevista'],'controlloDate','skipOnEmpty' => false, 'skipOnError' => false]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'idEventi' => 'Id Eventi',
            'nomeEvento' => 'Nome Evento',
            'dataInizio' => 'Data Inizio',
            'dataFine' => 'Data Fine',
            'dataPrevista' => 'Data Prevista',
            'numPartecipanti' => 'Num Partecipanti',
            'insegnante' => 'Insegnante',
            'svolgimento' => 'Svolgimento',
        ];
    }

    public function controlloDate($attribute,$params){

        if (isset($this->dataPrevista)and !isset($this->dataInizio) and !isset($this->dataFine)){
            return true;

        }

        else {
            $this->addError($attribute, 'Se riempi il campo data prevista non puoi riempire i campi data fine e data inizio!');
            return false;
        }
    }
}

and this is my view

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use dosamigos\datepicker\DatePicker;

/* @var $this yii\web\View */
/* @var $model common\models\Eventi */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="eventi-form">

    <?php $form = ActiveForm::begin(); ?>

    <?//S= $form->field($model, 'idEventi')->textInput() ?>

    <?= $form->field($model, 'nomeEvento')->textInput(['maxlength' => 45]) ?>
    <div class="col-lg-4">
        <?= $form->field($model, 'dataInizio')->widget(
            DatePicker::className(), [
            // inline too, not bad
            'inline' => false, 
            //lingua
            'language' => 'it',
            // modify template for custom rendering
            //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
            'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-MM-dd'
            ]
        ]);?>
    </div>
    <div class="col-lg-4">
        <?= $form->field($model, 'dataFine')->widget(
            DatePicker::className(), [
            // inline too, not bad
            'inline' => false, 
             //lingua
            'language' => 'it',
            // modify template for custom rendering
            //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
            'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-MM-dd'
            ]
        ]);?>
    </div>

    <div class="col-lg-4">
        <?= $form->field($model, 'dataPrevista')->widget(
            DatePicker::className(), [
            // inline too, not bad
            'inline' => false, 
             //lingua
            'language' => 'it',
            // modify template for custom rendering
            //'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
            'clientOptions' => [
                'autoclose' => true,
                'format' => 'yyyy-MM-dd'
            ]
        ]);?>
    </div>

    <?php 
        $t=time();
        $data=date("Y-m-d-G-i-s",$t);
    ?>
    <?= Html::activeHiddenInput($model,'data_ins',array('value'=>$data)) ?>

    <?= $form->field($model, 'numPartecipanti')->textInput(['maxlength' => 45]) ?>

    <?= $form->field($model, 'insegnante')->textInput(['maxlength' => 45]) ?>

    <?= $form->field($model, 'svolgimento')->textarea(['rows' => 6]) ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>

but when i submit in the view i havent not value in thedate 3 fields

tonydspaniard commented 9 years ago

Please set the rules for your dataPrevista and dataFine, otherwise models won't collect the data submitted.