how do I set value of hidden fields

how do I set value of hidden fields

ourteamwebourteamweb Posts: 3Questions: 1Answers: 0

I need to set the value of hidden fields when adding a new record.

The Username field is hidden and should be the same value as the Email field.

The Usergroups field is hidden and will always be "BUILDER".

I can't find any documentation on how to this within the Editor server script

Here is my current code:

Editor::inst( $db, 'members', 'id' )
  ->fields(
    Field::inst( 'Custom1' )
        ->validator( 'Validate::notEmpty' ),
    Field::inst( 'Name' )
        ->validator( 'Validate::notEmpty' ),
    Field::inst( 'Email' )
        ->validator( 'Validate::email', array( 'empty' => false ) ),
    Field::inst( 'Custom2' )
        ->validator( 'Validate::notEmpty' )
  )
  ->where( 'Usergroups', '%BUILDER%', 'LIKE' )
  ->process( $_POST )
  ->json();

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin
    edited June 2014 Answer ✓

    For the Usergroups field use the fields.def option to set a default value that will always be used.

    For the Username, use the preSubmit event to set the value:

    editor.on( 'preSubmit', function ( e, d, type ) {
      if ( type === 'create' || type === 'edit' ) {
        d.data.Username = d.data.Email;
      }
    } );
    

    Allan

  • ourteamwebourteamweb Posts: 3Questions: 1Answers: 0
    edited June 2014

    Thanks Allan, that worked for me.

    I added this to my js

    {
        "label": "Usergroups",
        "name": "Usergroups",
        "type": "hidden",
        "def": "BUILDER"
    },
    

    And I added this to my Editor code:

    Editor::inst( $db, 'sitelok', 'id' )
        ->fields(
            Field::inst( 'Usergroups' ),
            ...
    

    And I repeated the same process with other hidden fields.

This discussion has been closed.