Greetings,

I'm fairly new to PHP, and I am trying to validate a CCK text-area field with a minimum string length.

Any insight into the following (on creating a new validator to test for minimum string length) would be very much appreciated.

1. I first create (add) the new minLength validator in admin/build/validation_api/validators/add
2. What should be the rule if I want to test for a minimum strlen of 75 characters?
3. If the rule requires arguments, what are the arguments?

Thank you in advance!

Scott

Comments

scott859’s picture

Greetings again,

I solved this using the following RegEX (instead of PHP):

Rule: /^.{N}.*/

Change the "N" above to represent the minimum number of characters.

So, if you want to validate for a minimum of 150 characters the rule would be as follows:

/^.{150}.*/

Then you would create the message:

%field needs to be at least 150 characters. (or whatever you wish to use as your message)

Hope this helps those that are facing a similar question.

Scott

TapocoL’s picture

Status: Active » Fixed

Scott,

Well the one thing I will recommend with the validator is to use arguments. For example, if you need another validation script to check for a minimum length of 5 characters, then you would have to create a new validator just for that. So, change the form fields in the validator to this:

rule: /^.{%arguments[0]}$/
message: %field needs to be at least %arguments[0] characters.
And add a new argument with these values:
name: Minimum Length
description: Enter the minimum length of this field.

Then, when you are attaching the validator to a field, you input what you want that field's minimum length to be. Validation API will replace %arguments[0] with whatever you entered in that field (Note: PHP rules use $arguments[0]).

You can see an example of a minimum and maximum length validator by importing the included one with this project at admin/build/validation_api/validators/import

scott859’s picture

Hi TapocoL,

Thanks so much for the information. It makes sense to me, but for some reason it isn't working.

Here's what I am doing:

(1) I created the RegEX validator as you described in your post (post#2)

(2) I selected the link in the field that I wanted to validate - in this case it is a text area and the link options are:
a. Add a validator to field_description[%]
b. The other option is Add a validator to field_description[0]

Not completely understanding the difference, I initially selected (b) and when that didn't work then I started over (after deleting the validator and field connection to it) and selected (b). I experienced the same issue (indicated below) with both options.

(3) I then selected the validator that I created in (1) above and then applied a 150 minimum character validation to it. I did this by inputing "150" (without quotes) in the "Mininmum Length" argument text input field.

(4) When I go to test the validator in an existing node by editing the node and removing characters from the "field_description" field so that it is under 150 characters I get the proper message that the field is under 150.

(5) When I add the characters back into the "field_description" field so as to make it over 150 characters, then hit save - I STILL get the warning that it is under 150 characters - and it doesn't go to the "view" state - it stays in the edit state.

So, not sure what I am doing wrong here, any insight would be appreciated!

Thanks,

Scott

scott859’s picture

Greetings,

One thing I should indicate is that the test field that I am applying the validator to "field_description" has over 200 characters in it. When I select to edit the node but do nothing to it and just hit save - this warning message appears:

"field_description[0] needs to be at least 150 characters."

For some reason, the rule is not correctly interpreting the amount of characters in the field.

Thanks again,

Scott

TapocoL’s picture

Well, this sounds like a problem with the RegEx. And now that I look closer, I did not include a comma inside the limited repetition. So, I was just testing if the field was exactly the given argument. So if you put exactly 150 characters in the field, it would pass. Change the rule to display a comma right after the %arguments[0]:

/^.{%arguments[0],}$/

I believe that should work now.

scott859’s picture

Hi TapocoL,

Yes, that was it - thanks SO much for your help! I know we're all very busy and I really appreciate you taking the time to help on this.

Everything is working great - thanks again.

Scott

TapocoL’s picture

Status: Fixed » Closed (fixed)

np. Glad to help.

Craig

blueblade’s picture

still a little confused...