-
-
Save ojenikoh/685502cb82a11cda578b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$form = new Zend_Form(); | |
$form->setElements(array( | |
new Zend_Form_Element_Text('colour', array( | |
'required' => true, | |
'label' => 'Please enter acceptable colours (blue, green or red)', | |
'validators' => array( | |
array('NotEmpty', true, array( | |
'messages' => 'A colour is required' | |
)), | |
array('InArray', true, array( | |
'messages' => 'Colour is uncceptable', | |
'haystack' => array('blue', 'green', 'red') | |
)) | |
) | |
)), | |
new Zend_Form_Element_Text('someOtherInput', array( | |
'required' => true, | |
'label' => 'Please enter acceptable words (thank, you)', | |
'validators' => array( | |
array('NotEmpty', true, array( | |
'messages' => 'A word is required' | |
)), | |
array('InArray', true, array( | |
'messages' => 'Word is uncceptable', | |
'haystack' => array('thank', 'you') | |
)) | |
) | |
)) | |
)); | |
$prompt = new Cli_Prompt($form); | |
$prompt->prompt(); | |
//now we may do other stuff as we are guaranteed to have valid input |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Cli_Prompt | |
{ | |
/** | |
* @var Zend_Form | |
*/ | |
protected $_form = null; | |
/** | |
* Prompt constructor | |
* | |
* @param Zend_Form $form | |
*/ | |
public function __construct(Zend_Form $form) { | |
if (php_sapi_name() !== 'cli') { | |
throw new RuntimeException('Class can only be used from the command line'); | |
} | |
$this->_form = $form; | |
} | |
/** | |
* Prompts the user for input and validates the input | |
* | |
* @return Zend_Form | |
*/ | |
public function prompt() | |
{ | |
$elements = $this->_form->getElements(); | |
$errorFilter = new Filter_FormErrors(); | |
$colour = new Cli_Colour(); | |
foreach ($elements as $element) | |
{ | |
$firstRun = true; | |
$element instanceof Zend_Form_Element; | |
while (!$element->isValid($element->getValue())) { | |
$label = ''; | |
if (!$firstRun) { | |
$label = $element->getLabel() . | |
' (Error: ' . $colour->getColoredString( | |
$errorFilter->filter($element->getMessages()) , 'red', 'light_gray') . | |
'): '; | |
} else { | |
$label = $element->getLabel() . ': '; | |
$firstRun = false; | |
} | |
$input = readline($label); | |
$element->setValue($input); | |
} | |
} | |
return $this->_form; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Filter_FormErrors implements Zend_Filter_Interface | |
{ | |
/** | |
* Filter method | |
* | |
* @param array $errors | |
* @return string | |
*/ | |
public function filter($errors) { | |
if (!is_array($errors)) { | |
$errors = (array)$errors; | |
} | |
$flatten = array(); | |
array_walk_recursive($errors, function($value, $key) use (&$flatten) { | |
$flatten[] = $value; | |
}); | |
$message = implode(', ', $flatten); | |
return $message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment