Simple form to update a profile custom field

Simple form to update a profile custom field

by Clemente D.Jnewah -
Number of replies: 2

I've created a custom Moodle block, with a basic form. I want to show a custom profile user field called 'custom_name'.

I'm using the basic form code from moodle Documentation (https://docs.moodle.org/dev/Form_API) :

//moodleform is defined in formslib.php
require_once("$CFG->libdir/formslib.php");

class simplehtml_form extends moodleform {
    //Add elements to form
    public function definition() {
        global $CFG;
       
        $mform = $this->_form; // Don't forget the underscore! 

        $mform->addElement('text', 'email', get_string('email')); // Add elements to your form.
        $mform->setType('email', PARAM_NOTAGS);                   // Set type of element.
        $mform->setDefault('email', 'Please enter email');        // Default value.
         
    }
    //Custom validation should be added here
    function validation($data, $files) {
        return array();
    }
}

I simply need to show one custom input field ('profile_field_custom_name') and a submit button to save/update that data. If anyone could show me a similar finished example would be great. Thank you fory your help.


Average of ratings: -
In reply to Clemente D.Jnewah

Re: Simple form to update a profile custom field

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Did you know that Moodle already lets you add additional fields to the user's profile, without having to write any code? They can be edited through the user's profile, like any other field. https://docs.moodle.org/311/en/User_profile_fields

In reply to Mark Johnson

Ri: Re: Simple form to update a profile custom field

by Clemente D.Jnewah -
Hi Mark,
thank for you answer.

I know I can add additional profile fields directly from admin panel, but my goal is a bit different: i want to add a custom block on specific courses where user can add their 'custom_name' to use on course certificate.