Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Using AjaxUniqueTextField


Reply

4 Posts   1203 Views

Avatar
jebbench

9 July 2011 at 9:01am Community Member, 7 Posts

Hi,

I'm trying to set up a site using SilverStripe and I want to add a field to pages that must be unique, it looks like I can do this using the AjaxUniqueTextField class but I can't find any documentation on how to use it.

The constructor takes several arguments but I don't know what several of them should be, could someone please point me towards some documentation or provide an example of a text field that must be a unique value.

$name - The database field this form field is for.
$title - What to display to the user.
$restrictedField - ?
$restrictedTable - ?
$value - ?
$maxLength - Max length the input can be.
$validationURL - ?
$restrictedRegex - A regex the input must match to be valid?

public static $db = array(
'page_id' => 'varchar(32)'
);

function getCMSFields() {
$fields = parent::getCMSFields();

$fields->addFieldToTab('Root.Content.Main', new AjaxUniqueTextField('page_id', 'Page Id', $restrictedField, $restrictedTable, $value, 32, null, $restrictedRegex), 'Content');

return $fields;
}


Cheers,

James Bench

Avatar
Willr

10 July 2011 at 1:23pm Forum Moderator, 5511 Posts

AjaxUniqueTextField is one of those old legacy field types that have been around but no one really used it. I even thought we had deprecated that a while back but doesn't look like it.

Taking a stab in the dark (and looking at the file)

* $restrictedField - a string with the name of the database field you want force uniqueness on. e.g "Title"
* $restrictedTable - a string with the name of the database that field exists in, e.g "SiteTree"
* $value - the default value of the field (just like {TextField->value}
* $validationURL - optional, callback url, e.g "home/validate/foo" which returns a bool. Must be true for validation to pass.
* $restrictedRegex - yep, like you said, a regex that must pass for validation to pass.

Avatar
jebbench

11 July 2011 at 3:00am Community Member, 7 Posts

What should I be using instead of AjaxUniqueTextField if it's a legacy class?

Cheers,

James Bench

Avatar
Willr

11 July 2011 at 9:19am Forum Moderator, 5511 Posts

What should I be using instead of AjaxUniqueTextField if it's a legacy class?

Your own code, but try the AjaxUniqueTextField and see if you can get it to work, may just do exactly what you need!