Why is password field updated when updating other fields?

Why is password field updated when updating other fields?

ekkodillekkodill Posts: 2Questions: 1Answers: 0
edited May 2015 in DataTables

In my datatables editor table with users i cant update any field without password changing each time. I guess it's because setFormatter function returns password_hash ( $val, 1) each time. But how do i fix this, so i can:
1. Update fields without password changing.
2. Keep password hash somehow so new or updated passwords will be hashed.

        Field::inst( 'user.password' )
            ->validator( 'Validate::notEmpty' )
            ->setFormatter(function ( $val ) {
                return password_hash( $val, 1);
             }),
    "fields": [
            {
                "label": "Passord",
                "name": "user.password",
                "type": "password"
            },
 ]

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin
    Answer ✓

    I think the quickest solution is to use the preSubmit event and check if the password value has been set. If not, just delete it and the server-side scripts will not attempt to set it:

    editor.on( 'preSubmit', function ( e, data ) {
      if ( ! data.data['user.password'] ) {
        delete data.data['user.password'];
      }
    } );
    

    A better solution would probably be to use the set() method of the PHP libraries and setting that to false if the field is empty. However, that would really need the set() method to accept a closure function so you could compute if you want to set it or not - it currently doesn't have that ability.

    An even better solution will be in v1.5 :-). I'm going to add an option so that only values which have changed will be submitted. In this case the password will not have changed (presumably you have a getFormatter making it an empty string so they can't read the password hash!) so it wouldn't be submitted automatically.

    Allan

  • ekkodillekkodill Posts: 2Questions: 1Answers: 0

    Thank you for taking time answering:)
    I didnt really get it to work, but i figured i would just remove the whole password field for now.

    Thank you anyways.

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    Hi, I have been testing what you have written and not work for me, Can you give an example?
    thanks!

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin

    The above will not work in 1.5 due to a change in the client / server comms.

    I would suggest trying the submit options that are present in 1.5.

    Or better yet would be to never read the password from the database and only write when a value is submitted. That can be done with server-side events.

    Allan

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    ok, it works! But you can do to it is done without the click event.To operate with the buttons above http://prntscr.com/8m3a2d
    I have :

    $('#myTable').on( 'click', 'tbody tr', function () {
        editor.edit( this, {
            submit: 'changed'
        } );
    } );
    
  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    up!

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin

    I'm not sure why this was bumped - you noted in your previous message that it works for you now. Could you clarify what your question is for me please?

    Allan

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0
    edited October 2015

    ok, the problem is that now edits the onclick event when I click the row.

    I want to edit when I press the Edit button(Modificar) http://i.imgur.com/yqanLsK.png

    greetings and thanks!

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin

    I'm afraid I would need a link to the page to understand what is going wrong here. Without being able to see the code I don't know why it would immediately enter editing mode on click in the row, unless you have a click event handlers that it triggering that action.

    Allan

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    ok! I you send the information for message.
    thanks!

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin

    Thanks for the information. In your code (script.js) you have:

            $('#tablaUsuarios').on('click', 'tbody tr', function () {
                editor.edit(this, {
                    submit: 'changed'
                });
            });
    

    That is why it is triggered an edit whenever you click a row - it is being told to :-). If you don't what that, remove that code.

    Allan

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    Yes, but now if I modify a field, it changes the password, though I do not change it.

    I want that when I edit with the button to edit, the password does not change if I do not change it.

    $('#tablaUsuarios').on('click', 'tbody tr', function () {
        editor.edit(this, {
            submit: 'changed'
        });
    });
    

    With this it works but only when I click in the row and not in the button to edit(MODIFICAR) http://i.imgur.com/yqanLsK.png .

    Thank you and sorry for the inconvenience!

  • allanallan Posts: 54,688Questions: 1Answers: 8,553 Site admin

    I recently wrote a blog post about how to handle the submission of password fields.

    Also, I think I understand why you added that event handler code now - you only want the changed fields to be submitted? If so, then use the formOptions.main object to set the submit option to changed (it is default all).

    Allan

  • rompeplatosrompeplatos Posts: 20Questions: 4Answers: 0

    Already work!. Thank you very much, a greeting! :-):-):-):-)

    The code in PHP Server

    ->on( 'preEdit', function ( $e, $id, $values ) {
            if ( $values["usuarios"]["pass"] === "" ) {
                $e->field( 'usuarios.pass' )->set( false );
            }
        })
    
This discussion has been closed.