Hiding ActiveForm Fields when isNewRecord

I’ve currently implemented the following hack to remove fields from the form if you are creating a new record:





<php

$render = null;

if($model->isNewRecord){

    $render = function($val){};

}

?>

    <?= $form->field($model, 'description')->textInput(['maxlength' => 255])->render($render) ?>




Does anyone know of an actual built-in way to do this within the ActiveForm/Field codebase?

create class CustomActiveField extends yii\widgets\ActiveField and override method render

public function render($content = null)

{

if($this->model->isNewModel){

return ‘’;}

parent::render($content);

}

Use it in ActiveForm:

$form = ActiveForm::begin([‘fieldClass’ => 'CustomActiveField ']);