Closed jroedel closed 4 years ago
@jroedel ,
If you are looking at having extra validation on the credential
input for the Register form, there are a couple of ways to achieve this.
You can "decorate" the Register form using a Delegate Factory on the form. LmcUser
uses a factory to create the Register form from the lmcuser_register_form
alias ($container->get('lmcuser_register_form')
. By adding a Delegate Factory, you can add inputs to the form, filtering, validation, etc.
Here's how:
in a module.config.php
file somewhere, add the following:
'service_manager' => [
'delegators' => [
'lmcuser_register_form' => [\MyRegisterFormDelegateFactory::class],
]
],
and then define the MyRegisterFormDelegateFactory
class that will decorate the form, like this:
class MyRegisterFormDelegateFactory implements DelegatorFactoryInterface
{
/**
* This delegate adds more elements to the form
* @inheritDoc
* @see \Laminas\ServiceManager\Factory\DelegatorFactoryInterface::__invoke()
*
*/
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
{
$form = call_user_func($callback);
// Set filters and validators
$inputFilter = $form->getInputFilter();
$inputFilter->add([
'name' => 'credential',
'required' => true,
'filters' => [
new StripTags(),
new StringTrim(),
],
'validators' => [
new \Laminas\Validator\Regex([
'pattern' => 'yourregexpattern',
]),
],
]);
return $form;
}
}
Then, when the LmcUser Controller processes the Register action, it will call $form->isValid()
and reject the register data if the Regex validation fails.
The other method would be to add Javascript code to you register.phtml
template to do the validation locally before the form is submitted. You can override the LmcUser register.phtml
using the method described in the Wiki. I have used a combination of both methods in my application to add more input fields to the form and to add JS validation before the form is submitted.
LmcUser
is very flexible where you can override many of the default logic. We need to better document all these techniques in the Wiki.
I hope this is what you are looking for.
Thanks a lot @visto9259 , that decorator setup is great, I've never used them before. That will work for me!
thanks @visto9259
It would excellent to be able to restrict the username field. Currently it can be literally any characters as long as it's between 3 and 255 characters long.
Thanks