/** * Constructor * * @param FacilityService $facilityService * @throws InvalidArgumentException * * */ public function __construct( FacilityService $facilityService ) { $this->facilityService = $facilityService; $this->add( [ 'name' => 'name', 'filters' => [ [ 'name' => 'StripTags' ], [ 'name' => 'StringTrim' ], ], ] ); $this->facilityValidator = new Callback(function ($value) { if ($value instanceof Facility) { return true; } else if (!(is_array($value) && isset($value['id']))) { $this->facilityValidator->setMessage('No valid identifier specified'); return false; } try { $this->facilityService->find($value); return true; } catch (ModelNotFoundException $e) { $this->facilityValidator->setMessage($e->getMessage()); } catch (UnauthorizedException $e) { $this->facilityValidator->setMessage($e->getMessage()); } catch (Exception $e) { $this->facilityValidator->setMessage('Unknown error during validation'); } return false; }); $this->add( [ 'name' => 'facility', 'validators' => [ $this->facilityValidator ] ] ); }
/** * Create the Callback validator that will be used to ensure the * RowCollectionEditor is valid. We iterate over all the editors it has, * pass data to the editor via isValid() and return an error message if any * editor fails. * * @return Callback */ protected function createCallbackValidator() { $validator = new Callback(); $validator->setCallback(function () { return $this->rowCollectionEditor->isValid(); }); $validator->setMessage("Some errors were found in your {$this->rowCollectionEditor->getPluralTitle()}.\n Please double-check and try again.", Callback::INVALID_VALUE); return $validator; }
public function __construct(EntityManager $objectManager, $type) { // Firstname $firstname = new Input('prenom'); $firstname->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $firstname->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 50))); $this->add($firstname); // Lastname $lastname = new Input('nom'); $lastname->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $lastname->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 50))); $this->add($lastname); //Unique email validator $uniqueUserEmail = new UniqueObject(array('object_manager' => $objectManager, 'object_repository' => $objectManager->getRepository('Application\\Entity\\Utilisateur'), 'fields' => 'email', 'use_context' => true)); $uniqueUserEmail->setMessage('L\'email renseigné est déjà utilisé par un autre utilisateur.', 'objectNotUnique'); // Email $email = new Input('email'); $email->getFilterChain()->attach(new StringTrim())->attach(new StripTags()); $email->getValidatorChain()->attach(new NotEmpty())->attach(new StringLength(array('max' => 255)))->attach(new EmailAddress())->attach($uniqueUserEmail); $this->add($email); $editCallback = new Callback(function ($value) { if ($value == '') { return true; } /** @var StringLength $validator */ $validator = new StringLength(array('min' => 6, 'max' => 32)); return $validator->isValid($value); }); $editCallback->setMessage('Le mot de passe doit contenir entre 6 et 32 caractères.'); // Password $password = new Input('password'); $password->setRequired($type == UtilisateurForm::TYPE_ADD)->setContinueIfEmpty(true); $password->getFilterChain()->attach(new StringTrim()); $password->getValidatorChain()->attach($type == UtilisateurForm::TYPE_ADD ? new StringLength(array('min' => 6, 'max' => 32)) : $editCallback); $this->add($password); // Password Confirmation $passwordConfirmation = new Input('passwordConfirmation'); $passwordConfirmation->setRequired($type == UtilisateurForm::TYPE_ADD)->setContinueIfEmpty(true); $passwordConfirmation->getFilterChain()->attach(new StringTrim()); $passwordConfirmation->getValidatorChain()->attach($type == UtilisateurForm::TYPE_ADD ? new StringLength(array('min' => 6, 'max' => 32)) : $editCallback)->attach(new Identical('password')); $this->add($passwordConfirmation); $role = new Input('role'); $role->setRequired(false); $role->getFilterChain()->attach(new Digits()); $this->add($role); }
/** * Create any resources that need to be accessible both for processing * and rendering. * * @return void */ public function init() { $this->model = Pimple::getResource('users-gateway'); $this->fields = new Fields(); $this->row = $this->model->find($this->request->getQuery('user_id')); $this->fields->add('password')->setLabel('Password')->setEditable(true)->add('confirm_password')->setLabel('Confirm Password')->setEditable(true); $this->inputFilter = new InputFilter(); $password = new Input('password'); $this->inputFilter->add($password); $password->setRequired(true)->getValidatorChain()->attach(new StringLength(array('min' => 6))); $confirm = new Input('confirm_password'); $this->inputFilter->add($confirm); $validator = new Callback(array('callback' => function ($value) { return $value === $this->request->getPost('password'); })); $validator->setMessage('Passwords do not match.'); $confirm->setRequired(true)->getValidatorChain()->attach($validator); }
/** * @param callable $callback * @param string $message * @return CallbackValidator */ public static function callbackValidator(callable $callback, $message = null) { $validator = new CallbackValidator(compact('callback')); if ($message) { $validator->setMessage($message); } else { $validator->setMessage('Input tidak valid.'); } return $validator; }
/** * Add password and confirmation fields to the given fields collection * * @param Fields $fields * @return Component */ protected function addPasswordFields(Fields $fields) { static $passwordFieldName = 'password', $confirmFieldName = 'confirm_password', $minimumLength = 6; $request = $this->getRequest(); $fields->add($passwordFieldName)->assignHelperCallback('EditControl.Control', function ($helper, View $view) use($passwordFieldName, $request) { return $view->inputText(['name' => $passwordFieldName, 'type' => 'password', 'value' => $request->getPost($passwordFieldName)]); })->assignHelperCallback('InputFilter', function () use($passwordFieldName, $minimumLength) { $input = new Input($passwordFieldName); $input->setRequired(true)->getValidatorChain()->attach(new StringLength(['min' => $minimumLength])); return $input; })->setLabel('Password')->setEditable(true); $this->fields->add('confirm_password')->assignHelperCallback('EditControl.Control', function ($helper, View $view) use($confirmFieldName, $request) { return $view->inputText(['name' => $confirmFieldName, 'type' => 'password', 'value' => $request->getPost($confirmFieldName)]); })->assignHelperCallback('InputFilter', function () use($request, $passwordFieldName, $confirmFieldName) { $input = new Input($confirmFieldName); $passwordsMatchValidator = new Callback(['callback' => function ($value) use($request, $passwordFieldName) { return $value === $request->getPost($passwordFieldName); }]); $passwordsMatchValidator->setMessage('Passwords do not match.'); $input->setRequired(true)->getValidatorChain()->attach($passwordsMatchValidator); return $input; })->setLabel('Confirm Password')->setEditable(true); return $this; }