⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

password required in add form but not required in edit form



ocrama
  • profile picture
  • Member

Posted 03 October 2013 - 13:30 PM

hi all,

 

i have the password field that is required in add form but should not be required in edit form.

 

For the edit form i have 

$crud->callback_edit_field('password',array($this,'_set_password_input_to_empty'));

that set password form empty.

When i submit the form the library shows me a message "Password field required".

How can i sibmit the form whit password field empty?

 

thanks.


briggers
  • profile picture
  • Member

Posted 03 October 2013 - 14:09 PM

Hi ocrama,

 

Your best bet is to use add_fields() for your add form and edit_fields() for your edit form, leaving out the password field in the edit case.

 

If you need to set the password field to null or empty after an edit you can use callback_before_update  to set the password field to whatever you want.


ocrama
  • profile picture
  • Member

Posted 03 October 2013 - 14:36 PM

hi, thank you for reply!

 

i already use edit_fields() but i can't leave out the password field in edit form: when i edit a user i can edit the name, the surname, the username or the password. when i show the edit form, the password field is empty. 

If i want change the password type a new one in that field. But if i don't want change the password i have to leave it blank.

In these case, if i submit the form, it gives me a "password required field" message.

So i'd want that the password fileld is not required in edit form.


briggers
  • profile picture
  • Member

Posted 03 October 2013 - 16:01 PM

Hi,

 

I maybe misunderstood you. The example below taken straight from the API Manual shows exactly how to have the add/edit work with an encrypted password.

public function users(){
    $crud = new grocery_CRUD();
 
    $crud->set_table('db_user');
    $crud->set_subject('User');
    $crud->required_fields('user_name');          
    $crud->columns('user_name','email','real_name','active', 'groups');
    $crud->fields('user_name','email','password','real_name','active', 'groups');
 
    $crud->field_type('password', 'password');
 
    $crud->callback_before_insert(array($this,'encrypt_password_callback'));
    $crud->callback_before_update(array($this,'encrypt_password_callback'));
    $crud->callback_edit_field('password',array($this,'decrypt_password_callback'));
 
    $output = $crud->render();
    $this->_example_output($output);
}
 
function encrypt_password_callback($post_array, $primary_key = null)
{
    $this->load->library('encrypt');
 
    $key = 'super-secret-key';
    $post_array['password'] = $this->encrypt->encode($post_array['password'], $key);
    return $post_array;
}
 
function decrypt_password_callback($value)
{
    $this->load->library('encrypt');
 
    $key = 'super-secret-key';
    $decrypted_password = $this->encrypt->decode($value, $key);
    return "<input type='password' name='password' value='$decrypted_password' />";
}

If you want, you can include password in the list of required fields.


ocrama
  • profile picture
  • Member

Posted 03 October 2013 - 17:49 PM

thanks, but i have sha1 encryption that is one way hash...i can't decrypt passwords. I'm using ion_auth but maybe...grocery_crud isn't the correct tools...??


briggers
  • profile picture
  • Member

Posted 03 October 2013 - 18:07 PM

Well, of course, the choice of tools is up to you - pick the right one for your need. But if you cannot decrypt the password you cannot edit it so you cannot display it in the form whatever tool you use.

You could try a callback_before_update that has the following function

 

if new password = nothing

  get old password from database

  set new password = old password

else

  encrypt password

  set password = encrypted password

end if

 

Sorry, I can't see any other way working, maybe someone else has a better idea.


davidoster
  • profile picture
  • Member

Posted 11 October 2013 - 05:00 AM

Well maybe the best way is to use the callback_before_update and see do from there any special processing.

Since you're using sha1 by default you can't let the user "edit".

So in that sense I would omit all together the password field from the GC view and I would make another controller function that contains a form about, just changing the password, not editing. This would give you less headache I think.