Validation rule

8 posts, 0 answers
  1. Jan Kaare
    Jan Kaare avatar
    16 posts
    Member since:
    Dec 2010

    Posted 19 Apr 2013 Link to this post

    For the grid PHP wrapper usage, how to set up a string pattern validation rule for string value XX:XX ie. resulting string 12:15 as an example?

    And for the same grid PHP wrapper usage, how to set a grid string cell's maxlength, to let the cell input operate identical to what's found from standard html maxlength?

  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 23 Apr 2013 Link to this post

    Hello Jan,

    Like the other type of validations, pattern should be set in the DataSource's schema. For example:
    $productNameField = new \Kendo\Data\DataSourceSchemaModelField('ProductName');
    $productNameField->type('string')
               ->validation(array('required' => true, 'pattern' => '< your pattern >'));

    Regarding your second question, there is no text maxlenght validation rule among the default ones. If you would like to check the length of the user input, you may use a pattern or custom validation rule.

    Kind regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET AJAX banner
  4. Jaymie
    Jaymie avatar
    1 posts
    Member since:
    Mar 2013

    Posted 03 Jun 2013 Link to this post

    Hi Alexander,

    Would you be able to post an example of how to implement a custom validation rule using the PHP Wrapper. I have had several attempts at implementing one myself but can't get it to test against the rule.

    Thanks in advance,
    Jaymie
  5. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 05 Jun 2013 Link to this post

    Hi Jaymie,

    The tricky part is to use \Kendo\JavaScriptFunction in order to serialize the custom rule function properly.

    Example 1:
    $productNameField->type('string')
                     ->validation(array('required' => true, 'myCustomRule' => new \Kendo\JavaScriptFunction('myCustomRule')));
     
    //where myCustomRule is a name of a JavaScript function

    Example 2:
    class customSchemaValidation extends \Kendo\Data\DataSourceSchemaModelFieldValidation {
        public function foo($value) {
            if (is_string($value)) {
                $value = new \Kendo\JavaScriptFunction($value);
            }
     
            return $this->setProperty('foo', $value);
        }
    }
     
    $unitPriceValidation = new customSchemaValidation();
    $unitPriceValidation->required(true)
                 ->foo('foo')
                 ->min(1);
     
    //where foo is the name of the custom rule function

    I hope this information will help.

    Regards,
    Alexander Valchev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Raymond
    Raymond avatar
    8 posts
    Member since:
    Jul 2012

    Posted 16 Oct 2013 Link to this post

    Can you please show an example of what the myCustomRule javascript function might look like in example 1 ?  

    I'm using a popup template to edit some fields from a grid and want to validate the form when the Update button is clicked. 
    I tried using this method on a single DataSource field, with none of the other fields (5 in total) set to be 'required'.  When I click the Update button, the javascript function is called and it appears that the entire popup window is passed as an argument.  In addition it seems that all 5 of my fields are validated, as an empty notification tooltip is generated for each.
  7. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 21 Oct 2013 Link to this post

    Hello Raymond,

    Indeed the validation rule will be executed against all inputs in the form on `Update` button click. You will have to assert against that field and perform validation in the rule is for that field. Here few examples for this: 
     - Custom Rules
     - Custom Validation Rules Overview

    It is exactly the same as in the above articles in the context of the Grid widget. Here is an example with custom validation rule for `ProductName` in Grid popup editing: http://jsbin.com/iVuZeRi/1/edit

    Regarding the empty validation tooltip - you can define custom validation message per validation rule on the input element as data attribute. In the above example the validation rule is named "custom" and the ProductName input has data attribute data-custom-msg="some text" which is the validation message for that rule.
     
    You can find more details on how validation messages works here: Defining Custom Messages.

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  8. Raymond
    Raymond avatar
    8 posts
    Member since:
    Jul 2012

    Posted 21 Oct 2013 Link to this post

    Thank you, that helps with the custom message, but does not help with the initial problem.

    I want to be able to validate when I press the Update button.  A user might hit update without entering a field, thus never activating an onBlur, or I may have interdependent fields where I don't want to validate until I'm done.  The examples you kindly provided don't help because I'm using the popup template, the 'Update' button isn't part of my template form, it's part of the kendo window generated for the popup, so it doesn't trigger validation.
  9. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 23 Oct 2013 Link to this post

    Hello Raymond,

    In such a case you should handle the click of that custom button, find the Kendo Validator which is initialized for the edit form and call its validate method. For example: http://jsbin.com/iNIqul/1/edit

    Regards,
    Nikolay Rusev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top
UI for ASP.NET AJAX banner