⚠ 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

Changing required fields based on Dropdown



Berk
  • profile picture
  • Member

Posted 25 February 2016 - 14:19 PM

Hi,

 

I want to be able to change which fields are required based on result from a dropdown. Ex: if result is A, I want B and C to be required. Else if result is B, I want only C to be required. 

Is there a way to change required fields on the fly?

 

Thanks

Berk

 


titu
  • profile picture
  • Member

Posted 26 February 2016 - 16:07 PM

You could hard code somehting in javascript.


Amit Shah
  • profile picture
  • Member

Posted 28 February 2016 - 18:57 PM

Well that is 1 way - there is another way around - i am sharing you 1 of the example...

 

        $state_required_if = $this->input->post('other_state') ? '' : '|required';
        $crud->set_rules('state', 'State / Province', 'trim' . $state_required_if);
 

well.. here if you see . what i am doing is - if the drop down value selected is other_state - it will ignore state .. else it will ensure it is set required.

Every time the validation needs to be done - GC makes an ajax call .. where it sees this condition - dynamically generated.

 

You can alter the same accordingly to your needs.

 

Happy GCing.