Jump to content

Juergen

Members
  • Posts

    1,342
  • Joined

  • Last visited

  • Days Won

    14

Everything posted by Juergen

  1. Hello @Andy I have added your rule as the first community provided validation rule to FrontendForms. Take a look here. This validation rule is included in the next update, but if you do not want to wait until the next update, you only have to replace the code of the CustomRules.php from Github with yours and you are done.
  2. Hello @Andy Glad that it works like you want!! For this check, it is not really necessary to write a custom rule. You can also use the regex validation rule and add the regex there, but it is not wrong if you need this validation rule often.😉
  3. Hello @Andy I will write a new section in the docs in the next time about this possibility. Please note: If you want to use API variables inside your custom validation rule I guess you need to add this rule to the site/ready.php instead of the site/init.php. I have not tested it, but I guess you need to do it this way, but you can try it out. That is a good idea and if a validation rule seems to be useful for others I will add it to the FrontendForms validation rules file, so everyone can use it. I guess there are around 60 validation rules available at the moment, but more is more in this case. 😉
  4. Good news @Andy I have taken a look at the Valitron library, which I use to validate form fields and this library offers an easy to use solution to add custom rules. Take a look here. To add a custom rule to ProcessWire you need to add it to the site/init.php. For demonstration puroses I use the example from the Valitron docs. So please add this code (which always returns false) to your site/init.php \Valitron\Validator::addRule('alwaysFail', function($field, $value, array $params, array $fields) { // here you can write your validation code - this should return true or false return false; }, 'Everything you enter is not correct. You fail.'); Now you have added a new custom rule, which you can use sitewide like all other validation rules. Here is a form example containing this rule: $form = new \FrontendForms\Form('test'); $inputText = new \FrontendForms\InputText('text'); $inputText->setLabel('Input Text'); $inputText->setRule('required'); $inputText->setRule('alwaysFail'); // here is the new validation rule $form->add($inputText); $button = new \FrontendForms\Button('submit'); $button->setAttribute('value', 'Send'); $form->add($button); if ($form->isValid()) { } echo $form->render(); If you validate the form, you will always get the error message "Everything you enter is not correct. You fail." because this rule always returns false. You can take this example as a starting point for your own validation rule. The validation code must return a boolean value (true or false). Hope this helps
  5. Hello @Andy I will take your issue as an idea for a new feature and I will take a look if I could find a good working solution. At the moment I will wait if there are some unwanted negative side effects concerning the last update. If everything works as expected and no issues occur/will be reported I will go on to the next step and try to find a solution to your issue. Best regards
  6. I have updated FrontendForms to support hooks to customize the markup of certain elements. As written in a the blog post above there was a problem of using hooks, because they never fired. This should be solved now. You will find a working example on this website. If you press submit without filling out the form, all error messages will get an exclamation sign in front of the error message. This is done via the following hook inside the site/init.php: $wire->addHookAfter('Errormessage::render', function(HookEvent $event) { $msg = $event->object; $errorText = $msg->getText(); if($errorText){ $fontAwesome = '⚠ '; $event->return = $fontAwesome.$errorText; } }); This is for demonstration puropose that it works now! In addition I have added some other hook examples inside the Hooking section of the docs. This bug fix includes the change of really a lot of files of FrontendForms, so please that care that everything works after the update as expected. Make a backup before you update the module!!! Best regards
  7. The form will be always submitted after pressing the send button 😉 You have the choice, what should be done inside the isValid() function. This method returns always true if the "standard validation does not fail". It is up to you to make another validation inside the isValid() method, but there is no possibility to make isValid to false manually. As I explained in the example some post before, you can do addtional checks inside the isValid() method. Depending on the results of your custom checks you can send fe an email or not. The only problem could be that the form will not be displayed after isValid() is true. The code is not designed to handle such special cases, sorry!
  8. Try $alert0 = $form->getAlert(); $alert0->setText($e_msg); $alert0->setCSSClass('alert_dangerClass'); // or alert_successClass or alert_warningClass setErrorMsg() is a method of the form object
  9. I need to check the whole module files 🤪. After that I will bump up the version number, so you can download the latest version with the fixes.
  10. How can I miss that - I've always overlooked that! Thanks for the relevant information - this will save me a lot of time and troubles in the future.🙂
  11. Hello @PWaddict It seems that @Robin S has found the problem. Take a look here. I'll test it on mylocalhost and on the live site over the weekend and post my results here, but I'm sure this solves the problem.
  12. I have tried it and now it is working well on my localhost!!!! I will test it on a live site too, but it should work there too. Thank you!!!!!
  13. This could be the possible reason for the strange behavior. I will try it! Thank you so much!! 🙂
  14. I have no clue why this works under certain circumstances. I will take a look at the weekend. Otherwise I delete the hooking section if it does not work properly.
  15. You are right! It works on my localhost, but not on my live site. This is very strange!!!!
  16. It is a little bit difficult to explain: Direct call of a class method on a page: $form = new Form(); echo $form->render(); The method will be called directly on a page, so hooking Form::render works. Direct call of a method of another class inside a method on a page: public function ___render() { $out = ''; $object = new Element(); $out .= $object->renderElement(); return $out; } As you can see the method renderElement of the class Element will be called inside the render method of the Form class so Element::renderElement is hookable too. The problem occurs if a method of the Element class should be hooked that is not directly called inside the given renderElement() method. class Element { public function ___test() { return 'Test'; } public function ___renderElement{ return 'Element'; } } So if you want to hook Element::test it will not be triggered, because it will not executed inside the renderElement() method. That was my experience with the hook problem
  17. Could you please post the code of your init.php. I have taken the screenshots after adding this code to my init.php and as you can see it works in my case.
  18. Conclusion: It is not possible to hook a class that is not loaded directly on a page. In my case, the Label class is loaded and used indirectly within the form, as it is used within the inputfield class and not directly in the form class. The form class is loaded directly onto the page and can be hooked - the Label class is not.
  19. Hello @PWaddict I have adapted the hook to change the form markup inside the readme file to get it working now. The previous versions of hooks did not work, because the hooks will never be triggered. Instead you have to hook "Form::render" and change the markup there. Here you can see the example of the readme file to change the markup of the asterisk on required fields. Before: After: Hope this helps
  20. Hello @Frank Vèssia I have taken a look at this issue and I will not change the "untitled-xxxx" to something else, because this username is absolutely unique. If you want to prevent, such random usernames to be stored inside the database, then please go to the module configuration and go to the tab for the registration form. There please add the field "Username" to the registration form (and to the profile form as well if you want). This forces the user to enter a username during the registration process and you will not get the randomly created "untitled-xxxx". Best regards
  21. Hello @Frank Vèssia The untitled-xxxxx as name will be added by default if you choose login with email and password and not with username and password. BTW the random username can be overwritten by the user itself if he wants. So there is no need for the user to accept this random username. I know this is not a really nice random username, but using the email address instead is not a good idea, because if you want to output the username on the frontend, everyone can see the email address. I guess the "untitled" will be added by PW if nothing is entered in the name field and the number will be appended by a hook as I can remember, but I have to take a look (I do not remember exactly). The untitled-xxxx will be added by PW and makes the name absolutely unique. I will think over if there will be simple solution to change the name to a more nicer one 😉
  22. Hello @Andy Take a look at the custom rules. There are some ready-to-use validators which check against the database (fe. uniqueEmail to check if email is not registered inside the user table). List of Custom rules If you want to check against custom tables, you have to check it inside the isValid() method by your own. There is no possibility to create and add custom rules by yourself. if ($form->isValid()) { //make your custom check here (fe checking a form value against the database) if(customcheck is valid){ // go on with your code } else { // set an error message at the top of the form $alert = $form->getAlert(); $alert->setErrorMessage('Sorry, but there are errors inside the form'); // set an error message on the inputfield itself (fe. an email input) $emailInput = $form->getFormElementByName('email); $emailInput->setErrorMessage('There is an error'); } } I have not tested the code above, but you got the idea such an issue could be solved. The last option is to write an request on Github for a new validator and if it could be useful for others I will try to implement it 😉 Best regards
  23. Thanks for your quick response @bernhard I compared your example to mine and I see that you are calling the method ($foo->hello()) inside your ready() method. In this case it works on my side too, but this is not the scenario that I want. I want to manipulate the output not inside the ready() but before page render, so the markup of the method should be changed before the page will be rendered. public function ___renderAsterisk(): string { return '<span class="asterisk">*</span>'; } This is a render function, which will be called during the render function of the form and the form will be rendered during the page will be rendered. So I want to change the output of the renderAsterisk() method inside the site/init.php and this does not work at all. wire()->addHookAfter('Label::renderAsterisk', function ($event) { $event->return = ' - hooked aha :)'; }); So this works fine if the renderAsterisk() method will be called inside the ready.php separately. $foo = new Label(); bd($foo->renderAsterisk()); But it does not work if the renderAsterisk() method is included inside another render function (in this case inside the render function of the form. This should be the issue of the problem. To simplify: It works if the renderAsterisk() method will be called directly, but not if this method is included within another method and will be called there.
  24. Hello @bernhard This is interesting, but in my case the hook does not get fired. It seems that that it will be ignored. The function is hookable by prefixing 3 underscores: public function ___renderAsterisk(): string { return '<span class="asterisk">*</span>'; } The class 'Label' itself (where the function above is part of) is derived from the Wire class (take a look here on the top parent class), so hooks must be implemented and should work. Only to mention: the top parent class called 'Tag' is an abstract class - could this have an impact on the issue? Do you have a working example in one of your numerous modules, where you use a hook inside the init.php to hook a method in one of your modules?
×
×
  • Create New...