{"id":26793095,"url":"https://github.com/beapi/bea-form","last_synced_at":"2025-03-29T16:19:04.944Z","repository":{"id":34377712,"uuid":"38303696","full_name":"BeAPI/bea-form","owner":"BeAPI","description":"Form simple class for handling form messages and errors and infos","archived":false,"fork":false,"pushed_at":"2023-11-28T13:10:20.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-28T07:51:33.438Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BeAPI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-30T10:46:46.000Z","updated_at":"2023-11-28T13:06:35.000Z","dependencies_parsed_at":"2022-09-14T04:40:51.546Z","dependency_job_id":null,"html_url":"https://github.com/BeAPI/bea-form","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BeAPI%2Fbea-form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BeAPI%2Fbea-form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BeAPI%2Fbea-form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BeAPI%2Fbea-form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BeAPI","download_url":"https://codeload.github.com/BeAPI/bea-form/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246207500,"owners_count":20740723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-29T16:19:04.472Z","updated_at":"2025-03-29T16:19:04.936Z","avatar_url":"https://github.com/BeAPI.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# bea-form\nSimple form class for handling form messages and errors and infos in WordPress\n\n# Usage\nThe usage is very simple, you add messages to singleton class and display them easily.\nThe purpose of the class is to listen to changes on the page reload when the user submit form, and when there is a\nbreaking event, like action success or breaking error message, redirect the user to the same page but with a code. \n\nYou have two methods for display the message the first one is to add messages directly on the class.\n\n# Example :\n\n## In the template\n```php\n\n\u003cform method=\"post\"\u003e\n\t\u003c?php Bea_Form::get_instance()-\u003edisplay_contextual_or_post( 'my_action' ); ?\u003e\n\t\n\t\u003clabel for=\"name\" \u003e Your name \u003c/label\u003e\n\t\u003cinput type=\"text\" name=\"name\" id=\"name\" required /\u003e\n\t\u003cinput type=\"hidden\" name=\"action\" value=\"my_action\" /\u003e\n\t\u003c?php wp_nonce_field( 'my-action' ); ?\u003e\n\u003c/form\u003e\n```\n\n## In your controller\n\n```php\nadd_action('wp', 'my_action_check'); \nfunction my_action_check() {\n\t$form = Bea_Form::get_instance();\n\t\n\t// First check the element is in post\n\tif ( 'my_action' !== $form::element_in_post( 'action' ) ) {\n\t\treturn;\n\t}\n\t\n\t/**\n\t* Stop script and redirect to the same page with error\n\t*/\n\tif ( false === $form-\u003echeck_nonce( 'my-action' ) ) {\n\t\t/**\n\t\t* This will redirect to : domain.com/my-url?code=0\u0026action=my_action\n\t\t*/\n\t\t wp_safe_redirect( add_query_arg( array( 'code' =\u003e 0, 'action' =\u003e $form::element_in_post( 'action' ) ), home_url( '/my-url' ) ) );\n\t\t exit;\n\t}\n\t\n\t$name = trim( $form-\u003eelement_in_post( 'my_action' ) );\n\tif( empty( $name ) ) {\n\t\t$form-\u003eadd_general_error( __( 'There is an error on this form', 'my-text-domain' ) );\n\t\t$form-\u003eadd_error( 'name', __( 'Please fill your name', 'my-text-domain' ) );\n\t\treturn;\n\t}\n\t\n\t// Stop generation if there is any errors on the form\n\tif ( $form-\u003ehave_form_error() ) {\n\t\treturn;\n\t}\n\t\n\t/**\n\t * Make your actions and then redirect to the same page\n\t * This will redirect to : domain.com/my-url?code=1\u0026action=my_action\n\t */\n\t wp_safe_redirect( add_query_arg( array( 'code' =\u003e 1, 'action' =\u003e $form::element_in_post( 'action' ) ), home_url( '/my-url' ) ) );\n\t exit;\n}\n```\n\n## Displaying the messages\n\nAnd now we need to display the message on redirect, with the code and the action, they are not innocent.\nYou only have to add a filter with all the messages, the filter name is based on this pattern \n\n__bea\\_form\\_{action}\\_messages__\n\n```php\n/**\n * Messages for the form options\n */\nadd_filter( 'bea_form_'.'my_action'.'_messages', 'my_form_messages' );\n\nfunction my_form_messages() {\n\treturn array(\n\t\t0 =\u003e array( 'type' =\u003e 'error', 'message' =\u003e 'Error !' ),\n\t\t1 =\u003e array( 'type' =\u003e 'success', 'message' =\u003e 'Success !' ),\n\t\t2 =\u003e array( 'type' =\u003e 'info', 'message' =\u003e 'Info !' ),\n\t);\n}\n```\n\nThe array key represents the error code and the values represent the types of messages you want to display and the messages.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeapi%2Fbea-form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeapi%2Fbea-form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeapi%2Fbea-form/lists"}