Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****************************/
- /* ABOUT PATCH */
- /****************************/
- /*
- In my application, I have a situation, where user can update some records in database.
- Records which are passed as data array are updated only, so I need to validate them first.
- If I use 'required' on all fields possible, then I must provide all fields on update,
- so I removed 'required' parameter and only include necessary rules.
- What I want to get, for example:
- If I want to edit 'element_name' field, then it MUST be at least 'minLength' value 1, but with
- current version, if 'element_name' is passed empty, validate() function ignores it as
- says it is successfully validated which is NOT TRUE. 'element_name' is not min length 1, because it is empty!
- Another case would be for 'element_quantity'. IF IT IS PASSED (not need to be), then it HAS TO BE an integer value.
- Again, if I pass empty value, this is NOT an integer and if there was no 'required' field, field was ignored and
- flagged as successfully validated.
- */
- /****************************/
- /* EXAMPLE 1 */
- /****************************/
- //Input data
- $data = [
- 'element_name' => '' //Empty name, min lenght is NOT 1
- 'element_quantity' => '' //Empty value, NOT and integer
- ];
- //Rules
- $v->rule('lengthMin', 'element_name', 1)->message(__('Name cannot be empty!'))
- ->rule('integer', 'element_quantity')->message(__('Quantity must be integer!'));
- //Before PATCH
- //Validation passed, 'required' options is not set, values are empty and validation ignores them! False!
- //After PATCH
- //Validation failed, because 'element_name' is not at least 1 character long and 'element_quantity' is NOT an integer
- /****************************/
- /* EXAMPLE 2 */
- /****************************/
- //Input data
- $data = [
- 'element_name' => '' //Empty name, min lenght is NOT 1
- ];
- //Rules in all cases
- $v->rule('lengthMin', 'element_name', 1)->message(__('Name cannot be empty!'))
- ->rule('integer', 'element_quantity')->message(__('Quantity must be integer!'));
- //Before
- //Validation passed, 'element_name' doesn't have 'required' field set
- //After
- //Validation failed, 'element_name' must be at least 1 character long, IF EXISTS IN DATA ARRAY.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement