1 2/* 3 +------------------------------------------------------------------------+ 4 | Phalcon Framework | 5 +------------------------------------------------------------------------+ 6 | Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) | 7 +------------------------------------------------------------------------+ 8 | This source file is subject to the New BSD License that is bundled | 9 | with this package in the file LICENSE.txt. | 10 | | 11 | If you did not receive a copy of the license and are unable to | 12 | obtain it through the world-wide-web, please send an email | 13 | to license@phalconphp.com so we can send you a copy immediately. | 14 +------------------------------------------------------------------------+ 15 | Authors: Andres Gutierrez <andres@phalconphp.com> | 16 | Eduar Carvajal <eduar@phalconphp.com> | 17 +------------------------------------------------------------------------+ 18 */ 19 20namespace Phalcon\Validation\Validator; 21 22use Phalcon\Validation; 23use Phalcon\Validation\Message; 24use Phalcon\Validation\Validator; 25 26/** 27 * Phalcon\Validation\Validator\PresenceOf 28 * 29 * Validates that a value is not null or empty string 30 * 31 * <code> 32 * use Phalcon\Validation; 33 * use Phalcon\Validation\Validator\PresenceOf; 34 * 35 * $validator = new Validation(); 36 * 37 * $validator->add( 38 * "name", 39 * new PresenceOf( 40 * [ 41 * "message" => "The name is required", 42 * ] 43 * ) 44 * ); 45 * 46 * $validator->add( 47 * [ 48 * "name", 49 * "email", 50 * ], 51 * new PresenceOf( 52 * [ 53 * "message" => [ 54 * "name" => "The name is required", 55 * "email" => "The email is required", 56 * ], 57 * ] 58 * ) 59 * ); 60 * </code> 61 */ 62class PresenceOf extends Validator 63{ 64 65 /** 66 * Executes the validation 67 */ 68 public function validate(<Validation> validation, string! field) -> boolean 69 { 70 var value, message, label, replacePairs, code; 71 72 let value = validation->getValue(field); 73 if value === null || value === "" { 74 let label = this->prepareLabel(validation, field), 75 message = this->prepareMessage(validation, field, "PresenceOf"), 76 code = this->prepareCode(field); 77 78 let replacePairs = [":field": label]; 79 80 validation->appendMessage( 81 new Message( 82 strtr(message, replacePairs), 83 field, 84 "PresenceOf", 85 code 86 ) 87 ); 88 89 return false; 90 } 91 92 return true; 93 } 94} 95