public function testSpecifyingMessagesToInputReturnsThoseOnFailedValidation() { $this->input->setValue('bar'); $validator = new Validator\Digits(); $this->input->getValidatorChain()->attach($validator); $this->input->setErrorMessage('Please enter only digits'); $this->assertFalse($this->input->isValid()); $messages = $this->input->getMessages(); $this->assertArrayNotHasKey(Validator\Digits::NOT_DIGITS, $messages); $this->assertContains('Please enter only digits', $messages); }
public function buildFilter() { /****************************************************************** * Isto é um exemplo de como realizar a configurações do filtro, * O importante é registrar o campos do formulário pedidos no exercícios **********************************************************************/ // filter & validate by fields $category = new Input('category'); $category->getFilterChain()->attachByName('StringTrim')->attachByName('StripTags')->attachByName('StringToLower'); $category->getValidatorChain()->attachByName('InArray', array('haystack' => $this->getCategories())); $title = new Input('title'); $title->getFilterChain()->attachByName('StringTrim')->attachByName('StripTags'); $titleRegex = new Regex(array('pattern' => '/^[a-zA-Z0-9 ]*$/')); $titleRegex->setMessage('Title should only contain numbers, letters or spaces!'); $title->getValidatorChain()->attach($titleRegex)->attachByName('StringLength', array('min' => 1, 'max' => 128)); $photo = new Input('photo_filename'); $photo->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $photo->getValidatorChain()->attachByName('Regex', array('pattern' => '!^(http://)?[a-z0-9./_-]+(jp(e)?g|png)$!i')); $photo->setErrorMessage('Photo must be a URL or a valid filename ending with jpg or png'); $price = new Input('price'); $price->setAllowEmpty(TRUE); $price->getValidatorChain()->addByName('GreaterThan', array('min' => 0.0)); $price->getFilterChain()->attach(new Float()); // custom filter $expires = new Input('expires'); $expires->setAllowEmpty(TRUE); $expires->getValidatorChain()->attachByName('InArray', array('haystack' => array_keys($this->getExpireDays()))); $expires->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $city = new Input('cityCode'); $city->setAllowEmpty(TRUE); $city->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $name = new Input('contact_name'); $name->setAllowEmpty(TRUE); $name->getValidatorChain()->attachByName('Regex', array('pattern' => '/^[a-z0-9., -]{1,255}$/i')); $name->setErrorMessage('Name should only contain letters, numbers, and some punctuation.'); $name->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $phone = new Input('contact_phone'); $phone->setAllowEmpty(TRUE); $phone->getValidatorChain()->attachByName('Regex', array('pattern' => '/^\\+?\\d{1,4}(-\\d{3,4})+$/')); $phone->setErrorMessage('Phone number must be in this format: +nnnn-nnn-nnn-nnnn'); $phone->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $email = new Input('contact_email'); $email->setAllowEmpty(TRUE); $email->getValidatorChain()->attachByName('EmailAddress'); $email->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $description = new Input('description'); $description->setAllowEmpty(TRUE); $description->getValidatorChain()->attachByName('StringLength', array('min' => 1, 'max' => 4096)); $description->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $delCode = new Input('delete_code'); $delCode->setRequired(TRUE); $delCode->getValidatorChain()->addByName('Digits'); $this->add($category)->add($title)->add($photo)->add($price)->add($expires)->add($city)->add($name)->add($phone)->add($email)->add($description)->add($delCode); }
public function getInputFilter() { $inputFilter = new InputFilter(); $username = new Input(); $username->setName('username')->setRequired(true); $username->setErrorMessage('A Username is required to Login'); $inputFilter->add($username); $password = new Input(); $password->setName('password')->setRequired(true); $password->setErrorMessage('A Password is required to Login'); $inputFilter->add($password); return $inputFilter; }
public function buildFilter() { $category = new Input('category'); $category->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim')->attachByName('StringToLower'); $category->getValidatorChain()->attachByName('InArray', array('haystack' => $this->getCategories())); $title = new Input('title'); $title->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $titleRegex = new Regex(array('pattern' => '/^[a-zA-Z0-9 ]*$/')); $title->getValidatorChain()->attach($titleRegex)->attachByName('StringLength', array('min' => 1, 'max' => 128)); $photo = new Input('photo_filename'); $photo->setRequired(FALSE); $photo->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $photo->getValidatorChain()->attachByName('Regex', array('pattern' => '!^(http(s)?://)?[a-z0-9./_-]+(jp(e)?g|png)$!i')); $photo->setErrorMessage('Photo must be a URL or a valid filename ending with jpg or png'); $name = new Input('contact_name'); $name->setAllowEmpty(TRUE); $name->getValidatorChain()->attachByName('Regex', array('pattern' => '/^[a-z0-9. -]{1,255}$/i')); $name->setErrorMessage('Name should only contain letters, numbers, spaces, . or -.'); $name->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $email = new Input('contact_email'); $email->setAllowEmpty(TRUE); $email->getValidatorChain()->attachByName('EmailAddress'); $email->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $phone = new Input('contact_phone'); $phone->setAllowEmpty(TRUE); $phone->getValidatorChain()->attachByName('Regex', array('pattern' => '/^\\+?\\d{1,4}(-\\d{3,4})+$/')); $phone->setErrorMessage('Phone number must be in the format +nnnn-nnn-nnn-nnnn'); $phone->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $city = new Input('city'); $city->setAllowEmpty(TRUE); $city->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $city->getValidatorChain()->attachByName('InArray', array('haystack' => PostForm::$cityCodes)); $price = new Input('price'); $price->setAllowEmpty(TRUE); $price->getValidatorChain()->attachByName('GreaterThan', array('min' => 0.0)); $price->getFilterChain()->attach(new Float()); $expires = new Input('expires'); $expires->setAllowEmpty(TRUE); $expires->getValidatorChain()->attachByName('InArray', array('haystack' => array_keys($this->getExpireDays()))); $expires->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $deleteCode = new Input('delete_code'); $deleteCode->setRequired(TRUE); $deleteCode->getValidatorChain()->attachByName('Digits'); $description = new Input('description'); $description->setAllowEmpty(TRUE); $description->getValidatorChain()->attachByName('StringLength', array('min' => 1, 'max' => 4096)); $description->getFilterChain()->attachByName('StripTags')->attachByName('StringTrim'); $this->add($category)->add($title)->add($photo)->add($name)->add($email)->add($phone)->add($city)->add($price)->add($expires)->add($deleteCode)->add($description); }
public function buildFilter() { $category = new Input('category'); $category->getFilterChain()->attach(new StripTags())->attach(new StringTrim())->attach(new StringToLower()); $category->getValidatorChain()->attach(new InArray(array('haystack' => $this->categories))); $title = new Input('title'); $title->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $title->getValidatorChain()->attachByName('Alnum', array('allowWhiteSpace' => TRUE))->attachByName('StringLength', array('options' => array('min' => 6, 'max' => 128))); $price = new Input('price'); $price->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $priceRegex = new \Zend\Validator\Regex(array('pattern' => '/^[0-9]{1,10}+(\\.[0-9]{1,2})?$/')); $priceRegex->setMessage('Preço deve ter no máximo 12 caracteres, incluindo opcionalmente 2 casas decimais separadas por ".".'); $price->getValidatorChain()->attach($priceRegex); $dateExpires = new Input('date_expires'); $dateExpires->setAllowEmpty(TRUE); $dateExpires->getValidatorChain()->attach(new InArray(array('haystack' => $this->dateExpires))); $description = new Input('description'); $description->setAllowEmpty(TRUE); $description->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $description->getValidatorChain()->attachByName('StringLength', array('options' => array('max' => 4096))); $photoFilename = new Input('photo_filename'); $photoFilename->setAllowEmpty(TRUE); $photoFilename->getValidatorChain()->attachByName('Regex', array('pattern' => '!^(http://)?[a-z0-9./_-]+(jp(e)?g|png)$!i')); $photoFilename->setErrorMessage('Photo must be a URL or a valid filename ending with jpg or png'); $contactName = new Input('contact_name'); $contactName->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $contactName->getValidatorChain()->attachByName('Regex', array('pattern' => '/^[a-z0-9., -]{1,255}$/i')); $contactName->setErrorMessage('Name should only contain letters, numbers, and some punctuation.'); $contactEmail = new Input('contact_email'); $contactEmail->setAllowEmpty(TRUE); $contactEmail->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $contactEmail->getValidatorChain()->attachByName('EmailAddress'); $contactPhone = new Input('contact_phone'); $contactPhone->setAllowEmpty(TRUE); $contactPhone->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $contactPhone->getValidatorChain()->attachByName('Regex', array('pattern' => '/^(\\([0-9]{2}\\))\\s([9]{1})?([0-9]{4})-([0-9]{4})$/')); $contactPhone->setErrorMessage('Contato deve ter o seguinte formato:(99) 9999-9999 ou (99) 99999-9999'); $cityCode = new Input('cityCode'); $cityCode->setAllowEmpty(TRUE); $cityCode->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $cityCode->getValidatorChain()->attach(new InArray(array('haystack' => array_keys($this->cities)))); $deleteCode = new Input('delete_code'); $deleteCode->setAllowEmpty(TRUE); $deleteCode->getFilterChain()->attach(new StripTags())->attach(new StringTrim()); $deleteCode->getValidatorChain()->attachByName('Alnum', array('allowWhiteSpace' => TRUE)); $this->add($category)->add($title)->add($price)->add($dateExpires)->add($description)->add($photoFilename)->add($contactName)->add($contactEmail)->add($contactPhone)->add($cityCode)->add($deleteCode); }