Hello.
I faced some scenario that needs a lot of spare coding.
Imagine you have a signup formhaving 3 mandatory fields:
-
username
-
password
-
password_repeat
However, customer says he may optionally need the following:
-
Full name
-
Agreement checkbox
-
Captcha
My question is: what is the best practice to achieve this in
less code? It’s obvious this should be bound to a model. Most
likely, a behavior.
Common functionality for $useCaptcha = true; controller’s property
would be:
-
prevent validation, see CModel::validate($attributes)
-
prevent attribute assignment even if it’s safe
-
prevent form field rendering in a view
This is what I have in a code right now. It’s not clean and is spread
over controller / views (this is actually a controller’s action):
// Populate a list of attributes to validate.
$attributes = array_flip($form->getSafeAttributeNames());
// ... and exclude hidden attributes.
if (!$this->useName) unset($attributes['name'], $data['name']);
if (!$this->useAgreement) unset($attributes['agreement'], $data['agreement']);
if (!$this->useCaptcha) unset($attributes['captcha'], $data['captcha']);
// Set form data.
$form->setAttributes($data);
if ($form->validate(array_keys($attributes)) {...}
Thanks in advance for your feedback!