Custom field with validation


#1

Hello, I would like to create custom field in user profile with validation of insertion value from 0 to 100. So I have decided to extend PhabricatorUserCustomField class. The problem is, I don’t know, how implement some methods for correct working custom field.

My code is below:

final class EmployeeOverloadRatioField extends PhabricatorUserCustomField {

  private $value;

  public function getValue() {
    return $this->value;
  }

  public function getFieldKey() {
    return 'iba:overload-ratio';    
  }

  public function getModernFieldKey() {
    return 'overload-ratio';        
  }

  public function getFieldName() {
    return pht('Employee overload ratio');    
  }

  public function getFieldDescription() {
    return pht('Employee overload ratio in percent');
  }

  public function readValueFromObject(PhabricatorCustomFieldInterface $object) {            
    return PhabricatorCustomField::readValueFromObject($object);        
  }

  public function shouldAppearInApplicationTransactions() {
    return true;
  }

  public function shouldAppearInEditView() {
    return true;
  }

  public function shouldAppearInPropertyView() {
    return true;
  }  

  public function readValueFromRequest(AphrontRequest $request) {
    $this->value = $request->getStr($this->getFieldKey());
  }

  public function getValueForStorage() {
    return $this->getValue();
  }    

  public function renderEditControl(array $handles) {
    return id(new AphrontFormTextControl())
      ->setUser($this->getViewer())
      ->setName($this->getFieldKey())
      ->setValue($this->getValue())
      ->setLabel($this->getFieldName());
  }

  public function renderPropertyViewValue(array $handles) {
    $overload = $this->getValue();        
    if (!strlen($overload)) {
      return null;
    }

    $viewer = $this->getViewer();
    $view = new PHUIRemarkupView($viewer, $overload);    

    return $view;
  }
}

I think, the main problem is in readValueFromObject method. How can I get the value from given $object?
And which method should I use for value validation?

Thanks for answers