php - how to validate form field in cakephp using model and controller -
I have created a form that I need to use the model and controller. This is my form
Index.ctp
& lt ;? Php echo $ this- & gt; Form- & gt; Create ('Contact', Array ('url' = & gt; Array ('Controller' = & gt; 'Contacts',' Action '=> Add'))); Transcript $ this- & gt; Form- & gt; Text ('name'); Model: Contact.php
Category contact AppModel extended {var $ name = 'contact'; Var $ useTable = false; Public $ valid = array ('name' = & gt; array ('alpha numeric' = & gt; array ('rule' = & gt; alpha numeric ',' required '= & gt; wrong,' message '= & Gt; 'letters and numbers only'), 'middle' = & gt; array ('rule' = & gt; array ('middle', 5, 15), 'message' => 5 to 15 characters Between) '));} Controller: ContactsController.php
Add Public Function () {$ this-> Contact-> Valid (); $ this- & gt; Request & gt; data ['country'] ['COUNTRY_NAME'] = $ this- & gt; Request & gt; data ['contact'] ['country']; $ this - & gt; Country & gt; Saveall ($ This-> request & gt; data); $ this-> redirect ('/ contact / index /');} I try to verify by googling I am doing it, but it seems difficult for me to describe a process, it would be very helpful. My cake version is 2.3.8. I need to validate the area of this name, when I submit it Clicking will show this message in the form.
Your administrator code should be like this in the KPHPH verification process
1) as you have defined the validation rule in the KPPHP model, `$ Validates = array (); `2) Whenever you save a callback method directly to a particular model or with any collaboration, then Validate is asked to save the data which is being saved 3) One Before saving data, save callback to this The way to save is called after. 4) We use the Farm Input field in the controller for $$ - using Valid- & gt; Validate (), but when saving, we need to disable Validate callback by doing this by $-> Model-> Save ($ data, array ('verify' = & gt; false)); Otherwise you will validate the same data twice
Your administrator code should be something like this.
Add Public Function () {// Here we are checking that the Request Post method is if ($ this-> Request-> ('Post')) { $ This- & gt; Request-> Data ['country'] ['country_name'] = $ this- & gt; Request-> Data ['contact'] ['country']; // Here we are saving data if ($ this- & gt; Contact-> Saving all ($ this- & amp;; -> Request-> Data)) // Here we get the user $$ - Setting up flash message for - Session - & gt; Set Flash ('Your Record Is Added', 'Success'); $ This- & gt; Redirect (Arrays ('Controller' = & gt; 'Contact', 'Action' => 'Index')); } Else {// Here we are setting a flash message for the error message for the user if input $ is not valid as the expected $ -This- & gt; Session-> Set Flash ('Sorry, we can add your record', 'error'); }}} You can always mention for more information
Comments
Post a Comment