Showing posts with label validation. Show all posts
Showing posts with label validation. Show all posts

Saturday, February 11, 2017

Pass old input/ get old input from view after Form Request validation in laravel

Sometimes we need to get old values in form to show them when error back to the same form, It is now well organized in Laravel. Consider the following situations:

1. Suppose you submitted a form with some values.
2. Found some errors and send back to the form again from Laravel controller to show the form again.
3. You have to show the values when he was here few moments ago (before submit the form)

$username = Request::old('username');

or in view:

{{ old('username') }}

Tuesday, December 13, 2016

Add remove required validation in angular dynamically in angular js

<!doctype html>
<html>
<head>
    <title>Add remove required validation in angular dynamically in angular js</title>
    <script src="http://code.angularjs.org/1.2.12/angular.js"></script>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
</head>
<body>

<div data-ng-app="myApp" data-ng-controller="MyController">
    <form name="myForm" method="get" validate-form novalidate>
        <div class="container">
            Type:
            <select ng-model="type" my-validation>
                <option value="required">Required</option>
                <option value="not_required">Not Required</option>
            </select>
        </div>
        <div class="container">
            Name:
            <input type="text" name="name" ng-model="name" ng-required="isFieldRequired()" my-validation/>
            <span ng-show="myForm.name.$invalid" style="color:red;font-size:30px;">*</span>
        </div>
        <div><input type="submit" value="Submit"/></div>
    </form>
</div>

<script type="text/javascript">
    var app = angular.module("myApp", []);

    app.controller("MyController", function($scope) {
        $scope.type = "required";
        $scope.isFieldRequired = function() {
            return $scope.type == "required";
        }
    });

    app.directive('validateForm', function () {
        return {
            restrict: 'A',
            link: function ($scope, form) {
                form.on('submit', function () {
                    $(form).find(".container").css("border", "");
                    if($scope.myForm.$invalid) {
                        var error = $scope.myForm.$error;
                         for (var key in $scope.myForm.$error) {
                             for (var index = 0; index < $scope.myForm.$error[key].length; index++) {
                                 var err = $scope.myForm.$error[key][index];
                                 console.log("Field=" + err.$name + ', Error=' + key);
                             }
                         }


                        var firstInvalid = $(form[0].querySelector('.ng-invalid'));

                        if (firstInvalid.length > 0) {
                            firstInvalid.closest(".container").addClass("error_element").css("border", "2px solid red");
                            firstInvalid.focus();
                            return;
                        }
                    }
                    console.log("Form Validated");
                });
            }
        };
    });

    app.directive("myValidation", function() {
        return {
            restrict: "A",
            require: "ngModel",
            link: function(scope, elem, attrs, ctrl) {
                console.log(attrs)
            }
        }
    })
</script>

</body>
</html>

Wednesday, October 2, 2013

Grails validate email address

you can use

import org.apache.commons.validator.EmailValidator
...
EmailValidator emailValidator = EmailValidator.getInstance()
if (emailValidator.isValid(valueToTest)) ....

the apache commons validators are included with Grails so you don't need to install another plugin.

Monday, September 30, 2013

[Java/Grails] Class ConstrainedProperty

org.codehaus.groovy.grails.validation

[Java/Grails] Class ConstrainedProperty

java.lang.Object
  org.codehaus.groovy.grails.validation.ConstrainedProperty 
 


Method Summary
void addMetaConstraint(java.lang.String name, java.lang.Object value)
Obtains the value of the named meta constraint.
void applyConstraint(java.lang.String constraintName, java.lang.Object constrainingValue)
Applies a constraint for the specified name and consraint value.
Constraint getAppliedConstraint(java.lang.String name)
@param constraintName The name of the constraint to check
java.util.Collection getAppliedConstraints()
Obtains an applied constraint by name.
java.util.Map getAttributes()
java.lang.String getFormat()
java.util.List getInList()
@return Returns the inList.
java.lang.String getMatches()
@return Returns the matches.
java.lang.Comparable getMax()
java.lang.Integer getMaxSize()
java.lang.Object getMetaConstraintValue(java.lang.String name)
java.lang.Comparable getMin()
@return Returns the min.
java.lang.Integer getMinSize()
@return Returns the minSize.
java.lang.Object getNotEqual()
@return Returns the notEqual.
int getOrder()
@param order The order to set.
java.lang.String getPropertyName()
@return Returns the propertyName.
java.lang.Class getPropertyType()
@return Returns the max.
groovy.lang.Range getRange()
@return Returns the range.
java.lang.Integer getScale()
@return The scale, if defined for this property; null, otherwise
groovy.lang.Range getSize()
@param size The size to set.
java.lang.String getWidget()
boolean hasAppliedConstraint(java.lang.String constraintName)
@return Returns the propertyType.
static boolean hasRegisteredConstraint(java.lang.String constraintName)
@return Returns the appliedConstraints.
boolean isBlank()
@return the blank.
boolean isCreditCard()
boolean isDisplay()
@return Returns the display.
boolean isEditable()
@param editable The editable to set.
boolean isEmail()
@return Returns the email.
boolean isNullable()
@return Returns the nullable.
boolean isPassword()
boolean isUrl()
static void registerNewConstraint(java.lang.String name, java.lang.Class constraintClass)
static void registerNewConstraint(java.lang.String name, ConstraintFactory factory)
static void removeConstraint(java.lang.String name, java.lang.Class constraintClass)
static void removeConstraint(java.lang.String name)
void setAttributes(java.util.Map attributes)
void setBlank(boolean blank)
void setCreditCard(boolean creditCard)
void setDisplay(boolean display)
@return Returns the editable.
void setEditable(boolean editable)
@return Returns the order.
void setEmail(boolean email)
void setFormat(java.lang.String format)
void setInList(java.util.List inList)
void setMatches(java.lang.String regex)
void setMax(java.lang.Comparable max)
@param max The max to set.
void setMaxSize(java.lang.Integer maxSize)
@param maxSize The maxSize to set.
void setMessageSource(org.springframework.context.MessageSource source)
void setMin(java.lang.Comparable min)
@param min The min to set.
void setMinSize(java.lang.Integer minSize)
@param minSize The minLength to set.
void setNotEqual(java.lang.Object notEqual)
@param notEqual The notEqual to set.
void setNullable(boolean nullable)
void setOrder(int order)
void setPassword(boolean password)
void setPropertyName(java.lang.String propertyName)
@return Returns the url.
void setRange(groovy.lang.Range range)
void setSize(groovy.lang.Range size)
void setUrl(boolean url)
void setWidget(java.lang.String widget)
boolean supportsContraint(java.lang.String constraintName)
Checks with this ConstraintedProperty instance supports applying the specified constraint.
java.lang.String toString()
void validate(java.lang.Object target, java.lang.Object propertyValue, org.springframework.validation.Errors errors)

Wednesday, May 29, 2013

Which function in php validate if the string is valid html?

Html validation function in php:
function isValidHtml($string)
{
    $string = "<div>".$string."</div>";
    $start = strpos($string, '<');
    $end = strrpos($string, '>', $start);
    if($start === false && $end === false) {
        return true;
    }
    if ($end !== false) {
        $string = substr($string, $start);
    } else {
        $string = substr($string, $start, $end - $start);
    }
    libxml_use_internal_errors(true);
    libxml_clear_errors();
    $xml = simplexml_load_string($string);
    return count(libxml_get_errors()) == 0;
}
And best way to use this:

if(isValidHtml($strHtml) {
    echo "Valid html";
}

$strHtml = "<table><tr><td>Hi pritom</td></tr><table>"; is FALSE

Sunday, February 17, 2013

validate domain name using php regex

<?php 
function isValidDomainName($name = null) {
    if(isValidString($name)) {
        if(preg_match("/^(?:[-A-Za-z0-9]+\.)+[A-Za-z]{2,6}$/", getValidString($name))) {
            return true;
        }
    }
    return false;
}

function isValidString($text = null)
{
    if ($text == null) {
        return false;
    }
    if (is_null($text)) {
        return false;
    }
    if (is_string($text)) {
        if (strlen(trim(strval($text)))  0) {
            return true;
        }
    }
    return false;
}
function getValidString($text = null)
{
    if (isValidString($text)) {
        return trim(strval($text));
    }
    return null;
} 
?>