Sami
Sami

Reputation: 129

ActiveForm Name in yii2

i'm using Yii2 and ActiveForm.
in normal if any input in ActiveForm has used, input's name should be same as ActiveRecord attribute which is name of a column of related table in Database.

for some reason i want to use Active TextArea and it's name is also different from Active Record attributes.Actually i want to get TextArea's value and explode then save to database.

is there a way to use a ActiveInput with different name from ActiveRecord attributes?

View:

<?php $form = ActiveForm::begin([
             'id'=>'import-student',
             'options'=>[
                 'class'=>'form-horizontal',
                 'enctype'=>'multipart/form-data']
             ]); ?>    

    <?= $form->field($model, 'first_name', ['inputOptions'=>[
                             'placeholder'=>'import Users as text']])
             ->textArea(['rows'=>'12','class'=>'form-control'])
             ->label(false); ?>  
    <?= Html::submitButton('Submint', ['class'=>'btn btn-primary']) ?> 

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

Controller:

public function actionImport()
{
    $model = new Student();

    if ($model->load(Yii::$app->request->post()) && $users = $model->saveTextArea()) {
        Yii::$app->session->setFlash('info', print_r($users));
    } 

    return $this->render('import',[
        'model'=>$model,
    ]);
}

Model:

public function attributeLabels()
{
    return [
        'id' => Yii::t('backend', 'ID'),
        'first_name' => Yii::t('backend', 'First Name'),
        'last_name' => Yii::t('backend', 'Last Name'),
        'user_id' => Yii::t('backend', 'User ID'),
        'student_number' => Yii::t('backend', 'Student Number'),
        'national_id' => Yii::t('backend', 'National ID'),
        'average' => Yii::t('backend', 'Average'),
        'vip' => Yii::t('backend', 'Vip'),
        'location_id' => Yii::t('backend', 'Location ID'),
    ];
}
public function SaveTextArea()
{
    $users = explode(";", $this->first_name);
    foreach ($users as $user){
        list($first_name,$last_name,$student_number,$national_id) = explode(";", $user);
    }
    return $ali;
}

Upvotes: 3

Views: 3641

Answers (2)

Florin Mateescu
Florin Mateescu

Reputation: 197

A simpler way is to use

\yii\helpers\Html::textarea('the_name_you_need', 'value', options)

This way you will not be obliged to use an ActiveRecord attribute

You can read about it here.

Upvotes: 0

marche
marche

Reputation: 1756

Yes you can, just add a public attribute inside your model class:

class MyClass extends \yii\db\ActiveRecord
{
    public $myAttribute;
    ...

You also need to declare it as a safe attribute and it's type inside your rules method:

public function rules()
{
    ...
    [['myAttribute'], 'safe'],
    ...

and after that you can do any processing using another rule:

public function rules()
{
    ...
    [['myAttribute'], 'safe'],
    [['myAttribute'], 'myCustomFunction'],
    ...
}

public function myCustomFunction($attribute, $params)
{
    // Do explode and assign attribute values here
}

Upvotes: 2

Related Questions