Validate on a grid widget & $record.

A place for users and developers of the Xataface to discuss and receive support.

Validate on a grid widget & $record.

Postby auphi » Mon Feb 11, 2013 5:26 pm

Hey Steve,

I'm using fieldname_validate() on the data within a grid widget, which works like a charm.

I need to pull the id field, so: $record->val('{record_id}'), except here it's returning null. After some further testing, $record->val(ANYTHING) is returning null, but only within the fieldname_validate function. I did a print_r($record) to see if maybe for some reason it was the relationship's record, but I was able to find my correct [{record_id}] => {record_id}, and other form data within it. So, I'm not sure what's going on / why I'm not getting anything back.

Initially, I was using the beforeSave trigger, which worked just fine, but I ended up needing to be able to check against values that were being entered (before changing to fieldname_validate, I actually tried $_POST w/ beforeSave which I've done before to get the user entered values, but that didn't work. I am assuming that relationships are saved differently... I think I read via AJAX instead of PHP).

I went as far as to parse out the ID field from the generated [__id__] tag in $values. Besides seeming incredibly hackish, this only works if there is already relationship data saved.


Everything else is working as expected, but just in case I'm doing something dumb (which I will admit is quite possible)..

The table is called "call_slips", and "call_id" is the primary index.

My fields.ini file shows:
Code: Select all
[inventory]
transient=1
widget:type=grid
relationship=call_slip_inventory
widget:columns="inventory_id, quantity"


relationships.ini:
Code: Select all
[call_slip_inventory]
__sql__ = "SELECT * FROM call_slip_inventory WHERE call_id='$call_id'"


delegate class file - I cut everything else out of inventory__validate except to throw an error to display the id field
Code: Select all
   function inventory__validate(&$record, $value, &$params){
      $params['message'] = 'Record ID: '.$record->val('call_id');
      return 0;
   }


And I'm getting:
Errors
Some errors occurred while processing this form:
Record ID:

as a result.

Thoughts?
auphi
 
Posts: 20
Joined: Sun Oct 21, 2012 7:39 pm

Re: Validate on a grid widget & $record.

Postby shannah » Tue Feb 12, 2013 10:18 am

When you did the print_r($record), what did it say? Usually a print_r on an actual record will return a whole mess of stuff. It is easier to do print_r($record->vals())

-Steve
--
Steve Hannah
@shannah78 (on twitter)
sjhannah.com blog
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Validate on a grid widget & $record.

Postby auphi » Tue Feb 12, 2013 12:00 pm

Ah, that's a nice trick. Yeah, print_r($record) returned a rather jumble of information.

Here are my results of print_r($record->vals()) in the validate function:

Array ( [search_field] => [call_id] => [call_datetime] => [status] => [customer_id] => [site_id] => [contract_id] => [problem] => [call_instructions] => [tech_notes] => [cost] => [type] => [job_id] => [spo_id] => [technician] => [scheduled_datetime] => )


and here are the results from within beforeSave()

Array ( [search_field] => 9 [call_id] => 9 [call_datetime] => Array ( [year] => 2013 [month] => 1 [day] => 8 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => [hours] => 10 [minutes] => 10 [seconds] => 0 ) [status] => OPEN [customer_id] => 603 [site_id] => 3 [contract_id] => [problem] => [call_instructions] => [tech_notes] => [cost] => 0.00 [type] => QU [job_id] => 0 [spo_id] => 0 [technician] => 10 [scheduled_datetime] => Array ( [year] => 2013 [month] => 1 [day] => 8 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => [hours] => 7 [minutes] => 30 [seconds] => 0 ) )


I went ahead and did some further experimentation by setting up another transient field, thinking maybe it had something to do with that, and got:

Array ( [search_field] => [call_id] => [call_datetime] => Array ( [year] => 2013 [month] => 1 [day] => 8 [fraction] => 0 [warning_count] => 0 [warnings] => Array ( ) [error_count] => 0 [errors] => Array ( ) [is_localtime] => [hours] => 10 [minutes] => 10 [seconds] => 0 ) [status] => OPEN [customer_id] => 603 [site_id] => 3 [contract_id] => [problem] => [call_instructions] => [tech_notes] => [cost] => 0.00 [type] => QU [job_id] => 0 [spo_id] => 0 [technician] => [scheduled_datetime] => )


Looking at this, there are 3 pieces of data missing. First is the call_id, and then the last 2 fields, which are on another tab. Next I tried taking the same transient field and moving it to the tab with the grid widget (it apparently ended up getting cut out of the copy/paste I did for the fields.ini in my original post, but my grid widget is on a third tab by itself). Viola, everything shows empty like the original. So, $record data doesn't seem to work between tabs?

I was also still wondering why the id field was empty when it was on the main tab. The [call_id] field is set as widget:type=static, so I commented that out. No change - the first time - but on subsequent page reloads, it shows up. Uncommented the line - reload - still works - reload (again) - empty from then on. I tried this multiple times, with various other fields, and the pattern is consistent (I'm not concerned about the strange pattern, but I thought it was strange that static widgets show up as null).

So it looks like I have 2 separate problems. The first being that $record doesn't seem to hold data from fields on another tab, and the second being that when a field is set to widget type static it also shows null. I can get around the second easily enough, so I'm not concerned with that one as much, but not sure what to do with regards to the first (I really need to use separate tabs).
auphi
 
Posts: 20
Joined: Sun Oct 21, 2012 7:39 pm

Re: Validate on a grid widget & $record.

Postby auphi » Thu Feb 14, 2013 3:48 pm

I wasn't able to find any other references to this issue, but my temporary solution is that I set the [call_id] field to the tab with my grid widget (and just leave it hidden), which allows me to pull the field from $record, and in turn, I am just not able to show it on the main tab of the record (not what I wanted, but not the end of the world). Other than the previously mentioned downside, I feel that this isn't particularly ideal, as it limits data access to the one tab where the corresponding field is located, but it does seem to mostly work for what I need.

I'd be curious to know if there was a more elegant and less limited/hackish solution.
auphi
 
Posts: 20
Joined: Sun Oct 21, 2012 7:39 pm

Re: Validate on a grid widget & $record.

Postby auphi » Fri Feb 15, 2013 12:36 pm

Okay, I ended up having yet another problem, which then led me to find a solution to all of them. Posting this on the odd chance that anyone else runs into a similar situation, and can use this info to help inspire a solution.


My second problem was that apparently the validate function get's called twice. This was driving me crazy, as I couldn't figure out why when I was doing math and saving things to other tables my results kept getting doubled. I finally had the insight to test this by commenting everything else out and going "echo 1; return 1;", and received back "11".

So, this led me to the idea that trying to save values to another table within the validate function just wasn't going to work, and I really needed to do things in the beforeSave function (which I had originally planned on doing). This however brought me back to the reason I switched to using the validate function in the first place, as mentioned... how to get the values from the form accessible in the beforeSave function.

The solution, I realized, was so simple that I'm kicking myself for not thinking of it four days ago. (I'm going to blame all the programming that I have done in C recently - and it's lack of oop/classes.)

Code: Select all
class tables_mytable {

   private $values = array(); //Create a class variable to store the values from the grid field

   function fieldname__validate(&$record, $value, &$params){
      $this->values = $value; //Save $value to the class variable so that it's accessible outside of this function
      return 1;
   }

   function beforeSave(&$record){
      $value = $this->values;
      print_r($value); //Test: print to insure that things are working
   }
}


And now, I copy all my code back to beforeSave(). Everything works, I can use $record->val(id_field) (and everything else) with no problems or weird hack-arounds, and my math doesn't get all messed up from being run twice.
auphi
 
Posts: 20
Joined: Sun Oct 21, 2012 7:39 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 1 guest

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved