If I have an object (e.g. student object - this is my own custom class)
I would like to initiate this object during 'student registration' form initialization (or form build) and keep the object until I finish with the form (form submitted).
Is there any placeholder for me to hold this object in the FormBase class? Where I should declare my class? And how to keep the object?
Example in my code:
<?PHP
namespace bla;
use bla
use bla
use bla
class myCreateForm extends FormBase {
private $student; // i want to initiate this (new student()) and keep until form submit
/* where i should declare this? and how to keep it? */
/**
* Class constructor.
*/
public function __construct($param) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'id_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
//a lot of code here code here
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
//a lot of code here code here
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$output = $this->student->save(); // at the end, i would like to have my own save process which my class will handle it.
drupal_set_message($output);
}
}