Jump to content


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

validation form

  • Please log in to reply
4 replies to this topic

#1 Marcelo Bezerra

Marcelo Bezerra

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 07 December 2013 - 09: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='' />";

 



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

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 :)



#3 Marcelo Bezerra

Marcelo Bezerra

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 08 December 2013 - 05:27 AM

I will try that, thanks for yout answer



#4 Marcelo Bezerra

Marcelo Bezerra

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 08 December 2013 - 05:47 AM

it worked, thanks again



#5 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 08 December 2013 - 07:00 AM

Happy to help .. most welcome ..

 

and Happy GCing :)







Also tagged with one or more of these keywords: validation, form

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users