Jump to content

required field not actually required when adding a new user (question about field context)


Marc
 Share

Recommended Posts

I am building a simple admin module with a custom user registration form. The form displays fields from the user template. In the user template, I have set the 'email' field to required. This is an override, because the field itself is not required. So the field is only required in the context of the user template. However, when creating a new user form with the API, this override seems to be ignored and the email field is not required. Am I missing something? This is my code:

// Make a form.
$form = $modules->get('InputfieldForm');
$form->method = 'post';
$form->action = './';

// Add the user template's fields to the form.
$fields = $templates->get("user")->fieldgroup;
foreach($fields as $field) {
	// Do not display certain system fields.
	$skip = array('admin_theme', 'language', 'roles', 'pass');
	if (in_array($field->name, $skip)) continue;

    $inputfield = $fields->get($field->name)->getInputfield(new user());
    $form->add($inputfield);
}
Link to comment
Share on other sites


// "new user()" needs to be "new User", because classes do begin with uppercase char

// Also do you really want to create a new user object for each field?

$inputfield = $fields->get($field->name)->getInputfield(new user());

Link to comment
Share on other sites

// "new user()" needs to be "new User", because classes do begin with uppercase char
// Also do you really want to create a new user object for each field?
$inputfield = $fields->get($field->name)->getInputfield(new user());

Is using the $user variable a valid alternative for a new user object in this context?

You don't need to get the field every time as you already get it in the loop. Your getting the inputfield without context.

I think I understand so I modified the code a bit, but this does not solve the problem:

// Make a form.
$form = $modules->get('InputfieldForm');
$form->method = 'post';
$form->action = './';

// Add the user template's fields to the form.
$fields = $templates->get("user")->fieldgroup;
foreach($fields as $field) {
	// Do not display certain system fields.
	$skip = array('admin_theme', 'language', 'roles', 'pass');
	if (in_array($field->name, $skip)) continue;

    $inputfield = $field->getInputfield($user);
    $form->add($inputfield);
}
Link to comment
Share on other sites

I could really use another hint... I'm having the same issue with saving a regular page: when a field is set to 'required' in the template but not in the field properties itself, the custom form won't treat it as a required field. Probably because of the missing inputfield context Soma mentioned, which probably means the form is looking at the fields without the context of the template in which I set the fields as required. 

How should I modify this example for the above?

$p = new Page; // create new page object
$p->template = 'order'; // set template
$p->parent = wire('pages')->get('/Orders/'); // set the parent
$p->of(false); // turn off output formatting before setting values
$p->save(); // create the page

foreach ($p->fields as $f) {
$inputval = $formOrder->get($f->name)->value;

// Attach fields to page.
$p->set($f->name, $inputval);
}

$p->set('title', $p->order_number);

$p->save(); // Create the page.
Link to comment
Share on other sites

Searching around on the forum, I've come up with the following:

// Add template's fields to the form.
$p = new Page;
$template = $templates->get("call");
$fields = $template->fieldgroup;

foreach($fields as $field) {

    $inputfield = $field->getInputfield($p);

    $inputfield->label = $template->fieldgroup->getField($field, true)->label;
    $inputfield->description = $template->fieldgroup->getField($field, true)->description;
    $inputfield->required = $template->fieldgroup->getField($field, true)->required;

    $form->add($inputfield);
}

I'm overriding the fieldgroup properties with template field properties for 'label', 'description' and 'required'. Which works so that solves my initial problem. The way this works raises another question though: is there a way to override all properties instead of setting them individually like I did above for label, description and required? I tried something like this, but there must be a more efficient way?

// Add the template's fields to the form.
$p = new Page;
$template = $templates->get("call");
$fields = $template->fieldgroup;
foreach($fields as $field) {

    $inputfield = $field->getInputfield($p); 
   
    foreach ($inputfield->data as $key => $value) {
     $inputfield->$key = $fields->getField($field, true)->$key;
    }
    
    $form->add($inputfield);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...