⚠ 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

Active / Inactive bit field on add



ezgoen

ezgoen
  • profile picture
  • Member

Posted 23 April 2013 - 07:44 AM

Ho folks,

 

I hope this hasn't been asked and answered - i've searched and found no solution.

 

I didn't spot this until after deployment.

 

Scenario

Table has a bit field that is rendered as active /inactive

This field is set up as mandatory

 

on Add New record it default to active just fine - this is what I want and what is expected.

if - instead of "save and go back to list" i click 'Save', I am presented with a new record - as expected.

The default for the bit field is active - as expected.

When I click "save" on this I get error - the active field is mandatory.

I don't understand why nor can I track down why in the code.

I suspect jQuery is the culprit here but I haven't had time to track it down.

 

Has anyone else encountered this problem - if so - whats the fix??

 

I appreciate any help with this..

 

Cheers

 

Ez

 


davidoster

davidoster
  • profile picture
  • Member

Posted 23 April 2013 - 22:45 PM

Another thing that haven;t seen before!

Have you tried, http://www.grocerycrud.com/documentation/options_functions/callback_before_insert?


ezgoen

ezgoen
  • profile picture
  • Member

Posted 24 April 2013 - 00:45 AM

I found the culprit 

 

its in flexigrid-add.js

function clearForm()
	{
		$('#crudForm').find(':input').each(function() {
	        switch(this.type) {
	            case 'password':
	            case 'select-multiple':
	            case 'select-one':
	            case 'text':
	            case 'textarea':
	                $(this).val('');
	                break;
/* This sets the checked value to false - when it probably shouldn't 
   what if (like in my case) there is a default value 
   and should a group of radio buttons representing true or false 
   ever really have no checked items??
*/
	            case 'checkbox':
	            case 'radio':
	                this.checked = false;
	        }
	    });

		/* Clear upload inputs  */
		$('.open-file,.gc-file-upload,.hidden-upload-input').each(function(){
			$(this).val('');
		});
		
		$('.upload-success-url').hide();
		$('.fileinput-button').fadeIn("normal");
		/* -------------------- */		
		
		$('.remove-all').each(function(){
			$(this).trigger('click');
		});
		
		$('.chosen-multiple-select, .chosen-select, .ajax-chosen-select').each(function(){
			$(this).trigger("liszt:updated");
		});
	}

 

When the form fist loads it exhibits the correct behavior

after "save" the field becomes unchecked and no longer set to its default.

 

How should I approach this - embed some js variables that this function checks for default values or ???

 

 

Cheers

 

 

Ez


ezgoen

ezgoen
  • profile picture
  • Member

Posted 24 April 2013 - 01:48 AM

Ok

 

I hacked around the problem - its a bit brittle, perhaps someone can offer a more elegant approach.

 

You probably know I use a subclass of grocery_crud - so I can make my own changes on the fly

without touching the underlying code.

 

so I duplicated and modified get_true_false_input like so .

protected function get_true_false_input($field_info,$value)
	{
		$this->set_css($this->default_css_path.'/jquery_plugins/uniform/uniform.default.css');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/jquery.uniform.min.js');
		$this->set_js($this->default_javascript_path.'/jquery_plugins/config/jquery.uniform.config.js');
		
		$value_is_null = empty($value) && $value !== '0' && $value !== 0 ? true : false;
		
		$input = "<div class='pretty-radio-buttons'>";
		
		$checked = $value === '1' || ($value_is_null && $field_info->default === '1') ? "checked = 'checked'" : "";
		//set up a class name to add to the input field that tells us if its supposed to default to checked - 
		//we use this in flexigrid add
		$initial_checked = $value === '1' || ($value_is_null && $field_info->default === '1') ? " radio-default" : "";
		$input .= "<label><input id='field-{$field_info->name}-true' class='radio-uniform{$initial_checked}'  type='radio' name='{$field_info->name}' value='1' $checked /> ".$this->default_true_false_text[1]."</label> ";
		
		$checked = $value === '0' || ($value_is_null && $field_info->default === '0') ? "checked = 'checked'" : ""; 
		//set up a class name to add to the input field that tells us if its supposed to default to checked - 
		//we use this in flexigrid add$initial_checked = $value === '0' || ($value_is_null && $field_info->default === '0') ? " radio-default" : "";
		$input .= "<label><input id='field-{$field_info->name}-false' class='radio-uniform{$initial_checked}' type='radio' name='{$field_info->name}' value='0' $checked /> ".$this->default_true_false_text[0]."</label>";
		
		$input .= "</div>";
		
		return $input;
	}	
	

 

 

 

then I patched flexigrid-add.js like so:

 

	function clearForm()
	{
		$('#crudForm').find(':input').each(function() {
	        switch(this.type) {
	            case 'password':
	            case 'select-multiple':
	            case 'select-one':
	            case 'text':
	            case 'textarea':
	                $(this).val('');
	                break;
	            case 'checkbox': 
					this.checked = false;
					break;
	            case 'radio':
					/*use classnames to determine if this is default or not */
					if ($(this).hasClass('radio-default')){
						this.checked = true;
						/*little hack for the uniform plugin */
						if (!$(this).parent().hasClass('checked')){
							$(this).parent().addClass('checked');
						};
					
					} else {
						this.checked = false;
						if ($(this).parent().hasClass('checked')){
							$(this).parent().removeClass('checked');
						}; 
					}
			}
	    });

 

 

Presto, problem fixed - for now.

 

The check boxes probably need a similar treatment - but I'm not currently using them.

 

Should this logged somewhere as a bug???

 

 

Cheers

 

 

Ez


davidoster

davidoster
  • profile picture
  • Member

Posted 25 April 2013 - 13:14 PM

Maybe post this code on github as an issue?

https://github.com/scoumbourdis/grocery-crud/issues


ezgoen

ezgoen
  • profile picture
  • Member

Posted 05 May 2013 - 23:49 PM

I have posted this in github now - and since 

have discovered that defaults are not set for successive adds for any fields.

Perhaps this is why the default value feature is only partially implemented??

 

I must admit I didnt check the multi add functionality either when I built my own default value feature.

 

I think the way to go is to pass a javascript array of default values and have the fields set to these on add.

The problem here is it could get nasty, how far is it worth going for default values?

One might want to have the default value increment - would need to tell js thats what we want.

OR we might want next index id from the database - we would have to return that inside the ajax/json 

form post. But there's no reliable way to get next insert id - should be ok because we should be using auto increment feature of the database and therefor NOT need a default value.

 

Still bears some thinking as to the best way forward

 

 

Cheers

 

 

Ez