⚠ 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

How can I add a null value to a dropdown?



runar-orested
  • profile picture
  • Member

Posted 18 May 2018 - 16:13 PM

Hello:

    My name is Ruben, and while experienced programmer in PHP, I'm a newie on PHP frameworks, except a bit of cakePHP. As a learning exercice, I'm trying to port an old management program to CodeIgniter, and GroceryGrud is an excelent way to quickly add good quality forms, just point it to a table and presto.

 

    In this case, I do have a set of tables containing the ISO 3166-1 codes, to use them as nationality and country of origin dropdowns. I do also have a small control panel to update said tables with extra values, like the name of the country and nationality in several languages [native, english, french, spanish].

    I have found a problem when modifying the tables. The country_info table, contains the numeric and alphanumeric codes of the country, the names in several languages, and a field that says if a country is a dependant territory (a colony), and to which country they belong. If it is a colony, they contain the num3 code of the country (which is the table primary key), if it is not, the field is null. There is also a value, 000, that means 'unknown country'.

 

    I can easily create a relationship 1 to 1, to use the country name in a dropdown, to choose the country of birth of a person. That is because a person will HAVE a country of birth (known or not). So this works well:

    $crud->set_table('colleages');
    $crud->set_relation('nacionality','country_info','nationality_en');

The problem is on the control pannel itself. This does not work well, because dependantOf is a [0,1] to 1 relationship, and using this:

	$crud->set_table('country_info');
	$crud->unset_clone();

	// Required fields
	$crud->required_fields('alpha2','alpha3', 'num3');

	// Index fields for options
	$crud->set_relation('dependentOf','country_info','country_es');

makes dependentOf a set value always, which it is not the case. French Guiana ('GF','GUF',254,250) may be a french colony, but France ('FR','FRA',250, NULL) is not. And wheter a state dependency is unknow or in dispute, like the Falklands on 1982 ('FK','FLK',238,0), it is diferent of being fully independent ('FK','FLK',238,NULL).

 

So, how can I add a null option in the dropdown, so it is recogniced and written back in the database instead of leaving it empty, that ends writting a 0  and leaving all idependent states as dependent with unknown parent country.

 

Yes, I know the example is stupid, and that I could simply define another unused value as 'independant country', like -1, or 999, and keep going, but I want to learn how to do this if it is possible.

  • I've read a few notes in the forum saying that I sould create a model for that table, and have it add a virtual value for the 'independent' that would be replaced seamlessy as NULL in the database. 
  • Other mentions using pre and post query hooks that would do more or less the same.

So, given that I've been using codeigniter and grocery crud for less that two weeks, and I don't really know how the models do/should work:

 

1) Are there the only ways? Is there another way to do it?

2) Given a way, is it the best way to o it?

3) Does it work also with simple values instead of relations? Ex: driver license number: non empty varchar for the number, null for no present.

4) How do I do this? A link to an example with code would be enough, but I wouldn't mind a short explanation about it.

 

Thanks for any help you can send this way.

EDUCATE ME WITH THINE WISDOM, GREAT SAGES!