got a dropdownlist with 3 options namely 0,1,2. why wont this work to validate. [[‘marital_status’, ‘type’] ‘in’, ‘range’=>‘0’,‘1’,‘2’]. can the db field be of type ‘tinynt’
got a dropdownlist with 3 options namely 0,1,2. why wont this work to validate. [[‘marital_status’, ‘type’] ‘in’, ‘range’=>‘0’,‘1’,‘2’]. can the db field be of type ‘tinynt’
you can also do
range=>range(0,2)
so you don’t have to type all of them.
Your going to have to post more than you did to get an answer
Post:
dropdown list (function populating data if applicable)
Model rules for above field
Are you using scenarios? If so post them for the scenario that isn’t validating
the error generated is ‘marital status is invalid’. the database field is tinyint .
kindly share ur dropdown code here
u r passing name in place of id.(may be)
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\jui\DatePicker;
/* @var $this yii\web\View */
/* @var $model app\models\Registration */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="registration-form">
<?php $form = ActiveForm::begin(); ?>
<?php echo $form->field($model, 'gender[]')->dropDownList(['0' => 'N/A', '1' => 'Male', '2' => 'Female']) ?>
<?php echo $form->field($model, 'marital_status[]')->dropDownList(['0' => 'N/A', '1' => 'Married', '2' => 'Single']) ?>
<?php echo $form->field($model, 'type[]')->dropDownList(['1' => 'Farmer', '2' => 'Semen Supplier']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
=========================================================================
Registration.php
<?php
namespace app\models;
use Yii;
/**
This is the model class for table "registration".
@property string $gender
@property string $marital_status
@property string $type
@property string $user_id
*/
class Registration extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'registration';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['gender' , 'marital_status', 'type'], 'in', 'range' => ['0', '1', '2']]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'gender' => 'Gender',
'marital_status' => 'Marital Status',
'type' => 'Type',
];
}
}