Liip\RMT\Information\InteractiveQuestion::validate PHP Method

validate() public method

public validate ( $value )
    public function validate($value)
    {
        // Replace potential shortcuts
        if (count($shortcuts = $this->informationRequest->getOption('choices_shortcuts')) > 0) {
            if (in_array($value, array_keys($shortcuts))) {
                $value = $shortcuts[$value];
            } else {
                throw new \Exception('Please select a value in ' . json_encode(array_keys($shortcuts)));
            }
        }
        // Validation
        return $this->informationRequest->validate($value);
    }

Usage Example

 public function testValidateChoicesWithShortcuts()
 {
     $ir = new InformationRequest('fruit', array('type' => 'choice', 'choices' => array('apple', 'banana', 'cherry'), 'choices_shortcuts' => array('a' => 'apple', 'b' => 'banana', 'c' => 'cherry')));
     $iq = new InteractiveQuestion($ir);
     $this->assertEquals('apple', $iq->validate('a'));
 }