Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » DataObject validation for duplicate record

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 781 Views
  • congii
    Avatar
    Community Member
    6 Posts

    DataObject validation for duplicate record Link to this post

    Hi,

    I would like to check if a certain record already exist before adding it in the table. Show meaningful error message
    to the user too if it failes. I am using DataObject and ModelAdmin to manage those tables.

    class Country extends DataObject {

    static $db = array('CountryName');

    ....

    //check if country exist before saving

    ....

    }

    Is there anyone who would like to share some idea how to accomplish this?

    Thanks!

  • lx
    Avatar
    Community Member
    82 Posts

    Re: DataObject validation for duplicate record Link to this post

    The netefxvalidator works well with modeladmin, for ss 2.4 and 3.0
    The unique rule is what you are looking for.

    You have to add the validator in a function getCMSValidator

    Download: https://github.com/lx-berlin/NetefxValidator
    Documentation: http://www.netefx.de/Silverstripe-NetefxValidator.php

    Lx

  • soolan
    Avatar
    Community Member
    11 Posts

    Re: DataObject validation for duplicate record Link to this post

    Hello lx,
    Can you share your thoughts on vlaidation on CMS (backend) please.

    Assuming I have a Product DataObject, containing name, price and date fields,
    how can I validate these fields while I'm adding a new product at the backend.

    A piece of sample code for validator function would be very helpful.
    Thank you very much

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: DataObject validation for duplicate record Link to this post

    Please read this thread for a code example...

    http://www.silverstripe.org/general-questions/show/17007

  • lx
    Avatar
    Community Member
    82 Posts

    Re: DataObject validation for duplicate record Link to this post

    Hi soolan,

    here are some example rules, that are a little bit more complex than just "required".
    I hope they are working.

    regards
    lx

    class Product extends DataObject {

    ...

    public function getCMSValidator() {

    $rule_Name_required = new NetefxValidatorRuleREQUIRED ("Name", "The name of the product is required.");
    $rule_Price_higher_zero = new NetefxValidatorRuleGREATER ("Price", "You need to enter a price higher than 0.", null, "0");
    $rule_Date_required = new NetefxValidatorRuleREQUIRED ("Date", "The date is required.");
    $rule_Date_in_future = NetefxValidatorRuleFUNCTION("Date", "The date has to be in the future.", null,array('NetefxValidatorLibraryDate', 'DateIsMinDaysAfterToday', array('date' => 'Date', 'min' => 1)));

    $validator = new NetefxValidator($rule_Name_required, $rule_Price_higher_zero, $rule_Date_in_future, $rule_Date_required);

    return $validator;
    }

    ...

    }

  • soolan
    Avatar
    Community Member
    11 Posts

    Re: DataObject validation for duplicate record Link to this post

    Thank you very much guys.

    781 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.