Jump to content

Prevent data saving for Fieldtype


Recommended Posts

I'm building a Fieldtype with multiple columns. ( DB structure on the end of the post )

How do I prevent data saving with the API when saving to $page->fieldname and set an wireException ?

I only want to allow saving for subfields:
$page->fieldname->data_one = 'value1'; (data column will be used for this one)
$page->fieldname->data_two = 'value2';
 
Disallow this: 
$page->fieldname = 'bad for me';
 
// DB Structure

CREATE TABLE `field_myfield` (
  `pages_id` int(10) unsigned NOT NULL,
  `data` mediumtext NOT NULL,
  `data_two` mediumtext NOT NULL,
  `data_int_one` int(11) NOT NULL DEFAULT '0',
  `data_int_two` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`pages_id`),
  KEY `data_int_one` (`data_rows`),
  KEY `data_int_two` (`data_columns`),
  FULLTEXT KEY `data` (`data`),
  FULLTEXT KEY `data_two` (`data_one`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Link to comment
Share on other sites

You should be able to do this with sanitizeValue(). My experience with fieldtypes is very scarce, but AFAIK that's what this method is for :)

Edit: unless you're already using it as a base for your own fileldtype, take a look at how sanitizeValue() of FieldtypeEvents works. That pretty much explains it, I think. If you don't like the value, throw exception.

I'm not entirely sure if it's possible to entirely disable setting value via API though, if that's what you're after -- i.e. giving $page->yourfield a value that's an instance of the object it expects -- without disabling page edits etc. too.

Edited by teppo
  • Like 1
Link to comment
Share on other sites

I already have the logic in place for all other scenario's, using $value->isChanged('sub-field-name'), 

The only thing I don't catch right now is the scenario I want to have (prevent save). I will take a look at this. I think you get me started !

Thanks Teppo,

Link to comment
Share on other sites

Now I can't save data from the API side to the field.

I moved all data around put most logic in sanitizeValue()

On the admin side, the processInput() is handling the trackChange, so sanitizeValue knows how to kick in.

Now if I try to populate data from the API side, trackChanges are never fired, and I don't know where to fire those.

Who can give me a clue ? ( I did studied the FieldtypeMapMarker, eventfield fieldtypePhone )

source on github

//-------

trackChange is fired, but while saving with the API the InputField is ignored. In my case the Inputfield sets some variables. 

So when I save through the API, some variables are not set, and thus I know the API is saving, not the InputField.

Edited by Martijn Geerts
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...