Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation between fields #15

Open
spapas opened this issue Oct 14, 2024 · 5 comments
Open

Validation between fields #15

spapas opened this issue Oct 14, 2024 · 5 comments

Comments

@spapas
Copy link

spapas commented Oct 14, 2024

I want to implement a form similar to this:

(cl-forms:defform register-form (:action "/register/" :method :post)
                  ((username :string :value "" :constraints (list (clavier:is-a-string)
                                                                  (clavier:not-blank)))
                   (password :password :value "")
                   (password-verify :password :value "")
                   (submit :submit :label "Register")))

The fields password and password-verify must match. Is there a way to validate these (forms::validate-form form) ? If not how can I add the extra validation messages for these?

TIA

@mmontone
Copy link
Owner

Hi. I have only implemented validation per field, although I think I should also have implemented per whole form, so that validation between fields can be achieved. I think in this case you will have to provide validation for the form yourself.
Like a function validate-my-form that calls forms:validate-form, but then also checks the password field values. Let me know if you need help with this, I can try an implementation. Or if you have ideas on how to extend form validation to the whole form. I will think about it.

@mmontone
Copy link
Owner

mmontone commented Oct 14, 2024

If not how can I add the extra validation messages for these?

To add your own validation message follow forms:validate-form implementation in your custom validation function:
"Validates a form. Usually called after HANDLE-REQUEST. Returns multiple values; first value is true if the form is valid; second value a list of errors.
The list of errors is an association list with elements (<field> . field errors strings list>)."

@spapas
Copy link
Author

spapas commented Oct 14, 2024

Ah ok I thought that maybe there was a way to actually include validation among multiple fields.

For now I have implemented it using a function that verifies the whole form and either returns nil if everything's cool or the form with the errors if there are problems:

(defun register-form-errors (form)
  (if (forms::validate-form form)
      (forms::with-form-field-values (username password password-verify) form
        (cond ((not (equal password password-verify))
                (forms:add-form-error 'password-verify "Passwords do not match" form))
              ((equal username "root")
                (forms:add-form-error 'username "Wrong username" form))
              (t nil)))
      form))

To implement the validation among fields you could add an optional parameter with that function in the form definition, for example:

(cl-forms:defform register-form (:action "/register/" :method :post :validation #'register-validate) where the register-validate would receive the form instance and modify the form if there are errors (using forms:add-form-error f.e).

@spapas
Copy link
Author

spapas commented Oct 14, 2024

Also we could add a generic form-validation function that a form that wants such validation could override.

@mmontone
Copy link
Owner

Ah. I forgot about add-form-error. That's good use. I will think about an implementation for custom form validations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants