0

I have created and registered a Yii2 component function 'formSchema' which contains the array as such:

class FormSchema extends Component{

  public function formSchema()
  {

    $fields = array(
      ['field' => 'username', 'controltype' => 'textinput'],

      ['field' => 'email', 'controltype' => 'textArea'],
    ); 

     return $fields;
    }
   }
?>

I call the array in the active form, however I cannot get the ['controltype'] using the same successful method as I do to retrieve ['field'] as below. I would like to get that array element however seem unable to get any but the first level element:

<div class="usermanager-form">

<?php $form = ActiveForm::begin(['id'=>$model->formName()]); ?>

<?php

   $fields = $items = Yii::$app->formschema->formSchema();

   foreach($fields as $field)
   {

      $field = $field['field'];

      echo $form->field($model, $field);
    }

?>

1 Answer 1

1

You may use array values in this way:

$fields = Yii::$app->formschema->formSchema();
foreach ($fields as $field) {
    echo $form->field($model, $field['field'])->{$field['controltype']}();
}
Sign up to request clarification or add additional context in comments.

3 Comments

>>> Using your example, with thanks. here is an extended example using cited default Yii2 ActiveField widget method details: <?php $fields = Yii::$app->formschema->formSchema(); ?> <div class="usermanager-form"> <?php $form = ActiveForm::begin(['id'=>$model->formName()]); ?> <?php foreach($fields as $field) { echo $form->field($model, $field['name'])->label($field['label'])- >hint($field['hint'])->input($field['input']); } ?>
I am a little stuck working out how to format answer posts correctly, this is my first post, any guidance appreciated. There does not seem to be a HTML editor for comments to answers...
You cannot format code in comments - you may add this as separate answer. Also see stackoverflow.com/help/accepted-answer and stackoverflow.com/help/self-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.