0

I am trying to create a form element text with value in moodle. I trying the below :

$mform->addElement('text','test', get_string('test'));

This is used to create a text box . i want to add value also like

<input type='text' value='<?php .... ?>' />

How to do that in moodle

Nisanth
  • 323
  • 8
  • 24

2 Answers2

0

When you instantiate the form, you can pass the relevant data into it, e.g.

$form = new my_form();
$formdata = (object)array('test' => 'The value to display in the textbox');
$form->set_data($formdata);

(Usually the data passed into the form is some existing data retrieved from the database).

davosmith
  • 6,037
  • 2
  • 14
  • 23
0

I'm not sure what kind of data did you mean here.

If you want to set user data (for example, you are developing a form that edits existing record), then use $form->set_data() after creating a form instance as Davo suggested.

If you want to pre-fill the form with default value, then use this inside the form definition:

$mform->addElement('text','test', get_string('test'));
$mform->setDefault('test', 'your default value');

You can use both methods, in which case the data from set_data() will have priority.

Marina
  • 491
  • 3
  • 9