⚠ 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

different validation rules in one controller



ceroberoz
  • profile picture
  • Member

Posted 15 August 2013 - 20:02 PM

Hi, is that possible to add a validation into one controller so that one column won't be unlocked until some requirement done?

ie: this is my table structure

 

foo

====

id (PK)

name

status (enum pending, paid & done)

 

foo2

====

id (PK)

id_foo

name

upload1 (available where foo status = pending)

upload2 (available where foo status = paid)

status (enum pending & paid)

 

I was think about using set_relation function but I can't get the right validation to get it work :/


davidoster
  • profile picture
  • Member

Posted 15 August 2013 - 22:18 PM

You can do this by using a callback on the rules. set_rules as the manual says is the same as set_rules of CodeIgniter.

Check here how to structure a callback.


ceroberoz
  • profile picture
  • Member

Posted 16 August 2013 - 01:45 AM

You can do this by using a callback on the rules. set_rules as the manual says is the same as set_rules of CodeIgniter.

Check here how to structure a callback.

So if I want to call the <upload1> collumn and hidden the <upload2> collumn  I just create something like this right?

<? php

public function status_check($str, $upload1=NULL, $upload2=NULL)
    {
        if ($str[foo.status] == 'pending')
        {  
            //$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            $this->grocery_crud->field_type('upload2', 'hidden', $upload2);
            
            return TRUE;
        }
        else
        {
            $this->grocery_crud->field_type('upload1', 'hidden', $upload1);
            return FALSE;
        }
    }

right? I am still confused about how to applied this from CI examples.

---

or is that possible to add the 4th field into hidden fields to query the requirements that I need? (the column fields won't show up until one condition applied)

http://www.grocerycrud.com/documentation/options_functions/field_type

thanks for your answer before :D


Amit Shah
  • profile picture
  • Member

Posted 17 August 2013 - 12:27 PM

hi ceroberoz

 

in the call (as above) - $this->grocery_crud->field_type('upload2', 'hidden', $upload2);

the 3rd parameter of the function is used for setting a default value.

 

Correct me if i am wrong on the same. What you want is that based on the drop down selection of yours, you want either of the field to be enabled / visible for upload.

 

If that is the case then you need to do it by custom javascript.

Create your javascript where it binds to the element (drop down on which you want to act on change of its value)

 

$crud->set_js(<your script file>);

 

In that what you can do is.. hide / disable both the fields by default on load.

Then based on the selection of the value from dropdown, enable the respected field.

 

 

A suggestion - Do not use the jquery ready method. It wont work as jquery will be loaded after your script is loaded. Instead of the same use window.onload method. inside the method you can play around with jquery no problem cuz the function will be called when window is loaded and by that time jquery will also be loaded.