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

Add check length function to model class #536

Closed
wants to merge 3 commits into from

Conversation

JohnyFW
Copy link
Contributor

@JohnyFW JohnyFW commented Jun 25, 2018

Added function to check the length of a varchar field.
function that simplifies the test method of the models

@NeoRazorX
Copy link
Owner

ModelClass is so big, and this function seems unhelpful in this case.

@JohnyFW
Copy link
Contributor Author

JohnyFW commented Jun 26, 2018

ok

@JohnyFW JohnyFW closed this Jun 26, 2018
@shawe
Copy link
Contributor

shawe commented Jun 26, 2018

@JohnyFW I added this: https://github.com/FacturaScripts/webportal/blob/master/Lib/WebPortal/GeoLocation.php#L99 that seems to be in same line than yours, I added first to Contact model, but I receive indications to move it to this controller. To me this type of methods must be a model responsability (specific or generic, but in a model class).

Seems that I'm not the one interested or with the need to check if field length is valid. Maybe as Carlos say, ModelClass is not the best place for now because is actually very big, but I think that methods like this can be useful for all of us to force some common checks.

With API in mind, that only have a basic tests (null checks from ModelClass) and some other specific fields from each model, but not all fields strictly validated.

Maybe something like this can be more generic an usefull for all of us:

/**
 * Returns if $field has a valid length based on model field definition.
 *
 * @return bool
 */
private function hasValidFieldLength($field): bool
{
    if (\property_exists(\get_class($this), $field)) {
        $size = (int) preg_replace('/[^0-9]/', '', $this->getModelFields()[$field]['type']);
        return \mb_strlen($this->{$field}) <= $size;
    }
    return false;
}

NOTE: I write this method directly here and maybe it has any error.

@rsanjoseo
Copy link
Contributor

I would check the length of both text and numeric field in the model.

If the information is defined in the XML file, the generic test ) could already perform the verification.

As I mentioned in Tenerife, I would ALSO add the check in the html code to filter from the browser (if available).

@JohnyFW
Copy link
Contributor Author

JohnyFW commented Jun 26, 2018

The objective is to simplify the final models, especially the test before recording. for example:

$error = $this->errorInStrLen($this->nombre, 'name', 100, 1) 
      || $this->errorInStrLen($this->cifnif, 'cifnif', 30, 1) 
      || $this->errorInStrLen($this->ciudad, 'city', 100, 0);

return !$error && parent::test();

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