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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using AjaxUniqueTextField


Go to End


4 Posts   2302 Views

Avatar
jebbench

Community Member, 7 Posts

9 July 2011 at 9:01am

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

Forum Moderator, 5523 Posts

10 July 2011 at 1:23pm

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

Community Member, 7 Posts

11 July 2011 at 3:00am

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

Cheers,

James Bench

Avatar
Willr

Forum Moderator, 5523 Posts

11 July 2011 at 9:19am

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!