⚠ 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

form validation and filtering in GC



Nitin Thokare
  • profile picture
  • Member

Posted 27 May 2014 - 08:45 AM

I'm trying to validate form fields before it gets updated (if editing)/added (if adding new).

 

http://stackoverflow.com/questions/23872802/grocery-crud-and-form-validation

 

I'm begineer to GC and codeigniter. I don't know the difference between validation and filtering. What is the difference?

 

If I'm doing just validation by using set_rules (which is not enough), then where and how to do filtering?


Amit Shah
  • profile picture
  • Member

Posted 27 May 2014 - 13:27 PM

What type of filtering you looking at.. please clarify... to our understanding filtering is a terminology to restrict the output of the data limited to a certain constraints. If thats what you looking for then crud have where function. Will like u to refer to documentations before posting in.


Nitin Thokare
  • profile picture
  • Member

Posted 27 May 2014 - 16:40 PM

Oh.. Ya, I got it... filtering is just filtering result before displaying into table (same as searching by query).

 

So, my query about validation only.

 

Just like in codeigniter we set rule as

$this->form_validation->set_rules('name', 'Name', 'trim|max_length[50]|required|is_unique[company.name]');

which can be done in GC as, (I guess it's correct)

$crud->->set_rules('name', 'Name', 'trim|max_length[50]|required|is_unique[company.name]');

with this my function in controller goes like this:

 

function view_company(){
$this->check_logged_in();


$crud = new grocery_CRUD();
   /*set theme and table*/
   $crud->set_table('company');

   /*column to be in displayed table and fields for view/add/edit */
   $crud->columns('Id','Name','Email','Address','LogoURL');
   $crud->fields('Name','Description','Email','Address','LogoURL');
   $crud->display_as('LogoURL','Logo');

   /*required field in add/edit*/
   $crud->required_fields('Name','Address');
   $crud->set_field_upload('LogoURL','images');


   /*form validation rules*/
   $crud->set_rules('name', 'Name', 'trim|max_length[50]|required|is_unique[company.name]');
   $crud->set_rules('description', 'Description', 'trim|max_length[255]');
   $crud->set_rules('email', 'Email', 'trim|valid_email|max_length[255]|is_unique[company.email]');
   $crud->set_rules('address', 'Address', 'trim|required|max_length[255]');

   $output = $crud->render();
   $crud->set_subject('Company');

   $this->load->view('admin/view/company',$output);
}

Now when I try to add new record or edit existing row, it's not validating all fields as mentioned in set_rules above. Just required field works, that too due to addition of $crud->required_fields().

 

I'm not getting what's more need to be done so as to validate form data with above rules during edit and add new.

 

(I'm unable to understand usage of various callback functions. Which one to use where and how?)

 

I tried some thing like..

$crud->set_rules('name', 'Name', 'callback_validate_company_data');

function validate_company_data(){
}

But not getting what to write in validate_company_data().  I guess every rule need not be checked manually, instead we can take advantage of codeigniter's $this->form_validation->set_rules() or some other similar feature in GC. (in codeigniter we use $this->form_validation_run() . Is there anything similar needed in GC? If yes, what's it? And if not, why is above code not working?)

 

Please help me to understand, if possible with full example, how to complete above code so the form validation works perfectly as in codeigniter.

 

Thanks.