1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| action 中 --- $this->form = new sfForm(); $this->form->setWidgets(array( 'name' => new sfWidgetFormInputText(), 'email' => new sfWidgetFormInputText(array('default' => '[email protected]')), 'subject' => new sfWidgetFormChoice(array('choices' => array('Subject A', 'Subject B', 'Subject C'))), 'message' => new sfWidgetFormTextarea(), ));
模版中 <?php echo $form; ?>
如果要自定義 <?php echo $from['name']->renderRow(); ?>
如果需要改變 HTML 屬性 - 第一個參數是屬性 - 第二個參數是可選,替換標籤 <?php echo $form['name']->renderRow(array('size' => 25, 'class' => 'foo'), 'Your Name') ?>
可分開來顯示 (則不用 table 結構,自定義) - render() (for the widget), renderError(), renderLabel(), and renderHelp() - 即 <ul> <?php foreach ($form as $field): ?> <li> <?php echo $field->renderLabel() ?> <?php echo $field->render() ?> </li> <?php endforeach; ?> </ul> - 或 <ul> <?php echo $form->renderUsing('list') ?> </ul> setWidget 設置 - $form->setWidget('full_name', new sfWidgetFormInput(array('default' => 'John Doe'))); > <label for="full_name">Full Name</label> <input type="text" name="full_name" id="full_name" value="John Doe" />
- $form->setWidget('address', new sfWidgetFormTextarea(array('default' => 'Enter your address here'), array('cols' => 20, 'rows' => 5))); > <label for="address">Address</label> <textarea name="address" id="address" cols="20" rows="5">Enter your address here</textarea>
- $form->setWidget('pwd', new sfWidgetFormInputPassword()); > <label for="pwd">Pwd</label> <input type="password" name="pwd" id="pwd" />
- $form->setWidget('id', new sfWidgetFormInputHidden(array('default' => 1234))); > <input type="hidden" name="id" id="id" value="1234" />
- $form->setWidget('single', new sfWidgetFormInputCheckbox(array('value_attribute_value' => 'single', 'default' => true))); > <label for="single">Single</label> <input type="checkbox" name="single" id="single" value="true" checked="checked" /> 上傳文件組件和預覽 - $form->setWidget('picture', new sfWidgetFormInputFile()); > <label for="picture">Picture</label> <input id="picture" type="file" name="picture"/>
- $form->setWidget('picture', new sfWidgetFormInputFileEditable(array('default' => '/images/foo.png')));
驗證表單 $this->form->setValidators(array( 'name' => new sfValidatorString(), 'email' => new sfValidatorEmail(), 'message' => new sfValidatorString(array('min_length' => 4)) ));
if ($request->isMethod('post')) { $this->from->bind(/* 用戶提交數據 */); if ($this->form->isValid()) { xxx } }
用戶提交數據解決 - 使用 $this->form->setNameFormat('contact[%s]'); 定義表單 - 之後表單的 key value 就會被 contact 包裹成為 $contact 的陣列 - 則可以通過以下方法 bind,從而只取得表單而不是其他資料 $this->form->bind($request->getParameter('contact')); 顯示全局域錯誤 <?php if ($form->hasErrors()): ?> The form has some errors you need to fix. <?php endif; ?>
使用驗證器必須每個欄位都定義,如果是可選,可設置 required => false $this->form->setValidators(array( 'name' => new sfValidatorString(), 'email' => new sfValidatorEmail(array('required' => false)), 'message' => new sfValidatorString(array('min_length' => 4)) ));
驗證兩個條件以上,可用 sfValidatorAnd / sfValidatorOr $this->form->setValidators(array( 'name' => new sfValidatorString(), 'email' => new sfValidatorAnd( new sfValidatorEmail(), new sfValidatorString(array('min_length' => 4)), ), 'message' => new sfValidatorString(array('min_length' => 4)) ));
自定義錯誤信息 'email' => new sfValidatorEmail( array(), array( 'required' => "Please enter email", 'invalid' => "Please enter a valid email address" ) ), 'message' => new sfValidaorString( array('min_length' => 4), array( 'required' => "Please enter message", 'min_length' => "Please enter a longer message at least 4 char" ) ),
驗證兩次輸入的密碼 $this->form = new sfForm(); $this->form->setWidgets(array( 'login' => new sfWidgetFormInputText(), 'password1' => new sfWidgetFormInputText(), 'password2' => new sfWidgetFormInputText() ); $this->form->setValidators(array( 'login' => new sfValidatorString(), // login is required 'password1' => new sfValidatorString(), // password1 is required 'password2' => new sfValidatorString(), // password2 is required )); $this->form->setPostValidators(new sfValidatorSchemaCompare('password1', '==', 'password2'));
驗證下拉式選單中的資料是否存在於資料庫中 $form->setValidator('section_id', new sfValidatorDoctrineChoice(array( 'model' => 'Section', 'column' => 'name' )));
驗證此用戶名是否已經存在 $form->setValidator('nickname', new sfValidatorDoctrineUnique(array( 'model' => 'User', 'column' => 'login' )));
CSRF 保護 - 設置密碼 $form->addCSRFProtection('flkd445rvvrGV34G'); - settings.yml 中設置整個網站保護 all .settings: csrf_secret: ##…## *** 最好是將 from 儲存為 lib/form/ContactForm.class.php *** 這樣就可以簡單的讀取和改變內裡的資料 (如: setWidget / setValidator)
生成表單 php symfony doctrine:build-forms
|