CakePHP Version: 4.0.1
// Introduction
I need to validate my grade select list so it accepts an empty string or a populated string.
---
I'd like to check my interpretation of the following sentence which can be found here.
allowEmptyString() Should be used when you want to only accept an empty string.
Does this mean I should only use this function when I ONLY want to accept an empty string or when I want to accept an empty string or a populated string.
The reason I'm asking is because when I use allowEmptyString() it negates the displaying of --Select-- in my select lists. Please see below:
// Markets Add View $options = [ '' => '--Select--', // THIS IS WHAT IS NOT DISPLAYED WHEN I USE allowEmptyString() IN THE MARKETS TABLE VALIDATOR. 'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E' ]; echo $this->Form->control('grade', [ 'type' => 'select', 'options' => $options, 'class' => 'ns-std std', 'autocomplete' => 'off', 'label' => false ]);
// Markets table default validator ->scalar('grade') ->allowEmptyString('grade') // IF I COMMENT THIS LINE THE --Select-- OPTION IS DISPLAYED. ->add('grade', 'validGrade', [ 'rule' => 'isValidGrade', 'message' => __('Invalid grade!'), 'provider' => 'table' ])
---
If I make a literal interpretation of the sentence it means to me that I should NOT use this function in the validator because the grade select list can except a populated string OR an empty string and the sentence reads 'only accept an empty string'
But before I go ahead and delete all references to allowEmptyString() in my validators where appropriate I thought I'd check.
Also by default this would mean that there is no check for a empty or populated string.
---
// My Question
Is my interpretation of the sentence correct and I should NOT use this function because I want to accept an empty OR a populated string.
Thanks Zenzs.
---
EDIT Salines...
// Markets Add View echo $this->Form->select( 'grade', ['A', 'B', 'C', 'D', 'E'], [ 'empty' => '--Select--', 'class' => 'ns-std std', 'autocomplete' => 'off', 'label' => false ], );
// Markets table default validator ->scalar('grade') ->allowEmptyString('grade')
The above method all works and I now validate successfully for an empty string and a populated string.
I'll have to create a new custom validation rule as 0, 1, 2, 3 and 4 instead of A, B, C, D and E are inserted into the database and a cell to handle the conversions in the view but that's no problem. I'll have to do that tommorrow though as I've run out of time today.
http://dlvr.it/RRG5g8
No hay comentarios:
Publicar un comentario