Recommend this page to a friend! |
HtmlForm PHP 5 | > | All threads | > | custom validation | > | (Un) Subscribe thread alerts |
|
w. studer - 2012-01-08 15:12:52
Hi
how to make custom validations, like - check if folder not alreday exists, create but report if order can not be created - check if given value is not already used in the database - ... br Wolfgang
Sebastian Schlapkohl - 2012-01-08 17:24:17 - In reply to message 1 from w. studer
You can define a custom case in a validator like so:
->setValidator(FormValidator::get()->setCustomCase(*expression*)) where expression may be true/false or a string. The string is used to define an individual error-message. An empty string is treated as true, while the message acts as false, while also providing the appropriate text. To check serverside conditions you'll have to resolve them before setting the validator, which is quite easy: Define the whole base form beforehand an make sure to have a references to the parts, that need special validation or carry values that are part of the question. After that you can do something like this: $widget->getValidator()->addCustomCase(checkFunction($otherWidget1->getValue(), $otherWidget2->getValue())) To base the validation on entered data. Of course you would use setValidator if none is present yet.
w. studer - 2012-01-09 08:35:38 - In reply to message 2 from Sebastian Schlapkohl
Hmmm, somehow ... I really don't get it .....
Let's do it by am example //-------------------------------------------------- >?php // Create the form. $myForm = HtmlForm::get('myForm') ->... ; // Add a field $myFields = FieldSet::get()->setLegend('My form'); $myFields->addElement ( InputText::get( 'myField' ) ->setLabel( 'My field' ) ->setText( $myValue ) ->refill() ); $myForm->addElement($myFields); // Check if form has been sent before validating it $successContainer = ''; if ( $myForm->hasBeenSent() ): // Start form validation. $myForm->validate(); // Retrieve a complete valueset from the form. $valueSet = $myForm->getValueSet(); if ( $myForm->isValid() ): ... do something .... endif; endif; ?> <form id="myForm" action="" method="post"> <?=$myForm->doRender() ?> </form> //-------------------------------------------------- Let's assume I have a function "myFunction" which do some checks on "myField" and return TRUE or FALSE. In case of FALSE, an error has to be shown. What do I have to do where ? Thx Wolfgang
Sebastian Schlapkohl - 2012-01-09 09:25:21 - In reply to message 3 from w. studer
The qay this works depends on the fact if you already have all data to validate the input at the moment you define the input.
In that case you can simply define the custom case by writing $myFields->addElement ( InputText::get( 'myField' ) ->setLabel( 'My field' ) ->setText( $myValue ) ->setValidator( FormValidator::get() ->setCustomCase(myFunction($data1, $data2)) ) ->refill() ); Where myFunction would optimally return an empty string on true or an error message on false. If, however your validation relies on user entered data, contained in form elements, you'll have to add the validation after constructing the form (or better the elements), to be able to retrieve the current values of those element. Ideally, if those elements are defined before the custom validation you could do it like this: $element1 = InputText::get()......; $element2 = InputText::get()......; $myFields ->addElement($element1) ->addElement ( InputText::get( 'myField' ) ->setLabel( 'My field' ) ->setText( $myValue ) ->setValidator( FormValidator::get() ->setCustomCase(myFunction($element1->getValue(), $element2->getValue())) ) ->refill() ) ->addElement($element2); What's important here, is that the elements are completely defined, referenced and most probably refilled, to get to the right value in the validation, which will not work with chained definitions, since you can't reference the other elements that way and also can't get any values, while still being in the same, unfinished PHP-statement. It's also good to remember, that you can add rules to a validator later, if the form-definition-code would get out of hand otherwise. In that case, just finish all of the form, then collect all needed validation data and add the custom validation at the end (before validating) with: $collectedVal1 = ......; $collectedVal2 = ......; // in case there already is a validator $myInputTextElement->getValidator()->addCustomCase(myFunction()); // in case there already no validator yet $myInputTextElement->setValidator()->addCustomCase(myFunction());
w. studer - 2012-01-09 09:57:34 - In reply to message 4 from Sebastian Schlapkohl
Woah
thanks for very fast reply. Think I have now to play with this informations. Thanks and bye until next question.
w. studer - 2012-01-10 10:20:28 - In reply to message 5 from w. studer
Great
get it working as per my example above: //-------------------------------------------------- >?php // Create the form. $myForm = HtmlForm::get('myForm') ->... ; $myFields = FieldSet::get()->setLegend('My form'); $dataFields = array(); // Add a field with a validation set later $dataField = InputText::get( 'myField1' ) ->setLabel( 'My field1' ) ->setText( $myValue ) ->refill() ; $dataFields [ 'myField1' ] = $dataField; $myFields->addElement ( $dataField ); // Add a field with a validation set directly after $dataField = InputText::get( 'myField2' ) ->setLabel( 'My field2' ) ->setText( $myValue ) ->refill() ; if ( $xxxxx == 0 ): // field is mandatory $dataField->setValidator( FormValidator::get() ->setRequired() ); elseif ( $xxx > 0 ): // field is protected $dataField>setReadonly(); endif; $myFields->addElement ( $dataField ); // add fields to the form $myForm->addElement($myFields); // add additional validators based on data user entered if ( $myForm->hasBeenSent() ): // Retrieve a complete valueset from the form. $valueSet = $myForm->getValueSet(); if ( passedMyCheck( $valueSet->myField1 ) == FALSE ): $dataField = $dataFields [ 'myField1' ]; $dataField->getValidator()->setCustomCase( "myField1 check failed !" ); endif; endif; // Check if form has been sent before validating it $successContainer = ''; if ( $myForm->hasBeenSent() ): // Start form validation. $myForm->validate(); // Retrieve a complete valueset from the form. $valueSet = $myForm->getValueSet(); if ( $myForm->isValid() ): ... do something .... endif; endif; ?> <form id="myForm" action="" method="post"> <?=$myForm->doRender() ?> </form> //-------------------------------------------------- thanks a lot Wolfgang |
info at phpclasses dot org
.