⚠ 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 forum is read-only and soon will be archived. ⚠


password required in add form but not required in edit form


  • Please log in to reply
6 replies to this topic

#1 ocrama

ocrama

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 03 October 2013 - 01: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.



#2 briggers

briggers

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 03 October 2013 - 02: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.



#3 ocrama

ocrama

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 03 October 2013 - 02: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.



#4 briggers

briggers

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 03 October 2013 - 04: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.



#5 ocrama

ocrama

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 03 October 2013 - 05: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...??



#6 briggers

briggers

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts

Posted 03 October 2013 - 06: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.



#7 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users