Change the value in ActiveForm $form->field(

Hello all,
I am relatively new with yii2 and would like to learn with your help.
My problem is, I have a ActiveForm and a field where I want to put in a number with i.e. “14,58” and it should be converted into “14.58”.
The value I want to change is the value of ‘betrag’.

Source:

<?php $form = ActiveForm::begin(['action' => [$_action],]);?>

    <?= $form->field($model, 'betrag', ['template' => '{label}{input}{error}{hint}','options' => ['class' => 'form-group form-inline'] ])->textInput(['maxlength' => true]) ?>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? 'Speichern' : 'Speichern', ['class' => $model->isNewRecord ? 'btn form-inline' : 'btn form-inline']) ?>
    </div>
	<?= $form->field($model, 'wer', ['template' => '{label}{input}{error}{hint}','options' => ['class' => 'form-group form-inline']])->hiddenInput(['maxlength' => true,])->label('');?>

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

I found some examples and tried to use it, but inexperienced as I am I had problems to integrate it:
$field($model, ‘betrag’);
$field = str_replace(’,’, ‘.’, $field);

Thanks in advance for your help,
keem
PS: If you can recommend any good tutorials to learn about Yii2 I would appreciate it. :smiley:

The best tutorial out there is The Definitive Guide to Yii 2.0 :smiley:

Converting the value of a field from "14,58" to "14.58" could be implemented using the filter validator in the model. Check Guide > Special Topic > Core Validator > filter.

And I also recommend you read the following section of the Guide to understand what those validators are expected to do. Guide > Getting Data from Users > Validating Input

1 Like

Thank you, I will do that.

Hello again,
I tried to implement it in the model. Befor it was like this:

public function rules()
{
    return [

        [['datum'], 'safe'],
        [['betrag', 'wer'], 'required'],
        [['bestand', 'betrag', 'endbetrag'], 'number'],
        [['ist_rechnung', 'mwst'], 'integer'],
        [['wer', 'buchungstext', 'rechnungNr'], 'string', 'max' => 255],
    ];
}

I changed that into this:

public function rules()
{
    return [

        [['datum'], 'safe'],
        [['betrag', 'wer'], 'required'],
    [['betrag', 'filter'], 'filter'=> function($value){
	        $value = floatval($value);
		return $value;
        }],	
        [['bestand', 'endbetrag'], 'number'],
        [['ist_rechnung', 'mwst'], 'integer'],
        [['wer', 'buchungstext', 'rechnungNr'], 'string', 'max' => 255],
    ];
}

I am getting following error:

Invalid Configuration – yii\base\InvalidConfigException

Invalid validation rule: a rule must specify both attribute names and validator type.

Thank you very much softark.
This way it works now. :smile: ```

public function rules()
{
    return [
            [['datum'], 'safe'],
            [['betrag', 'wer'], 'required'],
			[['betrag'], 'filter', 'filter'=> function($value){
				$value = number_format(str_replace(',', '.', $value), 2,'.', ',');
				return floatval($value);
			}],	
        [['bestand', 'endbetrag'], 'number'],
        [['ist_rechnung', 'mwst'], 'integer'],
        [['wer', 'buchungstext', 'rechnungNr'], 'string', 'max' => 255],
    ];
}
1 Like

had to make a (hopefully) last change for it to work properly. The first attempt had problems with floatval recognizing “,” as a string and rounding off directly after it so that the following numbers where not regarded at all.
(e.g. 4,563 would become 4,00)
This way the value is first converted in a number without “,” and afterwards returned via floatval.

1 Like

Found another problem. When using:

$value = number_format(str_replace(',', '.', $value), 2,'.', ',');
				return floatval($value);

the number_format will turn the format of the $value in nnn.nnn.nnn,nn and this will be recognized as nnn because “.” will be recognized as separator e.g. 2,300 will be recognized as “2.3”. Working with numbers >= 1000 will not work.
This way, that problem will be solved:

$value = str_replace(',', '.', $value);
return floatval($value);

Gotta love this… ! :smiley:

1 Like