⚠ 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

required_fields.. question....



jcasanova
  • profile picture
  • Member

Posted 10 May 2012 - 19:05 PM

How can have a requiered field in ADD form, but not in edit form?


any ideas??

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 10 May 2012 - 19:10 PM

I don't understand why to do that but the solution is:

function just_a_test($operation = '')
{
$crud = new grocery_CRUD();

if($operation != 'edit' && $operation != 'update' && $operation != 'update_validation')
$crud->required_fields('first_name','last_name');
else
$crud->required_fields('last_name');

$crud->set_table('your_table');
$output = $crud->render();

....

}


or something similar you get the point ;)

jcasanova
  • profile picture
  • Member

Posted 10 May 2012 - 19:19 PM

thanks I get the point!

the reason why is because in the add forn I had "password" field , and in edit form If password is blank, do not update that field. Do you get the idea?

I was usign this to acomplish it:


$crud->add_fields('username','email','password','password_confirm','first_name','last_name','phone','grupos');
$crud->edit_fields('username','email','edit_password','edit_password_confirm','first_name','last_name','phone','grupos');
$crud->required_fields('username','email','first_name','last_name','phone','grupos','password','password_confirm');


so in the edit form the edit_ fields do not exists in the db (refering to the bug) so it works....

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 10 May 2012 - 20:11 PM

Now yes it make sense.

But remember that even if the field WAS at the database the same code will work for you. The required fields checks only if the field is into the add/edit form. Just a tip to know, perhaps you will need it.

The bug is that the change_field_type doesn't work with fields that they are not in the table.

jcasanova
  • profile picture
  • Member

Posted 10 May 2012 - 20:24 PM

You were right... I did go with this... (is someone needs it)



if($uri2 != 'edit' && $uri2 != 'update' && $uri2 != 'update_validation'){
$crud->required_fields('username','email','first_name','last_name','phone','grupos','password','password_confirm');
$crud->set_rules('password', 'Password', 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$crud->set_rules('password_confirm', 'Password Confirmation', 'required');
}else{
$crud->required_fields('username','email','first_name','last_name','phone','grupos');
$crud->set_rules('password', 'Password', 'min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$crud->set_rules('password_confirm', 'Password Confirmation', '');
}


So in the edit It is not required... if not blank it works like in the add checking the max/min lenght, so I get rid of the thinking in the return value from the callback that was killing me.


Thanks for the help and the tip.