Set the value when using the select Type

Set the value when using the select Type

mikedmasonmikedmason Posts: 39Questions: 12Answers: 0

Hi Guys,
I am using the select Type to have a drop down when i create or edit a record. How would i set the value to be the current database value if the user selects no? Or can I have it not update if the user sets it to no?

                            label: "Reset to default",
                            name: "user_data",
                            type:  "select",
                            options: [
                            { label: "Yes", value: "4545" },
                            { label: "No",  value: "user_data" }
                            ] 

Thanks
Mike Mason

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,776Questions: 1Answers: 10,112 Site admin

    Hi Mike,

    The value used should be based on the value property (not the label). So int he case above you would need to use:

    editor.field( 'user_data' ).val( 'user_data' );
    

    Is user_data just a fill you've put in for the above example, or is that actually the value that you would use in production code?

    Allan

  • mikedmasonmikedmason Posts: 39Questions: 12Answers: 0

    Hi Allan,
    user_data was just an example. What we are trying to achieve is a simple pin system. That a user can reset a users pin if needed. So I would like the a user to be able to select yes to reset the pin and no to leave it alone. So on an edit if they need to update the users name but not the pin then they would leave it as no. But on a new entry or edit they would set it to yes so it would set the default pin.

    So would i put the editor.field( 'user_data' ).val( 'user_data' ); in the value section?

                                label: "Reset Pin",
                                name: "user_pin",
                                type:  "select",
                                options: [
                                { label: "Yes", value: "4545" },
                                { label: "No" , value: editor.field( 'user_pin' ).val( 'user_pin' );}
                                ] 
    
  • allanallan Posts: 61,776Questions: 1Answers: 10,112 Site admin
    Answer ✓

    No, that won't work unfortunately as that function returns a field instance, not the value. It is also executed before the point at which the field would have any useful data in it.

    One possible option is to set a predefined value for the No value and then remove it from the data submitted to the server if that value is selected - that means the libraries won't update that field. For example, using preSubmit:

    editor.on( 'preSubmit', function ( e, data, action ) {
      if ( action === 'edit' && data.data.user_pin == '0000' ) {
        delete data.data.user_pin;
      }
    } );
    

    Would that work for you?

    Allan

  • mikedmasonmikedmason Posts: 39Questions: 12Answers: 0

    Hi Allan that would work. I will give it a go.

    Thanks
    Mike Mason

This discussion has been closed.