⚠ 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

How to make a field required on insert but NOT on update?



Marcelo Bezerra

Marcelo Bezerra
  • profile picture
  • Member

Posted 07 December 2013 - 21:14 PM

Hello guys

 

I´m using grocery on a project in the last 2 days and it looks very good so far. This is my first question here in the forum =)

 

My question is: How to make a field required on insert but NOT on update?

 

The table has a password field that must be filled in the insert, but in the update, if the user leaves the password blank, the system will simply not change it in the table.

 

I havent found any example of how to do this.

 

Can you help me? Thanks.

 

Here is my code

 

function tb_usuario()
{
 
  $crud = $this->grocery_crud; 
  $crud->set_theme('twitter-bootstrap');
  $crud->unset_print();
  $crud->set_rules('email','Email','required|valid_email');
  $crud->set_rules('senha','Senha','required');
  $crud->set_rules('login','Login','required');
  $crud->field_type('senha', 'password');
 
   $crud->callback_edit_field('senha',array($this,'set_password_input_to_empty'));
   $crud->callback_add_field('senha',array($this,'set_password_input_to_empty'));
 
  $crud->callback_before_insert(array($this,'encrypt_password_callback'));
  $crud->callback_before_update(array($this,'encrypt_password_callback')); 
  $output = $crud->render();
  $tituloPagina = " Gerenciamento de Usuário ";
  $this->defaultOutput($output, $tituloPagina);
}
 
 
function encrypt_password_callback($post_array) {
 
if(!empty($post_array['senha']))
   {
     $post_array['senha'] = md5($post_array['senha']);
   }
   else
   {
     unset($post_array['senha']);
   }
   
   return $post_array;
}
 
function set_password_input_to_empty() {
   return "<input type='password' name='senha' value='' />";

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 December 2013 - 04:00 AM

hi there

 

you can use it with

if($crud->getState() == 'add' || $crud->getState() == 'insert_validation') {

      //here u can put code for validations only for add ... and not update

}

 

Happy GCing :)


Marcelo Bezerra

Marcelo Bezerra
  • profile picture
  • Member

Posted 08 December 2013 - 05:27 AM

I will try that, thanks for yout answer


Marcelo Bezerra

Marcelo Bezerra
  • profile picture
  • Member

Posted 08 December 2013 - 05:47 AM

it worked, thanks again


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 December 2013 - 07:00 AM

Happy to help .. most welcome ..

 

and Happy GCing :)